import { useForm <%= ', InertiaFormProps ' if inertia_js_version.release == Gem::Version.new('1.3.0') %>} from '@inertiajs/react' import { FormEvent } from 'react' import { <%= inertia_model_form_type %>, <%= inertia_model_type %> } from './types' <% if inertia_js_version.release != Gem::Version.new('1.3.0') %> // Temporary fix for InertiaFormProps not being exported from @inertiajs/react type InertiaFormProps> = ReturnType> <% end %> interface FormProps { <%= singular_table_name %>: <%= inertia_model_type %> onSubmit: (form: InertiaFormProps<<%= inertia_model_form_type %>>) => void submitText: string } export default function Form({ <%= singular_table_name %>, onSubmit, submitText }: FormProps) { const form = useForm<<%= inertia_model_form_type %>>({ <% attributes.reject { |a| a.attachment? || a.attachments? }.each do |attribute| -%> <% if attribute.password_digest? -%> password: '', password_confirmation: '', <% else -%> <%= attribute.column_name %>: <%= singular_table_name %>.<%= attribute.column_name %>, <% end -%> <% end -%> }) const { data, setData, errors, processing } = form const handleSubmit = (e: FormEvent) => { e.preventDefault() onSubmit(form) } return (
<% attributes.each do |attribute| -%>
<% if attribute.password_digest? -%> setData('password', e.target.value)} /> {errors.password && (
{errors.password}
)}
setData('password_confirmation', e.target.value)} /> {errors.password_confirmation && (
{errors.password_confirmation}
)} <% else -%> <% if input_type(attribute) == "text_area" -%>