UNPKG

@atlaskit/checkbox

Version:

A checkbox is an input control that allows a user to select one or more options from a number of choices.

108 lines (106 loc) 4.12 kB
/* checkbox.tsx generated by @compiled/babel-plugin v0.39.1 */ import _extends from "@babel/runtime/helpers/extends"; import "./checkbox.compiled.css"; import * as React from 'react'; import { ax, ix } from "@compiled/react/runtime"; import { forwardRef, memo, useCallback, useEffect, useRef, useState } from 'react'; import { usePlatformLeafEventHandler } from '@atlaskit/analytics-next/usePlatformLeafEventHandler'; import mergeRefs from '@atlaskit/ds-lib/merge-refs'; import CheckboxIcon from './internal/checkbox-icon'; import Label from './internal/label'; import LabelText from './internal/label-text'; import RequiredIndicator from './internal/required-indicator'; /** * The input is visually hidden but remains in the DOM for accessibility. * State-based styling is handled by the Label component using CSS custom properties * that cascade to the CheckboxIcon, avoiding nested sibling selectors. */ const checkboxStyles = { root: "_19itglyw _nd5lfibj _12ji1r31 _1qu2glyw _12y31o36 _1bsb1osq _4t3i1osq _r06hglyw _6rthze3t _1pfhze3t _12l2ze3t _ahbqze3t _tzy4idpf" }; /** * __Checkbox__ * * A checkbox an input control that allows a user to select one or more options from a number of choices. * * - [Examples](https://atlassian.design/components/checkbox/examples) * - [Code](https://atlassian.design/components/checkbox/code) * - [Usage](https://atlassian.design/components/checkbox/usage) */ const Checkbox = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Checkbox({ isChecked: isCheckedProp, isDisabled = false, isInvalid = false, defaultChecked = false, isIndeterminate = false, onChange: onChangeProps, analyticsContext, label, name, value, isRequired, testId, xcss, className, ...rest }, ref) { const [isCheckedState, setIsCheckedState] = useState(isCheckedProp !== undefined ? isCheckedProp : defaultChecked); const onChange = useCallback((e, analyticsEvent) => { setIsCheckedState(e.target.checked); if (onChangeProps) { onChangeProps(e, analyticsEvent); } }, [onChangeProps]); const onChangeAnalytics = usePlatformLeafEventHandler({ fn: onChange, action: 'changed', analyticsData: analyticsContext, componentName: 'checkbox', packageName: "@atlaskit/checkbox", packageVersion: "17.3.12" }); const internalRef = useRef(null); const mergedRefs = mergeRefs([internalRef, ref]); // Use isChecked from the state if it is controlled const isChecked = isCheckedProp === undefined ? isCheckedState : isCheckedProp; useEffect(() => { if (internalRef.current) { internalRef.current.indeterminate = isIndeterminate; } }, [isIndeterminate]); return /*#__PURE__*/React.createElement(Label, { isDisabled: isDisabled, isChecked: isChecked, isIndeterminate: isIndeterminate, isInvalid: isInvalid, label: label, id: rest.id ? `${rest.id}-label` : undefined, testId: testId && `${testId}--checkbox-label` // Currently the rule hasn't been updated to enable "allowed" dynamic pass-throughs. // When there is more usage of this pattern we'll update the lint rule. , xcss: xcss }, /*#__PURE__*/React.createElement("input", _extends({ // It is necessary only for Safari. It allows to render focus styles. tabIndex: 0 }, rest, { type: "checkbox", ref: mergedRefs, disabled: isDisabled, checked: isChecked, value: value, name: name, required: isRequired, // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop className: ax([checkboxStyles.root, className]), onChange: onChangeAnalytics, "aria-invalid": isInvalid ? 'true' : undefined, "data-testid": testId && `${testId}--hidden-checkbox`, "data-invalid": isInvalid ? 'true' : undefined })), /*#__PURE__*/React.createElement(CheckboxIcon, { isIndeterminate: isIndeterminate, isChecked: isChecked }), label && /*#__PURE__*/React.createElement(LabelText, null, label, isRequired && /*#__PURE__*/React.createElement(RequiredIndicator, null))); })); Checkbox.displayName = 'Checkbox'; export default Checkbox;