import type { JSONSchema7, JSONSchema7Type } from 'json-schema'; import { useContext } from 'react'; import ControlsContext from './ControlsContext'; const errstyle = { background: 'red', color: 'white' }; function getControlName(schema: JSONSchema7) { if ('uw:control' in schema && typeof schema['uw:control'] === 'string') { return schema['uw:control']; } if (Array.isArray(schema.enum)) { return 'enum'; } // Not sure what to do with this! if (Array.isArray(schema.type)) { return undefined; } return schema.type; } type FieldProps = { className?: string, schema: JSONSchema7, value: JSONSchema7Type, onChange: (value: JSONSchema7Type) => void, }; function Field({ className, schema, value, onChange, }: FieldProps) { const controls = useContext(ControlsContext); const controlName = getControlName(schema); if (controlName == null) { return
Unsupported type
; } const Control = controls.get(controlName); if (Control) { return ( // This is actually a static component. // This could (should?) be rewritten as a switch statement. // eslint-disable-next-line react-hooks/static-componentsUnknown: {controlName}
); } export default Field;