interface IFormProps { // IsSubmitting?: boolean; // submitText?: string; // resetText?: string; // ( ) validateOnChange?: boolean; // blur'e ( ) validateOnBlur?: boolean; // , . config: IFieldsFormMetaModel[]; // . fields: FormFields; // validationSchema: Yup.MidexSchema; // onSubmit?: () => void; // reset onReset?: (e: React.MouseEvent<HTMLElement>) => void; // onChangeField?: ( e: React.SyntaticEvent<HTMLInputElement, name: string; value: string ) => void; // + onChangeFields?: (values: FormFields, prop: { isValid }) => void; }
render() { const { fields, validationSchema, validateOnBlur = true, validateOnChange = true, } = this.props; return ( <Formik initialValues={fields} render={this.renderForm} onSubmit={this.handleSubmitForm} validationSchema={validationSchema} validateOnBlur={validateOnBlur} validateOnChange={validateOnChange} validate={this.validateFormLevel} /> ); }
private validateFormLevel = (values: FormFields) => { const { onChangeFields, validationSchema } = this.props; if (onChangeFields) { validationSchema .validate(values) .then(() => { onChangeFields(values, { isValid: true }); }) .catch(() => { onChangeFields(values, { isValid: false }); }); } }
private handleSubmitForm = (): void => { const { onSubmit } = this.props; if (onSubmit) { onSubmit(); } }
interface IFieldsFormMetaModel { /** */ sectionName?: string; sectionDescription?: string; fieldsForm?: Array<{ /** */ name?: string; // prop 'fields' /** checked */ checked?: boolean; /** enum, */ type?: ElementTypes; /** */ label?: string; /** */ helperText?: string; /** */ required?: boolean; /** */ disabled?: boolean; /** - */ minLength?: number; /** */ initialValue?: IInitialValue; /** */ selectItems?: ISelectItems[]; // select, dropdown }>; }
export const config: IFieldsFormMetaModel[] = [ { sectionName: ' ', fieldsForm: [{ name: 'subject', label: '', type: ElementTypes.Text, }], }, { sectionName: '', sectionDescription: ' ', fieldsForm: [{ name: 'reminder', disabled: true, label: '', type: ElementTypes.CheckBox, checked: true, }], }, ];
const fields: SomeBusinessApiFields = { subject: ' ', reminder: 'yes', }
export const CreateClientSchema: ( props: CreateClientProps, ) => Yup.MixedSchema = (props: CreateClientProps) => Yup.object( { subject: Yup.string(), description: Yup.string(), date: dateSchema, address: addressSchema(props), }, );
fieldsMap: Record< ElementTypes, ( state: FormikFieldState, handlers: FormikHandlersState, field: IFieldsFormInfo, ) => JSX.Element > = { [ElementTypes.Text]: ( state: FormikFieldState, handlers: FormikHandlersState, field: IFieldsFormInfo ) => { const { values, errors, touched } = state; return ( <FormTextField key={field.name} element={field} handleChange={this.handleChangeField(handlers.setFieldValue, field.name)} handleBlur={handlers.handleBlur} value={values[field.name]} error={touched[field.name] && errors[field.name] || ''} /> ); }, [ElementTypes.TextSearch]: (...) => {...}, [ElementTypes.TextArea]: (...) => {...}, [ElementTypes.Date]: (...) => {...}, [ElementTypes.CheckBox]: (...) => {...}, [ElementTypes.RadioButton]: (...) => {...}, [ElementTypes.Select]: (...) => {...}, };
Source: https://habr.com/ru/post/461919/
All Articles