import React from 'react';
import { UseFieldApiConfig } from '@data-driven-forms/react-form-renderer';
import useFieldApi from '@data-driven-forms/react-form-renderer/use-field-api';
import {
Checkbox as MagmaCheckbox,
CheckboxProps as MagmaCheckboxProps,
FormGroup,
FormGroupProps as MagmaFormGroupProps,
} from 'react-magma-dom';
import { v4 as uuidv4 } from 'uuid';
interface MagmaMultiCheckboxProps extends MagmaFormGroupProps {
options: MagmaCheckboxProps[];
}
type CheckboxProps = MagmaCheckboxProps & UseFieldApiConfig;
type MultiCheckboxProps = MagmaMultiCheckboxProps & UseFieldApiConfig;
const GroupedCheckbox = (props: CheckboxProps) => {
const { input } = useFieldApi({
name: props.name,
type: 'checkbox',
value: props.value,
});
return ;
};
const CheckboxMapping = (props: MultiCheckboxProps) => {
const {
input,
options,
validateOnMount,
showError,
meta: { error, submitFailed },
...rest
} = useFieldApi(props);
const name = input.name || uuidv4();
const errorMessage =
((validateOnMount || submitFailed || showError) && error) ||
rest.errorMessage;
if (options && options.length > 0) {
return (
{options.map((option: MagmaCheckboxProps) => {
return (
);
})}
);
} else {
return (
);
}
};
export const Checkbox = React.memo(CheckboxMapping);