PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.17.0
GiveWP – Donation Plugin and Fundraising Platform v4.17.0
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / DonationForms / resources / app / utilities / registerFieldAndBuildProps.tsx

registerFieldAndBuildProps.tsx in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at src/DonationForms/resources/app/utilities/registerFieldAndBuildProps.tsx

37 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {Field} from '@givewp/forms/types';
2 import {FieldProps} from '@givewp/forms/propTypes';
3 import {UseFormReturn} from 'react-hook-form';
4 import buildRegisterValidationOptions from './buildRegisterValidationOptions';
5 import getErrorByFieldName from './getErrorByFieldName';
6
7 const formTemplates = window.givewp.form.templates;
8 const LabelTemplate = formTemplates.layouts.fieldLabel;
9 const ErrorMessageTemplate = formTemplates.layouts.fieldError;
10
11 /**
12 * @since 4.3.0 include aria-required attribute in all required fields.
13 */
14 export default function registerFieldAndBuildProps(
15 field: Field,
16 register: UseFormReturn['register'],
17 errors
18 ): FieldProps {
19 const fieldError = getErrorByFieldName(errors, field.name);
20 const validationOptions = buildRegisterValidationOptions(field.validationRules);
21
22 const baseInputProps = register(field.name, validationOptions);
23
24 const inputProps = {
25 ...baseInputProps,
26 'aria-required': !!field.validationRules?.required ? 'true' : undefined,
27 };
28
29 return {
30 ...field,
31 inputProps: inputProps,
32 fieldError,
33 Label: () => <LabelTemplate label={field.label} required={field.validationRules?.required} />,
34 ErrorMessage: () => <ErrorMessageTemplate error={fieldError} name={field.name} />,
35 };
36 }
37