Skip to content

Instantly share code, notes, and snippets.

@deyceg
Created July 29, 2016 08:19
Show Gist options
  • Save deyceg/1bae478ee4bf0b912d31b5b373de9274 to your computer and use it in GitHub Desktop.
Save deyceg/1bae478ee4bf0b912d31b5b373de9274 to your computer and use it in GitHub Desktop.
// in constructor iterate over FormWrapper instance this.props.children and decorate "*Field" with <Field>
decorateFields = (element) => {
const { props: { children } = {} } = element;
const { type: { name } = {} } = element;
if (typeof element === 'string' || typeof element.type === 'string') return element;
//Editable has prop component which is <InlineSelectField> that never gets decorated
if (name && name.endsWith('Field')) { // TODO not minficiation safe!!!
return this.renderField(element)
} else if (children) {
return React.cloneElement(element.type, element.props, React.Children.map(children, e => this.decorateFields(e)))
} else {
return React.cloneElement(element.type, element.props);
}
};
<FormWrapper form={id} postValidate={postValidate} validate={onValidate} onSubmit={onSubmit} hasError={hasError}>
<InlineTextField name="applicantOneName"/>
<Editable name="numberOfApplicants" options={numberOfApplicants} component={<InlineSelectField />} />
</FormWrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment