|
| 1 | +import {useState} from 'react' |
| 2 | +import {Meta, Story, Canvas} from '@storybook/addon-docs/blocks' |
| 3 | +import noop from 'lodash/noop' |
| 4 | +import Fields from './Fields' |
| 5 | + |
| 6 | +export const fieldsSample = [ |
| 7 | + { |
| 8 | + __typename: 'GravityFormsFormToObjectFieldUnionConnectionEdge', |
| 9 | + node: { |
| 10 | + __typename: 'CheckboxField', |
| 11 | + id: 2, |
| 12 | + adminLabel: '', |
| 13 | + adminOnly: null, |
| 14 | + allowsPrepopulate: false, |
| 15 | + conditionalLogic: { |
| 16 | + __typename: 'ConditionalLogic', |
| 17 | + rules: null, |
| 18 | + actionType: null, |
| 19 | + logicType: null |
| 20 | + }, |
| 21 | + cssClassList: [], |
| 22 | + label: 'Checkbox field example 2.', |
| 23 | + type: 'checkbox', |
| 24 | + visibility: 'visible', |
| 25 | + checkboxChoices: [ |
| 26 | + { |
| 27 | + __typename: 'ChoiceProperty', |
| 28 | + isSelected: false, |
| 29 | + text: 'New Choice 1', |
| 30 | + value: 'New Choice 1' |
| 31 | + }, |
| 32 | + { |
| 33 | + __typename: 'ChoiceProperty', |
| 34 | + isSelected: false, |
| 35 | + text: 'New Choice 2', |
| 36 | + value: 'New Choice 2' |
| 37 | + }, |
| 38 | + { |
| 39 | + __typename: 'ChoiceProperty', |
| 40 | + isSelected: false, |
| 41 | + text: 'New Choice 3', |
| 42 | + value: 'New Choice 3' |
| 43 | + } |
| 44 | + ], |
| 45 | + description: 'This field is required.', |
| 46 | + enableChoiceValue: null, |
| 47 | + enableSelectAll: true, |
| 48 | + errorMessage: '', |
| 49 | + inputName: '', |
| 50 | + inputs: [ |
| 51 | + { |
| 52 | + __typename: 'CheckboxInputProperty', |
| 53 | + id: 2.1, |
| 54 | + label: 'New Choice 1', |
| 55 | + name: '' |
| 56 | + }, |
| 57 | + { |
| 58 | + __typename: 'CheckboxInputProperty', |
| 59 | + id: 2.2, |
| 60 | + label: 'New Choice 2', |
| 61 | + name: '' |
| 62 | + }, |
| 63 | + { |
| 64 | + __typename: 'CheckboxInputProperty', |
| 65 | + id: 2.3, |
| 66 | + label: 'New Choice 3', |
| 67 | + name: '' |
| 68 | + } |
| 69 | + ], |
| 70 | + isRequired: true, |
| 71 | + size: 'medium' |
| 72 | + } |
| 73 | + } |
| 74 | +] |
| 75 | + |
| 76 | +<Meta title="Design System/Molecules/GravityForm/Fields" component={Fields} /> |
| 77 | + |
| 78 | +# Fields |
| 79 | + |
| 80 | +The `<Fields />` component handles maps through each GravityForm field in the form and matches it up with a GravityForm field component. |
| 81 | + |
| 82 | +_Note: This component should not be used outside of the `<GravityForm />._ |
| 83 | + |
| 84 | +## Adding a new field |
| 85 | + |
| 86 | +To add a new form field do the following: |
| 87 | + |
| 88 | +1. Create a test form in the GravityForm UI. |
| 89 | +2. Add the field that you would like to add support for. |
| 90 | +3. Add the form to a test page that renders the GravityForm block. |
| 91 | +4. Next, locate the Field directory `components/molecules/GravityForm/Fields/` and create a new component file such as `Text.js`. Note: This component name should matchup with the field name from GravityForms, not the input name. |
| 92 | +5. In `Fields/Fields.js` add a new case to the switch statement for your field type. All GravityForm fields should have a `type` property. You can reference your component by using dot notation ex:`GfFields.Text`. |
| 93 | +6. Check that the new Field component renders on your test page from step 3. |
| 94 | + |
| 95 | +**Things to Know** |
| 96 | + |
| 97 | +- Input components should be created in `components/atoms/Inputs/` directory. |
| 98 | +- GravityForm Field components should map props to the inputs. |
| 99 | +- Files names should correspond to the field type. |
| 100 | + |
| 101 | +## Setting up Field Defaults |
| 102 | + |
| 103 | +Needs documentation... |
| 104 | + |
| 105 | +## Adding field validation |
| 106 | + |
| 107 | +Validation can be added to each GravityForm field type by generating a validationSchema Object, or enhancing an already existing SchemaFactory. |
| 108 | + |
| 109 | +Field validation for GravityForms occurs during the field mapping process in `<Fields />` component. Each field type is matched up with [Yup validation](https://github.com/jquense/yup#api) type. If a match is found it will be processed using a `SchemaFactory` which will return a field validationSchema Object. |
| 110 | + |
| 111 | +Note: Validation types cannot be mixed. |
| 112 | + |
| 113 | +### Updating existing validation types |
| 114 | + |
| 115 | +To update an existing validation type, locate the `SchemaFactory` for that field type. If there is not a match you'll need to create a new `SchemaFactories` for that validation type. |
| 116 | + |
| 117 | +_`SchemaFactories` are located in `functions/gravityForms/yupSchema`._ |
| 118 | + |
| 119 | +2. To edit an existing validation, locate the method defined in `SchemaFactory`. |
| 120 | +3. To add a new validation method, do the following: |
| 121 | +4. Add new validation method. |
| 122 | +5. Include the new method in `schema()` using `.concat(this.yourNewMethod)`. |
| 123 | + |
| 124 | +### New Validation Types |
| 125 | + |
| 126 | +To add a new type of validation do the following: |
| 127 | + |
| 128 | +1. Add a new case `getValidationSchemaByType()` for the field type. |
| 129 | +2. Clone an existing `SchemaFactory` and update it with new methods and references to Yup API ex: `Yup.string()` to `Yup.number()` etc... |
| 130 | + |
| 131 | +## Unsupported Fields |
| 132 | + |
| 133 | +Due to the amount of available GravityForm fields support will need to be added as needed. |
0 commit comments