| 20 | } |
| 21 | |
| 22 | class FieldsStore { |
| 23 | constructor(fields) { |
| 24 | this.fields = internalFlattenFields(fields); |
| 25 | this.fieldsMeta = {}; |
| 26 | } |
| 27 | |
| 28 | updateFields(fields) { |
| 29 | this.fields = internalFlattenFields(fields); |
| 30 | } |
| 31 | |
| 32 | flattenRegisteredFields(fields) { |
| 33 | const validFieldsName = this.getAllFieldsName(); |
| 34 | return flattenFields( |
| 35 | fields, |
| 36 | path => validFieldsName.indexOf(path) >= 0, |
| 37 | 'You cannot set a form field before rendering a field associated with the value.' |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | setFieldsInitialValue = (initialValues) => { |
| 42 | const flattenedInitialValues = this.flattenRegisteredFields(initialValues); |
| 43 | const fieldsMeta = this.fieldsMeta; |
| 44 | Object.keys(flattenedInitialValues).forEach(name => { |
| 45 | if (fieldsMeta[name]) { |
| 46 | this.setFieldMeta(name, { |
| 47 | ...this.getFieldMeta(name), |
| 48 | initialValue: flattenedInitialValues[name], |
| 49 | }); |
| 50 | } |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | setFields(fields) { |
| 55 | const fieldsMeta = this.fieldsMeta; |
| 56 | const nowFields = { |
| 57 | ...this.fields, |
| 58 | ...fields, |
| 59 | }; |
| 60 | const nowValues = {}; |
| 61 | Object.keys(fieldsMeta) |
| 62 | .forEach((f) => { |
| 63 | nowValues[f] = this.getValueFromFields(f, nowFields); |
| 64 | }); |
| 65 | Object.keys(nowValues).forEach((f) => { |
| 66 | const value = nowValues[f]; |
| 67 | const fieldMeta = this.getFieldMeta(f); |
| 68 | if (fieldMeta && fieldMeta.normalize) { |
| 69 | const nowValue = |
| 70 | fieldMeta.normalize(value, this.getValueFromFields(f, this.fields), nowValues); |
| 71 | if (nowValue !== value) { |
| 72 | nowFields[f] = { |
| 73 | ...nowFields[f], |
| 74 | value: nowValue, |
| 75 | }; |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | this.fields = nowFields; |
nothing calls this directly
no test coverage detected