UNPKG

@gravity-ui/uikit

Version:

Gravity UI base styling and components

41 lines (40 loc) 1.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useRadioGroup = useRadioGroup; const __1 = require("../../index.js"); const filterDOMProps_1 = require("../../../components/utils/filterDOMProps.js"); const useFormResetHandler_1 = require("../useFormResetHandler/index.js"); function useRadioGroup(props) { const { name, value, defaultValue, options = [], disabled, onUpdate, onChange, onFocus, onBlur, } = props; const controlId = (0, __1.useUniqId)(); const [currentValue, setValueState] = (0, __1.useControlledState)(value, defaultValue ?? null, onUpdate); const fieldRef = (0, useFormResetHandler_1.useFormResetHandler)({ initialValue: currentValue, onReset: setValueState, }); const { focusWithinProps } = (0, __1.useFocusWithin)({ onFocusWithin: onFocus, onBlurWithin: onBlur }); const handleChange = (event) => { setValueState(event.target.value); if (onChange) { onChange(event); } }; const containerProps = { ...(0, filterDOMProps_1.filterDOMProps)(props, { labelable: true }), ...focusWithinProps, role: 'radiogroup', 'aria-disabled': disabled, }; const optionsProps = options.map((option) => ({ name: name || controlId, value: option.value, content: option.content, title: option.title, checked: currentValue === String(option.value), disabled: disabled || option.disabled, onChange: handleChange, ref: fieldRef, })); return { containerProps, optionsProps }; } //# sourceMappingURL=useRadioGroup.js.map