UNPKG

aws-northstar

Version:
83 lines (79 loc) 4.33 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, { useCallback, useEffect, useMemo } from 'react'; import MaterialInput from '@material-ui/core/OutlinedInput'; import { v4 as uuidv4 } from 'uuid'; import SearchIcon from '@material-ui/icons/Search'; import ClearIcon from '@material-ui/icons/Clear'; import InputAdornment from '@material-ui/core/InputAdornment'; const mapProps = (_a) => { var { type = 'text', required = false, autocomplete = true, disableBrowserAutocorrect = false } = _a, props = __rest(_a, ["type", "required", "autocomplete", "disableBrowserAutocorrect"]); const autoCorrectString = disableBrowserAutocorrect ? 'off' : 'on'; const autoCompleteString = autocomplete ? 'on' : 'off'; const inputProps = { 'aria-labelledby': props.ariaLabelledby, 'aria-describedby': props.ariaDescribedby, 'aria-required': required, }; return { type, name: props.name, required, placeholder: props.placeholder, disabled: props.disabled, readOnly: props.readonly, error: props.invalid, autoComplete: autoCompleteString, autoCorrect: autoCorrectString, autoFocus: props.autofocus, inputProps, }; }; /** * Enables users to input text in a single-line control. */ const Input = (_a) => { var { onChange } = _a, props = __rest(_a, ["onChange"]); const [showClearInputButton, setShowClearInputButton] = React.useState(false); const [inputValue, setInputValue] = React.useState(props.value === undefined ? '' : props.value); const id = props.controlId || uuidv4(); useEffect(() => { setInputValue(props.value === undefined ? '' : props.value); }, [props.value]); const handleChange = useCallback((value) => { onChange === null || onChange === void 0 ? void 0 : onChange(value); setShowClearInputButton(true); setInputValue(value); }, [onChange]); const testId = props['data-testid'] || `input-${props.type || 'text'}`; const clearSearchInput = useMemo(() => (React.createElement(InputAdornment, { position: "start" }, React.createElement(ClearIcon, { color: "action", "data-testid": `${testId}-clear-input`, onClick: () => { setShowClearInputButton(false); setInputValue(''); handleChange(''); } }))), [testId, handleChange]); return (React.createElement(MaterialInput, Object.assign({ id: id, fullWidth: true }, mapProps(props), { startAdornment: props.type === 'search' && (React.createElement(InputAdornment, { position: "start" }, React.createElement(SearchIcon, { color: "action" }))), endAdornment: props.type === 'search' && showClearInputButton && clearSearchInput, value: inputValue, onChange: (e) => handleChange(e.target.value), onBlur: props.onBlur, onFocus: props.onFocus, "data-testid": testId }))); }; export default Input;