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.

117 lines (115 loc) 5.18 kB
/* checkbox.tsx generated by @compiled/babel-plugin v0.39.1 */ import _extends from "@babel/runtime/helpers/extends"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["isChecked", "isDisabled", "isInvalid", "defaultChecked", "isIndeterminate", "onChange", "analyticsContext", "label", "name", "value", "isRequired", "testId", "xcss", "className"]; 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. */ var 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) */ var Checkbox = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function Checkbox(_ref, ref) { var isCheckedProp = _ref.isChecked, _ref$isDisabled = _ref.isDisabled, isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled, _ref$isInvalid = _ref.isInvalid, isInvalid = _ref$isInvalid === void 0 ? false : _ref$isInvalid, _ref$defaultChecked = _ref.defaultChecked, defaultChecked = _ref$defaultChecked === void 0 ? false : _ref$defaultChecked, _ref$isIndeterminate = _ref.isIndeterminate, isIndeterminate = _ref$isIndeterminate === void 0 ? false : _ref$isIndeterminate, onChangeProps = _ref.onChange, analyticsContext = _ref.analyticsContext, label = _ref.label, name = _ref.name, value = _ref.value, isRequired = _ref.isRequired, testId = _ref.testId, xcss = _ref.xcss, className = _ref.className, rest = _objectWithoutProperties(_ref, _excluded); var _useState = useState(isCheckedProp !== undefined ? isCheckedProp : defaultChecked), _useState2 = _slicedToArray(_useState, 2), isCheckedState = _useState2[0], setIsCheckedState = _useState2[1]; var onChange = useCallback(function (e, analyticsEvent) { setIsCheckedState(e.target.checked); if (onChangeProps) { onChangeProps(e, analyticsEvent); } }, [onChangeProps]); var onChangeAnalytics = usePlatformLeafEventHandler({ fn: onChange, action: 'changed', analyticsData: analyticsContext, componentName: 'checkbox', packageName: "@atlaskit/checkbox", packageVersion: "17.3.12" }); var internalRef = useRef(null); var mergedRefs = mergeRefs([internalRef, ref]); // Use isChecked from the state if it is controlled var isChecked = isCheckedProp === undefined ? isCheckedState : isCheckedProp; useEffect(function () { 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 ? "".concat(rest.id, "-label") : undefined, testId: testId && "".concat(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 && "".concat(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;