import * as React from 'react'; import get from 'lodash/get'; import {memoize, bind} from 'lodash-decorators'; import {FieldDescriptor, FieldDescriptors} from '../types'; import {mapObject} from '../utilities'; interface Props { field: FieldDescriptor; children(fields: FieldDescriptors): React.ReactNode; } export default class Nested extends React.PureComponent< Props, never > { render() { const { field: {name, value, onBlur, initialValue, error}, children, } = this.props; const innerFields: FieldDescriptors = mapObject( value, (value, fieldPath) => { const initialFieldValue = initialValue[fieldPath]; return { value, onBlur, name: `${name}.${fieldPath}`, initialValue: initialFieldValue, dirty: value !== initialFieldValue, error: get(error, fieldPath), onChange: this.handleChange(fieldPath), }; }, ); return children(innerFields); } @memoize() @bind() private handleChange(key: Key) { return (newValue: Fields[Key]) => { const { field: {value: existingItem, onChange}, } = this.props; const newItem = { ...(existingItem as any), [key]: newValue, }; onChange(newItem); }; } }