Skip to content

Instantly share code, notes, and snippets.

@Santheepkumar
Created September 17, 2022 15:56
Show Gist options
  • Select an option

  • Save Santheepkumar/fb7ea60be2e85d8247cc06ba8b1813e2 to your computer and use it in GitHub Desktop.

Select an option

Save Santheepkumar/fb7ea60be2e85d8247cc06ba8b1813e2 to your computer and use it in GitHub Desktop.
Proudof.jsx
import {
CheckboxInput,
Creatable,
MultiSelectInput,
NumberInput,
RadioInput,
SelectInput,
TextAreaInput,
TextInput,
} from "../widgets";
const fieldComponentMapper = {
text: TextInput,
number: NumberInput,
password: TextInput,
textarea: TextAreaInput,
select: SelectInput,
multiselect: MultiSelectInput,
checkbox: CheckboxInput,
radio: RadioInput,
creatablemultiselect: Creatable,
};
function GetComponentForConfig({ fieldConfig, values }) {
const FieldComponent = fieldComponentMapper[fieldConfig.type];
const hConfig = fieldConfig.show || [];
if (FieldComponent) {
return (
<>
<FieldComponent key={fieldConfig.id} {...fieldConfig} />
{/* Dependent Fields */}
{hConfig.map((hFieldConfig) => {
if (fieldConfig?.if === values?.[fieldConfig.name]) {
return (
<GetComponentForConfig
key={hFieldConfig.id}
fieldConfig={hFieldConfig}
values={values}
/>
);
}
})}
</>
);
}
return <div>Unknown config</div>;
}
function FieldBuilder({ config, values }) {
return (
<>
{config.map((fieldConfig) => (
<GetComponentForConfig
fieldConfig={fieldConfig}
key={fieldConfig.id}
values={values}
/>
))}
</>
);
}
export default FieldBuilder;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment