|
| 1 | +import React from 'react'; |
| 2 | +import Form from 'rc-field-form'; |
| 3 | +import Input from './components/Input'; |
| 4 | +import { isEqual } from 'lodash'; |
| 5 | + |
| 6 | +const ChildrenContent = (props: { name: number }) => { |
| 7 | + |
| 8 | + const { name } = props; |
| 9 | + |
| 10 | + const scopedForm = Form.useFormInstance({ scoped: true }); |
| 11 | + const college = Form.useWatch([name, 'college'], scopedForm); |
| 12 | + const location = Form.useWatch([name, 'location'], { scoped: true }); |
| 13 | + const [, forceUpdate] = React.useState({}); |
| 14 | + |
| 15 | + React.useEffect(() => { |
| 16 | + scopedForm.setFieldValue([name, 'nonexistent'], 'nonexistent'); |
| 17 | + }, [scopedForm, name]); |
| 18 | + |
| 19 | + return ( |
| 20 | + <div style={{ marginBottom: 16 }}> |
| 21 | + <div> |
| 22 | + <Form.Field |
| 23 | + name={[name, 'college']} |
| 24 | + rules={[ |
| 25 | + { |
| 26 | + required: true, |
| 27 | + message: 'college is required', |
| 28 | + }, |
| 29 | + ]} |
| 30 | + > |
| 31 | + <Input placeholder="College" /> |
| 32 | + </Form.Field> |
| 33 | + <span>{college}</span> |
| 34 | + </div> |
| 35 | + <div> |
| 36 | + <Form.Field |
| 37 | + name={[name, 'location']} |
| 38 | + rules={[ |
| 39 | + { required: true, message: 'location is required' }, |
| 40 | + ]} |
| 41 | + > |
| 42 | + <Input placeholder="Location" /> |
| 43 | + </Form.Field> |
| 44 | + <span>{location}</span> |
| 45 | + </div> |
| 46 | + <div> |
| 47 | + <Form.Field |
| 48 | + name={[name, 'field0']} |
| 49 | + valuePropName="checked" |
| 50 | + > |
| 51 | + <input type="checkbox" /> |
| 52 | + </Form.Field> |
| 53 | + Checked |
| 54 | + </div> |
| 55 | + <div> |
| 56 | + <Form.Field |
| 57 | + shouldUpdate |
| 58 | + > |
| 59 | + { |
| 60 | + () => { |
| 61 | + if (scopedForm.getFieldValue([name, 'field0'])) { |
| 62 | + return ( |
| 63 | + <Form.Field |
| 64 | + name={[name, 'field1']} |
| 65 | + > |
| 66 | + <input type="text" /> |
| 67 | + </Form.Field> |
| 68 | + ); |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | + } |
| 73 | + </Form.Field> |
| 74 | + </div> |
| 75 | + <div> |
| 76 | + <button onClick={() => forceUpdate({})}>forceUpdate</button> |
| 77 | + </div> |
| 78 | + <div> |
| 79 | + <span>{`scopedForm.getFieldsValue({strict: true }):`}</span> |
| 80 | + <span>{`${JSON.stringify(scopedForm.getFieldsValue({ strict: true }))}`}</span> |
| 81 | + </div> |
| 82 | + <div> |
| 83 | + <span>scopedForm.getFieldsValue():</span> |
| 84 | + <span>{`${JSON.stringify(scopedForm.getFieldsValue())}`}</span> |
| 85 | + </div> |
| 86 | + <div> |
| 87 | + <span>{`scopedForm.getFieldValue([name, 'location']):`}</span> |
| 88 | + <span>{`${JSON.stringify(scopedForm.getFieldValue([name, 'location']))}`}</span> |
| 89 | + </div> |
| 90 | + <div> |
| 91 | + <span>{`scopedForm.getFieldValue([name, 'nonexistent']):`}</span> |
| 92 | + <span>{`${JSON.stringify(scopedForm.getFieldValue([name, 'nonexistent']))}`}</span> |
| 93 | + </div> |
| 94 | + <div> |
| 95 | + <span>{`scopedForm.getFieldsValue({ strict: true, filter: meta => isEqual(meta.name, [name, 'location']) }):`}</span> |
| 96 | + <span>{`${JSON.stringify(scopedForm.getFieldsValue({ strict: true, filter: meta => isEqual(meta.name, [name, 'location']) }))}`}</span> |
| 97 | + </div> |
| 98 | + <div> |
| 99 | + <span>{`scopedForm.getFieldsValue(true, meta => isEqual(meta.name, [name, 'location'])):`}</span> |
| 100 | + <span>{`${JSON.stringify(scopedForm.getFieldsValue(true, meta => isEqual(meta.name, [name, 'location'])))}`}</span> |
| 101 | + </div> |
| 102 | + <div> |
| 103 | + <span>{`scopedForm.isFieldsTouched(true):`}</span> |
| 104 | + <span>{`${JSON.stringify(scopedForm.isFieldsTouched(true))}`}</span> |
| 105 | + </div> |
| 106 | + <div> |
| 107 | + <span>{`scopedForm.isFieldsTouched():`}</span> |
| 108 | + <span>{`${JSON.stringify(scopedForm.isFieldsTouched())}`}</span> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +export default () => { |
| 115 | + const [form] = Form.useForm(); |
| 116 | + console.log('rootForm', form); |
| 117 | + |
| 118 | + return ( |
| 119 | + <div> |
| 120 | + <Form |
| 121 | + form={form} |
| 122 | + initialValues={{ |
| 123 | + educations: [ |
| 124 | + { |
| 125 | + college: 'Ant Design', |
| 126 | + }, |
| 127 | + ], |
| 128 | + }} |
| 129 | + > |
| 130 | + <> |
| 131 | + <Form.Field name="name"> |
| 132 | + <Input placeholder="Name" /> |
| 133 | + </Form.Field> |
| 134 | + <Form.Field name="age"> |
| 135 | + <Input placeholder="Age" /> |
| 136 | + </Form.Field> |
| 137 | + <Form.List |
| 138 | + name="educations" |
| 139 | + > |
| 140 | + { |
| 141 | + (fields, { add }) => ( |
| 142 | + <div style={{ paddingLeft: 16 }}> |
| 143 | + <h2 style={{ marginBottom: 8 }}>Colleges</h2> |
| 144 | + { |
| 145 | + fields.map(field => { |
| 146 | + return ( |
| 147 | + <ChildrenContent key={field.key} name={field.name} /> |
| 148 | + ); |
| 149 | + }) |
| 150 | + } |
| 151 | + <button |
| 152 | + onClick={() => add()} |
| 153 | + >Add education</button> |
| 154 | + </div> |
| 155 | + ) |
| 156 | + } |
| 157 | + </Form.List> |
| 158 | + </> |
| 159 | + </Form> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +}; |
0 commit comments