UNPKG

aws-northstar

Version:
127 lines (123 loc) 6.88 kB
/** ******************************************************************************************************************* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ******************************************************************************************************************** */ var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import React from 'react'; import clsx from 'clsx'; import { v4 as uuidv4 } from 'uuid'; import { makeStyles } from '@material-ui/core/styles'; import MuiLink from '@material-ui/core/Link'; import MuiSelect from '@material-ui/core/Select'; import MenuItem from '@material-ui/core/MenuItem'; import ListSubheader from '@material-ui/core/ListSubheader'; import LoadingIndicator from '../LoadingIndicator'; import StatusIndicator from '../StatusIndicator'; import { ButtonIcon } from '../Button'; import useUniqueId from '../../hooks/useUniqueId'; const useStyles = makeStyles((theme) => ({ iconMenuItem: { marginRight: theme.spacing(0.4), }, groupMenuItem: { paddingLeft: theme.spacing(3), }, subheaderMenuItem: { display: 'flex', alignItems: 'center', pointerEvents: 'none', }, recoveryLink: { pointerEvents: 'auto', }, errorText: { marginRight: theme.spacing(0.4), }, placeholder: { display: 'none', }, })); const renderOptions = (options, classes, renderOption, disabled = false, groupItem = false) => { return options.map((option) => { if (option.options) { const key = typeof option.label === 'string' ? option.label : uuidv4(); return [ React.createElement(ListSubheader, { key: key }, option.label), ...renderOptions(option.options, classes, renderOption, option.disabled, true), ]; } else { const key = option.value || uuidv4(); return (React.createElement(MenuItem, { key: key, value: option.value, disabled: option.disabled || disabled, className: clsx({ [classes.groupMenuItem]: groupItem }) }, option.iconName && (React.createElement("span", { className: classes.iconMenuItem }, React.createElement(ButtonIcon, { type: option.iconName }))), (renderOption === null || renderOption === void 0 ? void 0 : renderOption(option)) || option.label || option.value)); } }); }; const muiSelectProps = ({ ariaDescribedby, ariaLabelledby, ariaRequired, controlId, disabled, invalid, label, onBlur, onChange, onFocus, selectedOption, placeholder, }) => { return Object.assign(Object.assign(Object.assign(Object.assign({ disabled, error: invalid, id: controlId, onChange, placeholder, onClose: onBlur, onOpen: onFocus, value: (selectedOption === null || selectedOption === void 0 ? void 0 : selectedOption.value) || '' }, (ariaLabelledby && { 'aria-labelledby': ariaLabelledby })), (ariaDescribedby && { 'aria-describedby': ariaDescribedby })), (label && { 'aria-label': label })), (ariaRequired && { 'aria-required': ariaRequired })); }; /** * Select components are used for collecting user provided information from a list of options. */ const Select = (_a) => { var { ariaDescribedby, ariaLabelledby, ariaRequired, disabled = false, empty, errorText, invalid = false, label, loadingText, onBlur, onChange, onFocus, onRecoveryClick, options = [], recoveryText, renderOption, placeholder, selectedOption, statusType = 'finished' } = _a, props = __rest(_a, ["ariaDescribedby", "ariaLabelledby", "ariaRequired", "disabled", "empty", "errorText", "invalid", "label", "loadingText", "onBlur", "onChange", "onFocus", "onRecoveryClick", "options", "recoveryText", "renderOption", "placeholder", "selectedOption", "statusType"]); const classes = useStyles(); const controlId = useUniqueId(props.controlId); const onRecoveryClickHandler = (event) => { event.preventDefault(); onRecoveryClick === null || onRecoveryClick === void 0 ? void 0 : onRecoveryClick(event); event.stopPropagation(); }; const testId = props['data-testid'] || 'select'; return (React.createElement(MuiSelect, Object.assign({ variant: "outlined", displayEmpty: true, fullWidth: true }, muiSelectProps({ ariaDescribedby, ariaLabelledby, ariaRequired, controlId, disabled, invalid, label, onBlur, onChange, onFocus, selectedOption, }), { MenuProps: { anchorOrigin: { vertical: 'bottom', horizontal: 'left', }, transitionDuration: 0, getContentAnchorEl: null, }, "data-testid": testId }), placeholder && (React.createElement(MenuItem, { key: "__placeholder__", value: "", disabled: true, className: classes.placeholder }, placeholder)), empty && !options.length && (React.createElement(ListSubheader, { key: "__status__", className: classes.subheaderMenuItem }, empty)), statusType !== 'finished' && (React.createElement(ListSubheader, { className: classes.subheaderMenuItem, key: "__status__" }, statusType === 'loading' && React.createElement(LoadingIndicator, { label: loadingText }), statusType === 'error' && (React.createElement(React.Fragment, null, React.createElement("span", { className: classes.errorText }, React.createElement(StatusIndicator, { statusType: "negative" }, errorText)), recoveryText && (React.createElement(MuiLink, { href: "#", onClick: onRecoveryClickHandler, className: classes.recoveryLink }, recoveryText)))))), statusType === 'finished' && renderOptions(options, classes, renderOption))); }; export default Select;