Advertisement
gjasnosz

Untitled

Oct 27th, 2021
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import PropTypes from "prop-types";
  3. import withStyles from "@material-ui/core/styles/withStyles";
  4. import Autocomplete, { createFilterOptions } from "@mui/material/Autocomplete";
  5. import TextField from "@material-ui/core/TextField";
  6.  
  7. const propTypes = {
  8.     onChange: PropTypes.func.isRequired,
  9.     label: PropTypes.string,
  10.     classes: PropTypes.shape({
  11.         input: PropTypes.string,
  12.         textField: PropTypes.string,
  13.         cssLabel: PropTypes.string,
  14.         cssOutlinedInput: PropTypes.string,
  15.         cssFocused: PropTypes.string,
  16.         notchedOutline: PropTypes.string,
  17.         loginFail: PropTypes.string,
  18.         helperText: PropTypes.string
  19.     }).isRequired,
  20.     children: PropTypes.node,
  21.     defaultValue: PropTypes.string.isRequired,
  22.     // eslint-disable-next-line react/forbid-prop-types
  23.     passedOptions: PropTypes.array.isRequired
  24. };
  25.  
  26. const defaultProps = {
  27.     children: null,
  28.     label: null,
  29. };
  30.  
  31. const styles = {
  32.     root: {
  33.         color: "#6b705c",
  34.         fontFamily: "Open Sans, sans-serif",
  35.         fontSize: "16px",
  36.         "&:hover, &:focus": {
  37.             outline: "none"
  38.         }
  39.     },
  40.     textField: {
  41.         width: "20rem",
  42.         marginTop: "0",
  43.         marginBottom: "8px"
  44.     },
  45.  
  46.     cssLabel: {
  47.         color: "black !important",
  48.         fontFamily: "Open Sans, sans-serif",
  49.         fontSize: "14px",
  50.         backgroundColor: "#F0EFEB !important"
  51.     },
  52.     cssOutlinedInput: {
  53.         height: "56px !important",
  54.         "&$cssFocused $notchedOutline": {
  55.             borderColor: "#a5a58d !important",
  56.         }
  57.     },
  58.     cssFocused: {
  59.         fontFamily: "Open Sans, sans-serif"
  60.     },
  61.  
  62.     notchedOutline: {
  63.         borderWidth: "1px",
  64.     },
  65.     helperText: {
  66.         margin: 0,
  67.         fontFamily: "Open Sans, sans-serif",
  68.         fontSize: "12px",
  69.     }
  70. };
  71. const filter = createFilterOptions();
  72.  
  73. const AutocompleteInput = ({
  74.     onChange, classes, defaultValue, passedOptions
  75. }) => (
  76.     <Autocomplete
  77.         onSelect={onChange}
  78.         value={defaultValue}
  79.         filterOptions={(options, params) => {
  80.             const filtered = filter(options, params);
  81.  
  82.             const { inputValue } = params;
  83.             const isExisting = options.some((option) => inputValue === option.name);
  84.             if (inputValue !== "" && !isExisting) {
  85.                 filtered.push({
  86.                     inputValue,
  87.                     name: `Add "${inputValue}"`,
  88.                 });
  89.             }
  90.  
  91.             return filtered;
  92.         }}
  93.         selectOnFocus
  94.         handleHomeEndKeys
  95.         options={passedOptions}
  96.         getOptionLabel={(option) => {
  97.             if (typeof option === "string") {
  98.                 return option;
  99.             }
  100.             if (option.inputValue) {
  101.                 return option.inputValue;
  102.             }
  103.             return option.name;
  104.         }}
  105.         /* eslint-disable-next-line react/jsx-props-no-spreading */
  106.         renderOption={(props, option) => <li {...props}>{option.name}</li>}
  107.         sx={{ width: 300 }}
  108.         classes={{
  109.             root: classes.cssOutlinedInput,
  110.             option: classes.cssLabel,
  111.             inputRoot: classes.cssLabel,
  112.             input: classes.cssLabel,
  113.             paper: classes.cssLabel
  114.         }}
  115.         freeSolo
  116.         renderInput={(params) => (
  117.             <TextField
  118.                 /* eslint-disable-next-line react/jsx-props-no-spreading */
  119.                 {...params}
  120.                 variant="outlined"
  121.                 className={classes.textField}
  122.                 InputLabelProps={{
  123.                     classes: {
  124.                         root: classes.cssLabel,
  125.                         focused: classes.cssFocused,
  126.                     },
  127.                 }}
  128.                 InputProps={{
  129.                     ...params.InputProps,
  130.                     classes: {
  131.                         root: classes.cssOutlinedInput,
  132.                         focused: classes.cssFocused,
  133.                         notchedOutline: classes.notchedOutline,
  134.                     }
  135.                 }}
  136.             />
  137.         )}
  138.     />
  139.  
  140. );
  141.  
  142. AutocompleteInput.propTypes = propTypes;
  143. AutocompleteInput.defaultProps = defaultProps;
  144.  
  145. export default withStyles(styles)(AutocompleteInput);
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement