UNPKG

@geneui/components

Version:

The Gene UI components library designed for BI tools

117 lines (110 loc) 3.4 kB
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 } from '../index-a0e4e333.js'; import useMount from '../hooks/useMount.js'; import '../configs-00612ce0.js'; import RadioGroup from '../RadioGroup/index.js'; import '../dateValidation-67caec66.js'; import '../_commonjsHelpers-24198af3.js'; import 'react-dom'; import '../index-031ff73c.js'; import '../Radio/index.js'; import '../checkboxRadioSwitcher-5b69d7bd.js'; import '../style-inject.es-746bb8ed.js'; import '../guid-8ddf77b3.js'; function checkValidation(required, isValid) { let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; if (isValid === false) return { key: 'customValidation', isValid: false }; if (required && (!value || value.length === 0)) return { key: 'required', isValid: false }; return { key: null, isValid: true }; } const Radio = /*#__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 [allowValidation, setAllowValidation] = useState(false); const validate = useCallback(() => checkValidation(required, isValid, value).isValid, [value, required, isValid]); const handleChange = useCallback(e => { const { value } = e.target; const validation = checkValidation(required, isValid, value); setValidationState(validation.isValid); onChange(e, validation.isValid, validation.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(RadioGroup, _extends({ ref: ref, value: value, isValid: !allowValidation || validationState, onChange: handleChange, onBlur: onBlur }, restProps)); }); Radio.propTypes = { /** * Value for radio field */ value: PropTypes.string, /** * Callback fires when radio field 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 }; Radio.defaultProps = { isValid: true, isFieldValid: noop, onChange: noop }; export { Radio as default };