| 138 | }; |
| 139 | |
| 140 | class FieldArrayInner<Values = {}> extends React.Component< |
| 141 | FieldArrayConfig & { formik: FormikContextType<Values> }, |
| 142 | {} |
| 143 | > { |
| 144 | static defaultProps = { |
| 145 | validateOnChange: true, |
| 146 | }; |
| 147 | |
| 148 | constructor(props: FieldArrayConfig & { formik: FormikContextType<Values> }) { |
| 149 | super(props); |
| 150 | // We need TypeScript generics on these, so we'll bind them in the constructor |
| 151 | // @todo Fix TS 3.2.1 |
| 152 | this.remove = this.remove.bind(this) as any; |
| 153 | this.pop = this.pop.bind(this) as any; |
| 154 | } |
| 155 | |
| 156 | componentDidUpdate( |
| 157 | prevProps: FieldArrayConfig & { formik: FormikContextType<Values> } |
| 158 | ) { |
| 159 | if ( |
| 160 | this.props.validateOnChange && |
| 161 | this.props.formik.validateOnChange && |
| 162 | !isEqual( |
| 163 | getIn(prevProps.formik.values, prevProps.name), |
| 164 | getIn(this.props.formik.values, this.props.name) |
| 165 | ) |
| 166 | ) { |
| 167 | this.props.formik.validateForm(this.props.formik.values); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | updateArrayField = ( |
| 172 | fn: Function, |
| 173 | alterTouched: boolean | Function, |
| 174 | alterErrors: boolean | Function |
| 175 | ) => { |
| 176 | const { |
| 177 | name, |
| 178 | |
| 179 | formik: { setFormikState }, |
| 180 | } = this.props; |
| 181 | |
| 182 | setFormikState((prevState: FormikState<any>) => { |
| 183 | let updateErrors = createAlterationHandler(alterErrors, fn); |
| 184 | let updateTouched = createAlterationHandler(alterTouched, fn); |
| 185 | |
| 186 | // values fn should be executed before updateErrors and updateTouched, |
| 187 | // otherwise it causes an error with unshift. |
| 188 | let values = setIn( |
| 189 | prevState.values, |
| 190 | name, |
| 191 | fn(getIn(prevState.values, name)) |
| 192 | ); |
| 193 | |
| 194 | let fieldError = alterErrors |
| 195 | ? updateErrors(getIn(prevState.errors, name)) |
| 196 | : undefined; |
| 197 | let fieldTouched = alterTouched |
nothing calls this directly
no test coverage detected
searching dependent graphs…