impl BooleanField

This commit is contained in:
Yanzhen Yu 2021-11-15 15:39:09 +08:00
parent 94c697f22c
commit 79d5821080
3 changed files with 25 additions and 2 deletions

View File

@ -0,0 +1,15 @@
import React from 'react';
import { FieldProps } from './fields';
import { Switch } from '@chakra-ui/react';
type Props = FieldProps;
const BooleanField: React.FC<Props> = props => {
const { formData, onChange } = props;
return (
<Switch isChecked={formData} onChange={evt => onChange(evt.currentTarget.checked)} />
);
};
export default BooleanField;

View File

@ -6,10 +6,12 @@ import {
FormErrorMessage,
Text,
} from '@chakra-ui/react';
import { isEmpty } from 'lodash-es';
import { FieldProps, getDisplayLabel } from './fields';
import StringField from './StringField';
import ObjectField from './ObjectField';
import ArrayField from './ArrayField';
import BooleanField from './BooleanField';
import UnsupportedField from './UnsupportedField';
type TemplateProps = {
@ -61,6 +63,10 @@ type Props = FieldProps & {
const SchemaField: React.FC<Props> = props => {
const { schema, label, formData, onChange } = props;
if (isEmpty(schema)) {
return null;
}
let Component = UnsupportedField;
if (schema.type === 'object') {
@ -69,6 +75,8 @@ const SchemaField: React.FC<Props> = props => {
Component = StringField;
} else if (schema.type === 'array') {
Component = ArrayField;
} else if (schema.type === 'boolean') {
Component = BooleanField;
}
const displayLabel = getDisplayLabel(schema, label);

View File

@ -4,10 +4,10 @@ import { Input } from '@chakra-ui/react';
type Props = FieldProps;
const ObjectField: React.FC<Props> = props => {
const StringField: React.FC<Props> = props => {
const { formData, onChange } = props;
return <Input value={formData} onChange={evt => onChange(evt.currentTarget.value)} />;
};
export default ObjectField;
export default StringField;