react-aria
Version:
Spectrum UI components in React
1 lines • 4.58 kB
Source Map (JSON)
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC;;;AAmDM,SAAS,0CACd,KAAwB,EACxB,KAAkB,EAClB,QAA4C;IAE5C,IAAI,cACF,UAAU,cACV,UAAU,oBACV,gBAAgB,qBAChB,iBAAiB,cACjB,UAAU,aACV,SAAS,cACT,UAAU,cACV,UAAU,aACV,SAAS,oBACT,gBAAgB,qBAChB,iBAAiB,EAClB,GAAG,CAAA,GAAA,mCAAQ,EAAE,OAAO,OAAO;IAE5B,IAAI,mBAAC,eAAe,EAAC,GAAG;IACxB,CAAA,GAAA,sBAAQ,EAAE;QACR,qEAAqE;QACrE,mDAAmD;QACnD,IAAI,SAAS,OAAO,EAClB,SAAS,OAAO,CAAC,aAAa,GAAG,CAAC,CAAC;IAEvC;IAEA,OAAO;QACL,YAAY,CAAA,GAAA,oCAAS,EACnB,YACA,CAAA,GAAA,oBAAM,EACJ,IAAO,CAAA;gBACL,0DAA0D;gBAC1D,iFAAiF;gBACjF,aAAa,CAAA,IAAK,EAAE,cAAc;YACpC,CAAA,GACA,EAAE;oBAGN;0BACA;2BACA;oBACA;mBACA;oBACA;oBACA;mBACA;0BACA;2BACA;IACF;AACF","sources":["packages/react-aria/src/checkbox/useCheckbox.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {AriaToggleProps, useToggle} from '../toggle/useToggle';\nimport {\n DOMAttributesWithRef,\n InputDOMProps,\n RefObject,\n ValidationResult\n} from '@react-types/shared';\nimport {InputHTMLAttributes, LabelHTMLAttributes, useEffect, useMemo} from 'react';\nimport {mergeProps} from '../utils/mergeProps';\nimport {ToggleProps, ToggleState} from 'react-stately/useToggleState';\n\nexport interface CheckboxProps extends ToggleProps {\n /**\n * Indeterminism is presentational only.\n * The indeterminate visual representation remains regardless of user interaction.\n */\n isIndeterminate?: boolean;\n}\n\nexport interface AriaCheckboxProps extends CheckboxProps, InputDOMProps, AriaToggleProps {}\n\nexport interface CheckboxAria extends ValidationResult {\n /** Props for the label wrapper element. */\n labelProps: LabelHTMLAttributes<HTMLLabelElement>;\n /** Props for the input element. */\n inputProps: InputHTMLAttributes<HTMLInputElement>;\n /** Props for the checkbox description element, if any. */\n descriptionProps: DOMAttributesWithRef<HTMLElement>;\n /** Props for the checkbox error message element, if any. */\n errorMessageProps: DOMAttributesWithRef<HTMLElement>;\n /** Whether the checkbox is selected. */\n isSelected: boolean;\n /** Whether the checkbox is in a pressed state. */\n isPressed: boolean;\n /** Whether the checkbox is disabled. */\n isDisabled: boolean;\n /** Whether the checkbox is read only. */\n isReadOnly: boolean;\n}\n\n/**\n * Provides the behavior and accessibility implementation for a checkbox component.\n * Checkboxes allow users to select multiple items from a list of individual items, or\n * to mark one individual item as selected.\n *\n * @param props - Props for the checkbox.\n * @param state - State for the checkbox, as returned by `useToggleState`.\n * @param inputRef - A ref for the HTML input element.\n */\nexport function useCheckbox(\n props: AriaCheckboxProps,\n state: ToggleState,\n inputRef: RefObject<HTMLInputElement | null>\n): CheckboxAria {\n let {\n labelProps,\n inputProps,\n descriptionProps,\n errorMessageProps,\n isSelected,\n isPressed,\n isDisabled,\n isReadOnly,\n isInvalid,\n validationErrors,\n validationDetails\n } = useToggle(props, state, inputRef);\n\n let {isIndeterminate} = props;\n useEffect(() => {\n // indeterminate is a property, but it can only be set via javascript\n // https://css-tricks.com/indeterminate-checkboxes/\n if (inputRef.current) {\n inputRef.current.indeterminate = !!isIndeterminate;\n }\n });\n\n return {\n labelProps: mergeProps(\n labelProps,\n useMemo(\n () => ({\n // Prevent label from being focused when mouse down on it.\n // Note, this does not prevent the input from being focused in the `click` event.\n onMouseDown: e => e.preventDefault()\n }),\n []\n )\n ),\n inputProps,\n descriptionProps,\n errorMessageProps,\n isSelected,\n isPressed,\n isDisabled,\n isReadOnly,\n isInvalid,\n validationErrors,\n validationDetails\n };\n}\n"],"names":[],"version":3,"file":"useCheckbox.cjs.map"}