@geneui/components
Version:
The Gene UI components library designed for BI tools
127 lines (120 loc) • 3.71 kB
JavaScript
import { _ as _extends } from '../_rollupPluginBabelHelpers-e8fb2e5c.js';
import React__default, { forwardRef, useState, useCallback, useEffect } from 'react';
import PropTypes from 'prop-types';
import { n as noop, i as interceptValue } from '../index-a0e4e333.js';
import useMount from '../hooks/useMount.js';
import '../configs-00612ce0.js';
import Checkbox from '../Checkbox/index.js';
import '../dateValidation-67caec66.js';
import '../_commonjsHelpers-24198af3.js';
import 'react-dom';
import '../index-031ff73c.js';
import '../index-6d7e99cd.js';
import '../tslib.es6-f211516f.js';
import '../hooks/useDeviceType.js';
import '../hooks/useWindowSize.js';
import '../hooks/useDebounce.js';
import '../GeneUIProvider/index.js';
import '../style-inject.es-746bb8ed.js';
import '../checkboxRadioSwitcher-5b69d7bd.js';
import '../guid-8ddf77b3.js';
function checkValidation(required, isValid, checked) {
if (isValid === false) return {
key: 'customValidation',
isValid: false
};
if (required && !checked) return {
key: 'required',
isValid: false
};
return {
key: null,
isValid: true
};
}
const CheckboxField = /*#__PURE__*/forwardRef((props, ref) => {
const {
onChange,
value,
isValid,
required,
isFieldValid,
forceAllowValidation,
...restProps
} = props;
const isControlled = 'value' in props && typeof value !== 'undefined';
const [validationState, setValidationState] = useState(true);
const [checked, setChecked] = useState(false);
const [allowValidation, setAllowValidation] = useState(false);
const validate = useCallback(() => checkValidation(required, isValid, checked).isValid, [checked, required, isValid]);
const handleChange = useCallback(e => {
const {
checked
} = e.target;
const {
isValid: isCheckboxValid,
key
} = checkValidation(required, isValid, checked);
setValidationState(isCheckboxValid);
setChecked(checked);
onChange(interceptValue(e, value), isCheckboxValid, key);
}, [onChange, required, isValid]);
// we use this because need to show field validation after onBlur
const onBlur = useCallback(() => setAllowValidation(true), []);
useEffect(() => {
isControlled && setValidationState(validate());
}, [isControlled, validate]);
// need this for handling user's `isValid` prop
useEffect(() => {
setValidationState(isValid);
}, [isValid]);
// call function when validation state changes
useEffect(() => {
isFieldValid(validationState);
}, [validationState]);
// set Allow validation true if submit button clicked
useEffect(() => {
forceAllowValidation && setAllowValidation(true);
}, [forceAllowValidation]);
useMount(() => setValidationState(validate()));
return /*#__PURE__*/React__default.createElement(Checkbox, _extends({
required: required,
ref: ref,
value: value,
isValid: !allowValidation || validationState,
onChange: handleChange,
onBlur: onBlur
}, restProps));
});
CheckboxField.propTypes = {
/**
* Value for checkbox
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
/**
* Callback fires when checkbox changes
*/
onChange: PropTypes.func,
/**
* Define is field required or no.
*/
required: PropTypes.bool,
/**
* Callback fires when field validation state changes
*/
isFieldValid: PropTypes.func,
/**
* Additional validation state
*/
isValid: PropTypes.bool,
/**
* Allow validation without onBlur, validate field when mount
*/
forceAllowValidation: PropTypes.bool
};
CheckboxField.defaultProps = {
isValid: true,
isFieldValid: noop,
onChange: noop
};
export { CheckboxField as default };