@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) • 5.74 kB
JavaScript
/* checkbox.tsx generated by @compiled/babel-plugin v0.36.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 { B200 } from '@atlaskit/theme/colors';
import { CheckboxIcon, Label, LabelText, RequiredIndicator } from './internal';
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__*/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.0.4"
});
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,
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(["_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": ix("var(--ds-border-width-outline, 2px)".concat(" solid ", "var(--ds-border-focused, ".concat(B200, ")")))
}
})), /*#__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;