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.

96 lines (94 loc) 4.54 kB
/* checkbox.tsx generated by @compiled/babel-plugin v0.36.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 { B200 } from '@atlaskit/theme/colors'; import { CheckboxIcon, Label, LabelText, RequiredIndicator } from './internal'; const checkboxStyles = null; /** * __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.0.4" }); 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, 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(["_19itglyw _nd5lfibj _12ji1r31 _1qu2glyw _12y31o36 _1bsb1osq _4t3i1osq _r06hglyw _6rthze3t _1pfhze3t _12l2ze3t _ahbqze3t _tzy4idpf _r050fibj _9bg71mn3 _19op11so _a30fhteq _1610e4h9 _13wo93zu _q5a61gyf _8gq114p8 _wje2mov0 _1fo21cni _eyedglyw _1ef7js4s _1h6rjs4s _smzgj3jf _1dk7j3jf _l71j1i6y _t34a1i6y _y32gkivo _1ah9199y _1frj180l _h78e1g3k _dwvb1ps2 _1bok170n _1mhr17cl _fx2i3rva _10oaq2ws _w12s155b _15y61vek _e6ww155b _sg1j1vek _rphw18jz _y9y9mz2b _141bmz2b _255gmz2b _jj67mz2b _swhgmz2b _axq81diq _1jiz1diq _s9051diq _1efy1diq _1oav1diq _10y313gf _1wt913gf _qq8613gf _1adw13gf _12sr13gf _16jiglyw _693jglyw _1niqglyw _cl9tglyw _1jbnglyw _j1ta1hou _18qu1ps2 _1g52170n _1log17cl _32hzmz2b _1vv91diq _yjhd1hou _w1cowc43 _jdqn1onz _4y4t1onz _lvfrwc43 _g68dwc43 _13dk1onz _w1el1onz _a9yw1onz _1l3g1onz _dcdpgir2 _wobcgir2 _rp9wgir2 _9ebfwc43 _e43iwc43 _1tkuwc43 _1lehwc43 _b9q3wc43 _tu2918qt _1uxv18qt _1ufw18qt _opo918qt _1e8318qt _1k3d18qt _25yv18qt _1igz18qt _c7cc18qt _1swg18qt", 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;