@zohodesk/components
Version:
Dot UI is a customizable React component library built to deliver a clean, accessible, and developer-friendly UI experience. It offers a growing set of reusable components designed to align with modern design systems and streamline application development
33 lines (27 loc) • 673 B
JavaScript
import { useState } from "react";
const useSwitch = props => {
const {
isChecked: isCheckedProp,
isDefaultChecked,
onChange,
isReadOnly,
isDisabled
} = props;
const isControlled = isCheckedProp !== undefined;
const [internalChecked, setInternalChecked] = useState(isDefaultChecked);
const isChecked = isControlled ? isCheckedProp : internalChecked;
const handleChange = e => {
if (isReadOnly || isDisabled) return;
if (!isControlled) {
setInternalChecked(!isChecked);
}
onChange && onChange({
isChecked: !isChecked
}, e);
};
return {
isChecked,
handleChange
};
};
export default useSwitch;