@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
215 lines (214 loc) • 7.41 kB
JavaScript
"use client";
var _AlignmentHelper, _span, _span2;
import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js";
import React, { useCallback, useContext, useEffect, useReducer, useRef } from 'react';
import clsx from 'clsx';
import { validateDOMAttributes, getStatusState, combineDescribedBy, extendPropsWithContext } from "../../shared/component-helper.js";
import AlignmentHelper from "../../shared/AlignmentHelper.js";
import { applySpacing } from "../space/SpacingUtils.js";
import { skeletonDOMAttributes, createSkeletonClass } from "../skeleton/SkeletonHelper.js";
import Context from "../../shared/Context.js";
import Suffix from "../../shared/helpers/Suffix.js";
import useId from "../../shared/helpers/useId.js";
import { pickFormElementProps } from "../../shared/helpers/filterValidProps.js";
import FormLabel from "../form-label/FormLabel.js";
import FormStatus from "../form-status/FormStatus.js";
import CheckIcon from "./CheckIcon.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const defaultProps = {
statusState: 'error'
};
function Checkbox(localProps) {
const context = useContext(Context);
const extractPropsFromContext = useCallback(() => {
return extendPropsWithContext(localProps, defaultProps, context.Checkbox, {
skeleton: context === null || context === void 0 ? void 0 : context.Checkbox
}, pickFormElementProps(context === null || context === void 0 ? void 0 : context.formElement));
}, [context.Checkbox, context === null || context === void 0 ? void 0 : context.formElement, localProps]);
const props = extractPropsFromContext();
const {
value,
status,
statusState,
statusProps,
statusNoAnimation,
globalStatus,
suffix,
size,
label,
labelPosition,
labelSrOnly,
title,
element,
disabled,
readOnly,
skeleton,
className,
id: idProp,
indeterminate,
checked,
onChange,
onClick,
ref: refProp,
...rest
} = props;
const [, forceUpdate] = useReducer(() => ({}), {});
const id = useId(idProp);
const isFn = typeof refProp === 'function';
const refHook = useRef(undefined);
const ref = !isFn && refProp || refHook;
useEffect(() => {
if (isFn) {
refProp === null || refProp === void 0 || refProp(ref.current);
}
}, [refProp, isFn, ref]);
const preventChangeRef = useRef(false);
const isCheckedRef = useRef(checked !== null && checked !== void 0 ? checked : false);
const prevCheckedRef = useRef(checked);
useEffect(() => {
if (checked !== prevCheckedRef.current) {
isCheckedRef.current = !!checked;
prevCheckedRef.current = !!checked;
forceUpdate();
}
}, [checked]);
useEffect(() => {
ref.current.indeterminate = indeterminate;
}, [indeterminate, ref]);
const callOnChange = useCallback(args => {
onChange === null || onChange === void 0 || onChange(args);
}, [onChange]);
const handleChange = useCallback(event => {
if (preventChangeRef.current) {
return;
}
preventChangeRef.current = false;
const updatedCheck = !isCheckedRef.current;
isCheckedRef.current = updatedCheck;
forceUpdate();
callOnChange({
checked: updatedCheck,
event
});
if (ref.current) {
ref.current.focus({
preventScroll: true
});
}
}, [callOnChange, ref]);
const onChangeHandler = useCallback(event => {
handleChange(event);
}, [handleChange]);
const onClickHandler = useCallback(event => {
const preventDefault = () => {
event.preventDefault();
if (event.target['checked'] !== isCheckedRef.current) {
preventChangeRef.current = true;
isCheckedRef.current = !isCheckedRef.current;
forceUpdate();
}
};
if (readOnly) {
return preventDefault();
}
onClick === null || onClick === void 0 || onClick({
checked: isCheckedRef.current,
preventDefault,
event,
...event
});
}, [onClick, readOnly]);
const onKeyDownHandler = useCallback(event => {
if (event.key === 'Enter') {
handleChange(event);
}
}, [handleChange]);
const showStatus = getStatusState(status);
const handleInputAttributes = useCallback(() => {
const inputParams = {
disabled,
checked: isCheckedRef.current,
readOnly,
...rest
};
if (showStatus || suffix) {
inputParams['aria-describedby'] = combineDescribedBy(inputParams, showStatus ? id + '-status' : null, suffix ? id + '-suffix' : null);
}
if (readOnly) {
inputParams['aria-readonly'] = inputParams.readOnly = true;
}
return validateDOMAttributes(props, skeletonDOMAttributes(inputParams, skeleton, context));
}, [context, disabled, id, props, readOnly, rest, showStatus, skeleton, suffix]);
const mainParams = applySpacing(props, {
className: clsx("dnb-checkbox dnb-form-component", createSkeletonClass(null, skeleton, context), className, status && `dnb-checkbox__status--${statusState}`, size && `dnb-checkbox--${size}`, label && `dnb-checkbox--label-position-${labelPosition || 'right'}`)
});
const inputParams = handleInputAttributes();
const statusComp = _jsx(FormStatus, {
show: showStatus,
id: id + '-form-status',
globalStatus: globalStatus,
label: label,
textId: id + '-status',
widthSelector: id + ', ' + id + '-label',
text: status,
state: statusState,
noAnimation: statusNoAnimation,
skeleton: skeleton,
...statusProps
});
const Element = element || 'input';
return _jsxs("span", {
...mainParams,
children: [_jsxs("span", {
className: "dnb-checkbox__order",
children: [label && _jsx(FormLabel, {
id: id + '-label',
forId: id,
text: label,
disabled: disabled,
skeleton: skeleton,
srOnly: labelSrOnly,
vertical: false
}), _jsxs("span", {
className: "dnb-checkbox__inner",
children: [_AlignmentHelper || (_AlignmentHelper = _jsx(AlignmentHelper, {})), labelPosition === 'left' && statusComp, _jsxs("span", {
className: "dnb-checkbox__shell",
children: [_jsx(Element, {
id: id,
name: id,
type: "checkbox",
title: title,
className: "dnb-checkbox__input",
value: isCheckedRef.current ? value || '' : '',
disabled: disabled,
...inputParams,
onChange: onChangeHandler,
onClick: onClickHandler,
onKeyDown: onKeyDownHandler,
ref: ref
}), _jsx("span", {
className: clsx('dnb-checkbox__button', createSkeletonClass('shape', skeleton, context)),
"aria-hidden": true,
children: _span || (_span = _jsx("span", {
className: "dnb-checkbox__focus"
}))
}), _span2 || (_span2 = _jsx("span", {
className: "dnb-checkbox__indeterminate"
})), _jsx(CheckIcon, {
size: size
})]
})]
}), suffix && _jsx(Suffix, {
className: "dnb-checkbox__suffix",
id: id + '-suffix',
context: props,
children: suffix
})]
}), (labelPosition === 'right' || !labelPosition) && statusComp]
});
}
withComponentMarkers(Checkbox, {
_formElement: true
});
export default Checkbox;
//# sourceMappingURL=Checkbox.js.map