|
| 1 | +import Alert from '@TutorShared/atoms/Alert'; |
| 2 | +import ComponentErrorBoundary from '@TutorShared/components/ComponentErrorBoundary'; |
| 3 | +import FormCheckbox from '@TutorShared/components/fields/FormCheckbox'; |
| 4 | +import FormDateInput from '@TutorShared/components/fields/FormDateInput'; |
| 5 | +import FormFileUploader from '@TutorShared/components/fields/FormFileUploader'; |
| 6 | +import FormImageInput from '@TutorShared/components/fields/FormImageInput'; |
| 7 | +import FormInput from '@TutorShared/components/fields/FormInput'; |
| 8 | +import FormRadioGroup from '@TutorShared/components/fields/FormRadioGroup'; |
| 9 | +import FormSelectInput from '@TutorShared/components/fields/FormSelectInput'; |
| 10 | +import FormSwitch from '@TutorShared/components/fields/FormSwitch'; |
| 11 | +import FormTextareaInput from '@TutorShared/components/fields/FormTextareaInput'; |
| 12 | +import FormTimeInput from '@TutorShared/components/fields/FormTimeInput'; |
| 13 | +import FormVideoInput from '@TutorShared/components/fields/FormVideoInput'; |
| 14 | +import FormWPEditor from '@TutorShared/components/fields/FormWPEditor'; |
| 15 | +import { type FormControllerProps } from '@TutorShared/utils/form'; |
| 16 | +import { FieldType, type Option } from '@TutorShared/utils/types'; |
| 17 | +import { Controller, type RegisterOptions, type UseFormReturn } from 'react-hook-form'; |
| 18 | + |
| 19 | +interface FieldRendererProps { |
| 20 | + name: string; |
| 21 | + label?: string; |
| 22 | + buttonText?: string; |
| 23 | + helpText?: string; |
| 24 | + infoText?: string; |
| 25 | + placeholder?: string; |
| 26 | + type: FieldType; |
| 27 | + options?: Option<string | number>[]; |
| 28 | + defaultValue?: unknown; |
| 29 | + rules?: Exclude<RegisterOptions, 'valueAsNumber' | 'valueAsDate' | 'setValueAs'>; |
| 30 | + form: UseFormReturn; |
| 31 | +} |
| 32 | + |
| 33 | +const FieldRenderer = ({ |
| 34 | + name, |
| 35 | + label, |
| 36 | + buttonText, |
| 37 | + helpText, |
| 38 | + infoText, |
| 39 | + placeholder, |
| 40 | + type, |
| 41 | + options, |
| 42 | + defaultValue, |
| 43 | + rules, |
| 44 | + form, |
| 45 | +}: FieldRendererProps) => { |
| 46 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 47 | + const renderField = (controllerProps: FormControllerProps<any>) => { |
| 48 | + const field = (() => { |
| 49 | + switch (type) { |
| 50 | + case 'text': |
| 51 | + return <FormInput {...controllerProps} label={label} placeholder={placeholder} helpText={helpText} />; |
| 52 | + case 'number': |
| 53 | + return ( |
| 54 | + <FormInput {...controllerProps} type="number" label={label} placeholder={placeholder} helpText={helpText} /> |
| 55 | + ); |
| 56 | + case 'password': |
| 57 | + return ( |
| 58 | + <FormInput |
| 59 | + {...controllerProps} |
| 60 | + type="password" |
| 61 | + label={label} |
| 62 | + placeholder={placeholder} |
| 63 | + helpText={helpText} |
| 64 | + /> |
| 65 | + ); |
| 66 | + case 'textarea': |
| 67 | + return <FormTextareaInput {...controllerProps} label={label} placeholder={placeholder} helpText={helpText} />; |
| 68 | + case 'select': |
| 69 | + return ( |
| 70 | + <FormSelectInput |
| 71 | + {...controllerProps} |
| 72 | + label={label} |
| 73 | + options={options || []} |
| 74 | + placeholder={placeholder} |
| 75 | + helpText={helpText} |
| 76 | + /> |
| 77 | + ); |
| 78 | + case 'radio': |
| 79 | + return <FormRadioGroup {...controllerProps} label={label} options={options || []} />; |
| 80 | + case 'checkbox': |
| 81 | + return <FormCheckbox {...controllerProps} label={label} />; |
| 82 | + case 'switch': |
| 83 | + return <FormSwitch {...controllerProps} label={label} helpText={helpText} />; |
| 84 | + case 'date': |
| 85 | + return <FormDateInput {...controllerProps} label={label} placeholder={placeholder} helpText={helpText} />; |
| 86 | + case 'time': |
| 87 | + return <FormTimeInput {...controllerProps} label={label} placeholder={placeholder} helpText={helpText} />; |
| 88 | + case 'image': |
| 89 | + return ( |
| 90 | + <FormImageInput |
| 91 | + {...controllerProps} |
| 92 | + label={label} |
| 93 | + buttonText={buttonText} |
| 94 | + helpText={helpText} |
| 95 | + infoText={infoText} |
| 96 | + /> |
| 97 | + ); |
| 98 | + case 'video': |
| 99 | + return ( |
| 100 | + <FormVideoInput |
| 101 | + {...controllerProps} |
| 102 | + label={label} |
| 103 | + buttonText={buttonText} |
| 104 | + helpText={helpText} |
| 105 | + infoText={infoText} |
| 106 | + /> |
| 107 | + ); |
| 108 | + case 'uploader': |
| 109 | + return <FormFileUploader {...controllerProps} label={label} buttonText={buttonText} helpText={helpText} />; |
| 110 | + case 'WPEditor': |
| 111 | + return <FormWPEditor {...controllerProps} label={label} placeholder={placeholder} helpText={helpText} /> |
| 112 | + default: |
| 113 | + return <Alert type="danger">Unsupported field type: {type}</Alert>; |
| 114 | + } |
| 115 | + })(); |
| 116 | + |
| 117 | + return ( |
| 118 | + <ComponentErrorBoundary |
| 119 | + componentName={`field ${name}`} |
| 120 | + onError={(error, errorInfo) => { |
| 121 | + console.warn(`Field ${name} failed to render:`, { error, errorInfo }); |
| 122 | + }} |
| 123 | + > |
| 124 | + {field} |
| 125 | + </ComponentErrorBoundary> |
| 126 | + ); |
| 127 | + }; |
| 128 | + |
| 129 | + return ( |
| 130 | + <Controller |
| 131 | + name={name} |
| 132 | + control={form.control} |
| 133 | + defaultValue={defaultValue ?? ''} |
| 134 | + rules={rules} |
| 135 | + render={(controllerProps) => renderField(controllerProps)} |
| 136 | + /> |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +export default FieldRenderer; |
0 commit comments