UNPKG

@zohodesk/dot

Version:

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

44 lines (39 loc) 833 B
import { useRef, useEffect } from 'react'; export default function useRadio(_ref) { let { id, onChange, getRef, options, selectedValue } = _ref; const handleChange = value => { onChange && onChange(id, value); }; const radioRef = useRef({ radios: {}, focus: () => {} }).current; const 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(); } } useEffect(() => { radioRef.focus = handleFocus; getRef && getRef(radioRef, id); return () => { getRef && getRef(null, id); }; }, [getRef]); return { handleGetRef, handleChange }; }