* Converts internal field state to published field state
( formState: InternalFormState<FormValues>, field: InternalFieldState )
| 6 | * Converts internal field state to published field state |
| 7 | */ |
| 8 | function publishFieldState<FormValues = Record<string, any>>( |
| 9 | formState: InternalFormState<FormValues>, |
| 10 | field: InternalFieldState |
| 11 | ): FieldState { |
| 12 | const { |
| 13 | errors, |
| 14 | initialValues, |
| 15 | lastSubmittedValues, |
| 16 | submitErrors, |
| 17 | submitFailed, |
| 18 | submitSucceeded, |
| 19 | submitting, |
| 20 | values, |
| 21 | } = formState; |
| 22 | const { |
| 23 | active, |
| 24 | blur, |
| 25 | change, |
| 26 | data, |
| 27 | focus, |
| 28 | modified, |
| 29 | modifiedSinceLastSubmit, |
| 30 | name, |
| 31 | touched, |
| 32 | validating, |
| 33 | visited, |
| 34 | } = field; |
| 35 | const value = getIn(values as object, name); |
| 36 | let error = errors ? getIn(errors as object, name) : undefined; |
| 37 | if (error && error[ARRAY_ERROR]) { |
| 38 | error = error[ARRAY_ERROR]; |
| 39 | } |
| 40 | const submitError = submitErrors && getIn(submitErrors as object, name); |
| 41 | const initial = initialValues && getIn(initialValues, name); |
| 42 | const pristine = field.isEqual(initial, value); |
| 43 | const dirtySinceLastSubmit = !!( |
| 44 | lastSubmittedValues && |
| 45 | !field.isEqual(getIn(lastSubmittedValues, name), value) |
| 46 | ); |
| 47 | const valid = !error && !submitError; |
| 48 | return { |
| 49 | active, |
| 50 | blur, |
| 51 | change, |
| 52 | data, |
| 53 | dirty: !pristine, |
| 54 | dirtySinceLastSubmit, |
| 55 | error, |
| 56 | focus, |
| 57 | initial, |
| 58 | invalid: !valid, |
| 59 | length: Array.isArray(value) ? value.length : undefined, |
| 60 | modified, |
| 61 | modifiedSinceLastSubmit, |
| 62 | name, |
| 63 | pristine, |
| 64 | submitError, |
| 65 | submitFailed, |
searching dependent graphs…