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.

118 lines (115 loc) 7.09 kB
/* checkbox.tsx generated by @compiled/babel-plugin v0.36.1 */ "use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("./checkbox.compiled.css"); var _react = _interopRequireWildcard(require("react")); var React = _react; var _runtime = require("@compiled/react/runtime"); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _usePlatformLeafEventHandler = require("@atlaskit/analytics-next/usePlatformLeafEventHandler"); var _mergeRefs = _interopRequireDefault(require("@atlaskit/ds-lib/merge-refs")); var _colors = require("@atlaskit/theme/colors"); var _internal = require("./internal"); var _excluded = ["isChecked", "isDisabled", "isInvalid", "defaultChecked", "isIndeterminate", "onChange", "analyticsContext", "label", "name", "value", "isRequired", "testId", "xcss", "className"]; function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } var 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) */ var Checkbox = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.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 = (0, _objectWithoutProperties2.default)(_ref, _excluded); var _useState = (0, _react.useState)(isCheckedProp !== undefined ? isCheckedProp : defaultChecked), _useState2 = (0, _slicedToArray2.default)(_useState, 2), isCheckedState = _useState2[0], setIsCheckedState = _useState2[1]; var onChange = (0, _react.useCallback)(function (e, analyticsEvent) { setIsCheckedState(e.target.checked); if (onChangeProps) { onChangeProps(e, analyticsEvent); } }, [onChangeProps]); var onChangeAnalytics = (0, _usePlatformLeafEventHandler.usePlatformLeafEventHandler)({ fn: onChange, action: 'changed', analyticsData: analyticsContext, componentName: 'checkbox', packageName: "@atlaskit/checkbox", packageVersion: "17.0.4" }); var internalRef = (0, _react.useRef)(null); var mergedRefs = (0, _mergeRefs.default)([internalRef, ref]); // Use isChecked from the state if it is controlled var isChecked = isCheckedProp === undefined ? isCheckedState : isCheckedProp; (0, _react.useEffect)(function () { if (internalRef.current) { internalRef.current.indeterminate = isIndeterminate; } }, [isIndeterminate]); return /*#__PURE__*/React.createElement(_internal.Label, { isDisabled: isDisabled, 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", (0, _extends2.default)({ // 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: (0, _runtime.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 _smzgo41a _1dk7o41a _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 && "".concat(testId, "--hidden-checkbox"), "data-invalid": isInvalid ? 'true' : undefined, style: { "--_gqccd2": (0, _runtime.ix)("var(--ds-border-width-outline, 2px)".concat(" solid ", "var(--ds-border-focused, ".concat(_colors.B200, ")"))) } })), /*#__PURE__*/React.createElement(_internal.CheckboxIcon, { isIndeterminate: isIndeterminate, isChecked: isChecked }), label && /*#__PURE__*/React.createElement(_internal.LabelText, null, label, isRequired && /*#__PURE__*/React.createElement(_internal.RequiredIndicator, null))); })); Checkbox.displayName = 'Checkbox'; var _default = exports.default = Checkbox;