UNPKG

@awsui/components-react

Version:

On July 19th, 2022, we launched [Cloudscape Design System](https://cloudscape.design). Cloudscape is an evolution of AWS-UI. It consists of user interface guidelines, front-end components, design resources, and development tools for building intuitive, en

61 lines 3.7 kB
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import React from 'react'; import clsx from 'clsx'; import { useSingleTabStopNavigation, useUniqueId } from '@awsui/component-toolkit/internal'; import { getAnalyticsMetadataAttribute } from '@awsui/component-toolkit/internal/analytics-metadata'; import InternalCheckbox from '../../checkbox/internal'; import RadioButton from '../../internal/components/radio-button'; import { KeyCode } from '../../internal/keycode'; import styles from './styles.css.js'; export function SelectionControl({ selectionType, indeterminate = false, onShiftToggle, onFocusUp, onFocusDown, name, ariaLabel, focusedComponent, rowIndex, itemKey, verticalAlign = 'middle', onChange, ...sharedProps }) { const controlId = useUniqueId(); const isMultiSelection = selectionType === 'multi'; const { navigationActive } = useSingleTabStopNavigation(null); const setShiftState = (event) => { if (isMultiSelection) { onShiftToggle === null || onShiftToggle === void 0 ? void 0 : onShiftToggle(event.shiftKey); } }; const onMouseDownHandler = (event) => { setShiftState(event); if (isMultiSelection) { // To overcome an issue // If you shift+click or ctrl+click on a label for a checkbox, checkbox is not checked. // https://bugzilla.mozilla.org/show_bug.cgi?id=559506 event.preventDefault(); } }; // native checkboxes do not have focus move via keyboard, we implement it here programmatically const handleKeyDown = (event) => { setShiftState(event); if (isMultiSelection && !navigationActive) { if (event.keyCode === KeyCode.up) { event.preventDefault(); onFocusUp === null || onFocusUp === void 0 ? void 0 : onFocusUp(event); } if (event.keyCode === KeyCode.down) { event.preventDefault(); onFocusDown === null || onFocusDown === void 0 ? void 0 : onFocusDown(event); } } }; const handleClick = (event) => { const target = event.currentTarget; const nativeInput = (target.tagName === 'INPUT' ? target : target.querySelector('input')); // Clicking on input, does not focus it on Firefox (AWSUI-11345) nativeInput === null || nativeInput === void 0 ? void 0 : nativeInput.focus(); }; const selector = isMultiSelection ? (React.createElement(InternalCheckbox, { ...sharedProps, onChange: onChange, showOutline: focusedComponent === 'selection-control', controlId: controlId, "data-focus-id": "selection-control", indeterminate: indeterminate })) : (React.createElement(RadioButton, { ...sharedProps, controlId: controlId, name: name, value: '', onSelect: onChange })); return (React.createElement(React.Fragment, null, React.createElement("label", { onKeyDown: handleKeyDown, onKeyUp: setShiftState, onMouseDown: onMouseDownHandler, onMouseUp: setShiftState, onClick: handleClick, htmlFor: controlId, className: clsx(styles.label, styles.root, verticalAlign === 'top' && styles['label-top']), "aria-label": ariaLabel, title: ariaLabel, ...(rowIndex !== undefined && !sharedProps.disabled ? getAnalyticsMetadataAttribute({ detail: { position: `${rowIndex + 1}`, item: itemKey || '', }, }) : {}) }, selector), React.createElement("span", { className: styles.stud, "aria-hidden": true }, "\u00A0"))); } //# sourceMappingURL=selection-control.js.map