import "./InputAutoComplete.css"; import Autocomplete from "@mui/material/Autocomplete"; import TextField from "@mui/material/TextField"; import Chip from "@mui/material/Chip"; import Badge from "@mui/material/Badge"; import Popper from "@mui/material/Popper"; import { SearchItem } from "./extras"; import { ReactNode } from "react"; import { prettyName } from "utils/formatting"; import { useLang } from "lang"; export type BaseOption = { id: string; label: string; } & T; export type Option = string | BaseOption; export type InputAutoCompleteProps> = { validateOption?: (v: string[]) => string[] | null; groupBy?: (opt: T) => string; options: T[]; getLabel?: (opt: T) => string; getID?: (opt: T) => string; } & InputAutoCompletePropsBase; export type InputAutoCompletePropsBase = { label?: string; className?: string; disabled?: boolean; required?: boolean; onlyShowLabel?: boolean; helperText?: ReactNode; endAdornment?: ReactNode; onEnter?: () => void; disableChips?: boolean; } & ( | { value: string | null; multiple?: false; onChange: (v: string | null) => void; } | { value: string | string[]; multiple: true; onChange: (v: string[]) => void; } ); export function computeSimilarity( text: string, optId: string, optLabel: string ) { let similarityScore = 0; const lowCaseId = optId.toLocaleLowerCase(); const lowCaseLabel = optLabel.toLocaleLowerCase(); for (const segment of text.toLocaleLowerCase().split(" ")) { if (lowCaseId.includes(segment)) { similarityScore += lowCaseId.length / lowCaseId.length; } if (lowCaseLabel.includes(segment)) { similarityScore += lowCaseLabel.length / lowCaseLabel.length; } } return similarityScore; } export function InputAutoComplete( props: InputAutoCompleteProps ) { const lang = useLang(); // aliases const getID = (opt: T): string => { if (props.getID) { return props.getID(opt); } if (typeof opt === "string") { return opt; } return opt.id; }; const getLabel = (opt: T): string => { if (props.getLabel) { return props.getLabel(opt); } if (typeof opt === "string") { return opt; } return opt.label; }; // processing of inputs const value = Array.isArray(props.value) ? props.value : !!props.value ? [props.value] : []; const errors = props.validateOption ? props.validateOption(value) : null; const options = props.options .filter((opt) => !value.includes(getID(opt))) .sort((a, b) => { return getID(a) > getID(b) ? 1 : -1; }); // processing of errors let helperText = props.helperText ?? <>; let error: boolean = false; if (errors !== null) { error = true; if (errors.length > 0) { helperText = (
    {errors.map((msg, i) => (
  • {msg}
  • ))}
); } } // events const onChange = (_e: any, v: any) => { const cleanValue: string[] = v === null ? [] : Array.isArray(v) ? v.map((opt) => { if (typeof opt === "string") { return opt; } return opt.id; }) : [v]; if (props.multiple) { props.onChange(cleanValue); } else { props.onChange(cleanValue[cleanValue.length - 1]); } }; return ( { return ( ); }} filterOptions={(opts, s) => { const searchTerm = s.inputValue; if (!searchTerm) { return opts; } return opts .map((opt) => { return { opt, similarityScore: computeSimilarity( searchTerm, getID(opt), getLabel(opt) ), }; }) .filter((opt) => opt.similarityScore > 0) .sort((a, b) => { return a.similarityScore < b.similarityScore ? 1 : -1; }) .map(({ opt }) => opt); }} groupBy={props.groupBy} renderOption={(listProps, option, { inputValue }) => { const label = getLabel(option); const id = getID(option); return ( <> ); }} renderInput={(params) => ( { if (e.key === "Enter" && props.onEnter) { props.onEnter(); } }} variant="filled" required={props.required} label={props.label ?? lang.get("ENTITY_ID")} helperText={helperText} disabled={props.disabled} error={error} placeholder={ props.disableChips && !props.multiple ? props.value ?? "" : "" } /> )} renderTags={(tagValue, getTagProps) => { const inner = (
{tagValue.map((option, index) => { const label = getLabel(option); const id = getID(option); const tagProps = getTagProps({ index }); tagProps.className += " input-autocomplete--chip"; return ( {id === label ? ( {prettyName(label)} ) : ( {label} )} {id !== label && !props.onlyShowLabel && ( {id} )} } {...tagProps} /> ); })}
); if (props.disableChips) { return <>; } if (props.multiple) { return ( {inner} ); } else { return inner; } }} /> ); }