UNPKG

aws-northstar

Version:
171 lines (167 loc) 9.3 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, { useEffect, useState, useMemo, useCallback, } from 'react'; import TextField from '@material-ui/core/TextField'; import MaterialUIAutocomplete from '@material-ui/lab/Autocomplete'; import Link from '@material-ui/core/Link'; import SearchIcon from '@material-ui/icons/Search'; import InputAdornment from '@material-ui/core/InputAdornment'; import { makeStyles } from '@material-ui/core/styles'; import TokenGroup from '../TokenGroup'; import Checkbox from '../Checkbox'; import Stack from '../../layouts/Stack'; import LoadingIndicator from '../LoadingIndicator'; import StatusIndicator from '../StatusIndicator'; import { getFlattenOptions } from '../../utils/getFlattenOptions'; import useUniqueId from '../../hooks/useUniqueId'; const useStyles = makeStyles({ input: { display: 'inline-block', marginRight: '20px', }, recoveryLink: { pointerEvents: 'auto', }, textfield: { paddingRight: '30px', }, }); const covertStringValue = (value) => { if (typeof value === 'string') { return { value, label: value, }; } return value; }; const getValue = (value) => { if (!value.value) { return value.multiple ? [] : null; } if (value.multiple) { return value.value.map(covertStringValue); } return covertStringValue(value.value); }; /** * An autosuggest control is a normal text input enhanced by a panel of suggested options. * */ const AutosuggestBase = (_a) => { var { options = [], filteringType = 'auto', loadingText = 'Loading ...', errorText = 'Error fetching', recoveryText, name, empty, disabled, placeholder, invalid, icon, ariaRequired = false, freeSolo = false, disableClearable = false, ariaDescribedby, ariaLabelledby, onInputChange, onFocus, onBlur, onRecoveryClick, disableBrowserAutocorrect = false, statusType = 'finished', filterOptions, value } = _a, props = __rest(_a, ["options", "filteringType", "loadingText", "errorText", "recoveryText", "name", "empty", "disabled", "placeholder", "invalid", "icon", "ariaRequired", "freeSolo", "disableClearable", "ariaDescribedby", "ariaLabelledby", "onInputChange", "onFocus", "onBlur", "onRecoveryClick", "disableBrowserAutocorrect", "statusType", "filterOptions", "value"]); const classes = useStyles(); const [inputValue, setInputValue] = useState(getValue({ multiple: props.multiple, value, })); const [open, setOpen] = React.useState(false); const controlId = useUniqueId(props.controlId); const autoCompleteString = disableBrowserAutocorrect ? 'off' : 'on'; useEffect(() => { setInputValue(getValue({ multiple: props.multiple, value, })); }, [props.multiple, value, setInputValue]); const onRecoveryClickHandler = useCallback((event) => { event.preventDefault(); onRecoveryClick === null || onRecoveryClick === void 0 ? void 0 : onRecoveryClick(event); event.stopPropagation(); }, [onRecoveryClick]); const flattenOptions = useMemo(() => { return getFlattenOptions(options); }, [options]); const handleOnChange = useCallback((_, value) => { var _a; const newValue = getValue({ multiple: props.multiple, value, }); setInputValue(newValue); (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, newValue); }, [props, setInputValue]); const handleOnInput = useCallback((event, value, reason) => { if (filteringType === 'manual') { onInputChange === null || onInputChange === void 0 ? void 0 : onInputChange(event, value, reason); } }, [onInputChange, filteringType]); const getOptionsSelected = useCallback((option, value) => { return option.value === value.value; }, []); const loadingAndErrorText = useMemo(() => (React.createElement(React.Fragment, null, statusType === 'loading' && React.createElement(LoadingIndicator, { label: loadingText }), statusType === 'error' && (React.createElement(StatusIndicator, { statusType: "negative" }, errorText, ".", recoveryText && (React.createElement("span", null, React.createElement(Link, { href: "#", onClick: onRecoveryClickHandler, className: classes.recoveryLink }, recoveryText))))))), [statusType, classes.recoveryLink, errorText, recoveryText, loadingText, onRecoveryClickHandler]); const iconComponent = useMemo(() => { if (icon) { const IconComponent = icon; return React.createElement(IconComponent, { color: "action" }); } return React.createElement(SearchIcon, { color: "action" }); }, [icon]); const textfield = useCallback((params) => (React.createElement(TextField, Object.assign({ autoCorrect: autoCompleteString, placeholder: placeholder, required: ariaRequired, error: !!(statusType === 'error' || invalid) }, params, { variant: "outlined", size: "small", margin: "normal", name: name, InputProps: Object.assign(Object.assign({ 'aria-describedby': ariaDescribedby, 'aria-labelledby': ariaLabelledby }, params.InputProps), { className: classes.textfield, type: 'search', startAdornment: icon !== false && React.createElement(InputAdornment, { position: "start" }, iconComponent) }) }))), [ ariaDescribedby, ariaLabelledby, ariaRequired, autoCompleteString, classes.textfield, invalid, name, placeholder, statusType, icon, iconComponent, ]); const renderOption = useCallback((option) => { var _a, _b; if (props.multiple && props.checkboxes) { return (React.createElement(Checkbox, { checked: inputValue.map((input) => input.value).includes(option.value), value: option.value }, ((_a = props.renderOption) === null || _a === void 0 ? void 0 : _a.call(props, option)) || option.label)); } return ((_b = props.renderOption) === null || _b === void 0 ? void 0 : _b.call(props, option)) || option.label; }, [props, inputValue]); const handleDeleteOption = useCallback((option) => { var _a; if (props.multiple) { const tempData = inputValue.filter((o) => o.value !== option.value); setInputValue(tempData); (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, tempData); } }, [inputValue, setInputValue, props]); const testId = props['data-testid'] || (props.multiple ? 'multiselect' : 'autosuggest'); return (React.createElement(Stack, null, React.createElement(MaterialUIAutocomplete, { multiple: props.multiple, "data-testid": testId, disabled: disabled, autoHighlight: true, popupIcon: null, freeSolo: freeSolo, disableClearable: disableClearable, open: statusType === 'error' || statusType === 'loading' ? true : open, id: controlId, value: inputValue, noOptionsText: empty, options: flattenOptions, groupBy: (option) => option.group || '', loadingText: loadingAndErrorText, getOptionSelected: getOptionsSelected, onChange: handleOnChange, onInputChange: handleOnInput, onFocus: onFocus, onBlur: onBlur, onOpen: () => { setOpen(true); }, onClose: () => { setOpen(false); }, loading: statusType !== 'finished', renderOption: renderOption, filterOptions: filterOptions, getOptionLabel: (option) => option.label || '', renderInput: textfield }), props.multiple && (React.createElement(TokenGroup, { items: (inputValue || []), onDismiss: handleDeleteOption })))); }; const Autosuggest = (props) => { return React.createElement(AutosuggestBase, Object.assign({ multiple: false, checkboxes: false }, props)); }; export default Autosuggest; export { AutosuggestBase };