@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
64 lines • 3.99 kB
JavaScript
import { __rest } from "tslib";
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useContext } from 'react';
import clsx from 'clsx';
import { getAnalyticsMetadataAttribute } from '@awsui/component-toolkit/internal/analytics-metadata';
import InternalCheckbox from '../../checkbox/internal';
import { SingleTabStopNavigationContext } from '../../internal/context/single-tab-stop-navigation-context';
import { useUniqueId } from '../../internal/hooks/use-unique-id';
import { KeyCode } from '../../internal/keycode';
import RadioButton from '../../radio-group/radio-button';
import styles from './styles.css.js';
export function SelectionControl(_a) {
var { selectionType, indeterminate = false, onShiftToggle, onFocusUp, onFocusDown, name, ariaLabel, focusedComponent, rowIndex, itemKey, verticalAlign = 'middle' } = _a, sharedProps = __rest(_a, ["selectionType", "indeterminate", "onShiftToggle", "onFocusUp", "onFocusDown", "name", "ariaLabel", "focusedComponent", "rowIndex", "itemKey", "verticalAlign"]);
const controlId = useUniqueId();
const isMultiSelection = selectionType === 'multi';
const { navigationActive } = useContext(SingleTabStopNavigationContext);
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, Object.assign({}, sharedProps, { showOutline: focusedComponent === 'selection-control', controlId: controlId, "data-focus-id": "selection-control", indeterminate: indeterminate }))) : (React.createElement(RadioButton, Object.assign({}, sharedProps, { controlId: controlId, name: name, value: '', label: '' })));
return (React.createElement(React.Fragment, null,
React.createElement("label", Object.assign({ 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