UNPKG

@zohodesk/dot

Version:

In this Library, we Provide Some Basic Components to Build Your Application

50 lines (42 loc) 1.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = useRadio; var _react = require("react"); function useRadio(_ref) { var id = _ref.id, onChange = _ref.onChange, getRef = _ref.getRef, options = _ref.options, selectedValue = _ref.selectedValue; var handleChange = function handleChange(value) { onChange && onChange(id, value); }; var radioRef = (0, _react.useRef)({ radios: {}, focus: function focus() {} }).current; var firstRadioValue = options[0].value; function handleGetRef(ele, val) { radioRef.radios[val] = ele; } function handleFocus() { if (!!selectedValue) { radioRef.radios[selectedValue].focus(); } else { radioRef.radios[firstRadioValue].focus(); } } (0, _react.useEffect)(function () { radioRef.focus = handleFocus; getRef && getRef(radioRef, id); return function () { getRef && getRef(null, id); }; }, [getRef]); return { handleGetRef: handleGetRef, handleChange: handleChange }; }