Pass "raw" component constructor down to FormComponentInner instead of passing function that instantiates it

This commit is contained in:
SachaG 2018-05-23 17:12:04 +09:00
parent 2edebcf551
commit 2b05700e41
2 changed files with 24 additions and 30 deletions

View file

@ -235,66 +235,59 @@ class FormComponent extends Component {
Function passed to FormComponentInner to help with rendering the component Function passed to FormComponentInner to help with rendering the component
*/ */
renderComponent = properties => { getFormInput = () => {
const { input, inputType } = properties; const inputType = this.getType();
// if input is a React component, use it // if input is a React component, use it
if (typeof input === 'function') { if (typeof this.props.input === 'function') {
const InputComponent = input; const InputComponent = this.props.input;
return <InputComponent {...properties} />; return InputComponent;
} else { } else {
// else pick a predefined component // else pick a predefined component
switch (inputType) { switch (inputType) {
case 'text': case 'text':
return <Components.FormComponentDefault {...properties} />; return Components.FormComponentDefault;
case 'nested':
return <Components.FormNested {...properties} />;
case 'number': case 'number':
return <Components.FormComponentNumber {...properties} />; return Components.FormComponentNumber;
case 'url': case 'url':
return <Components.FormComponentUrl {...properties} />; return Components.FormComponentUrl;
case 'email': case 'email':
return <Components.FormComponentEmail {...properties} />; return Components.FormComponentEmail;
case 'textarea': case 'textarea':
return <Components.FormComponentTextarea {...properties} />; return Components.FormComponentTextarea;
case 'checkbox': case 'checkbox':
return <Components.FormComponentCheckbox {...properties} />; return Components.FormComponentCheckbox;
case 'checkboxgroup': case 'checkboxgroup':
return <Components.FormComponentCheckboxGroup {...properties} />; return Components.FormComponentCheckboxGroup;
case 'radiogroup': case 'radiogroup':
return <Components.FormComponentRadioGroup {...properties} />; return Components.FormComponentRadioGroup;
case 'select': case 'select':
return <Components.FormComponentSelect {...properties} />; return Components.FormComponentSelect;
case 'selectmultiple': case 'selectmultiple':
return <Components.FormComponentSelectMultiple {...properties} />; return Components.FormComponentSelectMultiple;
case 'datetime': case 'datetime':
return <Components.FormComponentDateTime {...properties} />; return Components.FormComponentDateTime;
case 'date': case 'date':
return <Components.FormComponentDate {...properties} />; return Components.FormComponentDate;
case 'time': case 'time':
return <Components.FormComponentTime {...properties} />; return Components.FormComponentTime;
default: default:
const CustomComponent = Components[input]; const CustomComponent = Components[this.props.input];
return CustomComponent ? ( return CustomComponent ? CustomComponent : Components.FormComponentDefault;
<CustomComponent {...properties} />
) : (
<Components.FormComponentDefault {...properties} />
);
} }
} }
}; };
@ -311,7 +304,7 @@ class FormComponent extends Component {
showCharsRemaining={!!this.showCharsRemaining()} showCharsRemaining={!!this.showCharsRemaining()}
onChange={this.handleChange} onChange={this.handleChange}
clearField={this.clearField} clearField={this.clearField}
renderComponent={this.renderComponent} formInput={this.getFormInput()}
/> />
); );
} }

View file

@ -66,13 +66,14 @@ class FormComponentInner extends PureComponent {
); );
const properties = this.getProperties(); const properties = this.getProperties();
const FormInput = this.props.formInput;
if (intlInput) { if (intlInput) {
return <Components.FormIntl {...properties} />; return <Components.FormIntl {...properties} />;
} else { } else {
return ( return (
<div className={inputClass}> <div className={inputClass}>
{instantiateComponent(beforeComponent, properties)} {instantiateComponent(beforeComponent, properties)}
{renderComponent(properties)} <FormInput {...properties}/>
{hasErrors ? <Components.FieldErrors errors={errors} /> : null} {hasErrors ? <Components.FieldErrors errors={errors} /> : null}
{this.renderClear()} {this.renderClear()}
{showCharsRemaining && ( {showCharsRemaining && (
@ -99,7 +100,7 @@ FormComponentInner.propTypes = {
charsRemaining: PropTypes.number, charsRemaining: PropTypes.number,
charsCount: PropTypes.number, charsCount: PropTypes.number,
charsMax: PropTypes.number, charsMax: PropTypes.number,
renderComponent: PropTypes.func.isRequired, inputComponent: PropTypes.func,
}; };
FormComponentInner.contextTypes = { FormComponentInner.contextTypes = {