@chayns-components/core
Version:
A set of beautiful React components for developing your own applications with chayns.
83 lines (82 loc) • 4.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _useKeyboardFocusHighlighting = require("../../hooks/useKeyboardFocusHighlighting");
var _uuid = require("../../hooks/uuid");
var _calculate = require("../../utils/calculate");
var _Checkbox = require("./Checkbox.styles");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const Checkbox = ({
children,
isChecked,
isDisabled,
labelClassName,
onChange,
shouldShowAsSwitch,
borderRadius,
colors,
shouldShowCentered = false,
shouldChangeOnLabelClick = true,
shouldEnableKeyboardHighlighting
}) => {
const [isActive, setIsActive] = (0, _react.useState)(isChecked ?? false);
const [checkboxTop, setCheckboxTop] = (0, _react.useState)(undefined);
const checkboxBoxRef = (0, _react.useRef)(null);
const checkboxRootRef = (0, _react.useRef)(null);
const handleChange = (0, _react.useCallback)(event => {
setIsActive(event.target.checked);
if (typeof onChange === 'function') {
onChange(event);
}
}, [onChange]);
const uuid = (0, _uuid.useUuid)();
(0, _react.useEffect)(() => {
if (checkboxRootRef.current && !shouldShowCentered) {
var _checkboxBoxRef$curre;
const singleLineHeight = (0, _calculate.getHeightOfSingleTextLine)({
container: checkboxRootRef.current
});
const boxHeight = ((_checkboxBoxRef$curre = checkboxBoxRef.current) === null || _checkboxBoxRef$curre === void 0 ? void 0 : _checkboxBoxRef$curre.getBoundingClientRect().height) ?? 0;
setCheckboxTop((singleLineHeight - boxHeight) / 2);
}
}, [shouldShowCentered]);
const shouldShowKeyboardHighlighting = (0, _useKeyboardFocusHighlighting.useKeyboardFocusHighlighting)(shouldEnableKeyboardHighlighting && !isDisabled);
return /*#__PURE__*/_react.default.createElement(_Checkbox.StyledCheckbox, {
ref: checkboxRootRef
}, /*#__PURE__*/_react.default.createElement(_Checkbox.StyledCheckboxInput, {
$shouldShowKeyboardHighlighting: shouldShowKeyboardHighlighting,
checked: isChecked,
disabled: isDisabled,
id: uuid,
onChange: handleChange,
type: "checkbox"
}), /*#__PURE__*/_react.default.createElement(_Checkbox.StyledCheckboxBoxWrapper, {
$shouldShowAsSwitch: shouldShowAsSwitch,
style: {
top: shouldShowCentered ? '50%' : checkboxTop,
transform: shouldShowCentered ? 'translateY(-50%)' : undefined
}
}, /*#__PURE__*/_react.default.createElement(_Checkbox.StyledCheckboxBox, {
htmlFor: uuid,
ref: checkboxBoxRef,
$isChecked: isChecked ?? isActive,
$isDisabled: isDisabled,
$shouldShowAsSwitch: shouldShowAsSwitch,
$checkedBackgroundColor: colors === null || colors === void 0 ? void 0 : colors.checkedBackgroundColor,
$uncheckedBackgroundColor: colors === null || colors === void 0 ? void 0 : colors.uncheckedBackgroundColor,
$borderRadius: borderRadius,
$borderColor: colors === null || colors === void 0 ? void 0 : colors.borderColor
})), /*#__PURE__*/_react.default.createElement(_Checkbox.StyledCheckboxLabel, {
className: labelClassName,
$isDisabled: isDisabled,
$shouldChangeOnLabelClick: shouldChangeOnLabelClick,
$shouldShowAsSwitch: shouldShowAsSwitch,
htmlFor: shouldChangeOnLabelClick ? uuid : undefined
}, children));
};
Checkbox.displayName = 'Checkbox';
var _default = exports.default = Checkbox;
//# sourceMappingURL=Checkbox.js.map