From 0fc3ea6146c4b5472c5e297d689f9f2f1eb0d1e5 Mon Sep 17 00:00:00 2001 From: Ari Date: Wed, 15 Jun 2022 10:43:14 -0400 Subject: [PATCH] selector.select --- .../components/Inputs/InputAutoComplete.tsx | 12 +- .../src/components/Inputs/InputAutoText.tsx | 57 +++++---- webapp/src/components/Inputs/extras.tsx | 115 +++++++++++------- .../ServiceEditor/ServiceEditorField.tsx | 36 ++++++ 4 files changed, 155 insertions(+), 65 deletions(-) diff --git a/webapp/src/components/Inputs/InputAutoComplete.tsx b/webapp/src/components/Inputs/InputAutoComplete.tsx index 62502f5..cf9efd2 100644 --- a/webapp/src/components/Inputs/InputAutoComplete.tsx +++ b/webapp/src/components/Inputs/InputAutoComplete.tsx @@ -5,19 +5,23 @@ import TextField from "@mui/material/TextField"; import Chip from "@mui/material/Chip"; import { Badge } from "@mui/material"; import { SearchItem } from "./extras"; +import { ReactNode } from "react"; export type InputAutoCompleteProps> = { - className?: string; validateOption?: (v: string[]) => string[] | null; groupBy?: (opt: T) => string; options: T[]; getLabel?: (opt: T) => string; getID?: (opt: T) => string; helperText?: string; + endAdornment?: ReactNode; + required?: boolean; + onlyShowLabel?: boolean; } & InputAutoCompletePropsBase; export type InputAutoCompletePropsBase = { label?: string; + className?: string; } & ( | { value: string | null; @@ -155,6 +159,7 @@ export function InputAutoComplete( id={id} label={label} searchTerm={inputValue} + onlyShowLabel={props.onlyShowLabel} /> ); }} @@ -162,6 +167,7 @@ export function InputAutoComplete( ( label={ <> {id === label ? {label} : {label}} - {id !== label && {id}} + {id !== label && !props.onlyShowLabel && ( + {id} + )} } {...tagProps} diff --git a/webapp/src/components/Inputs/InputAutoText.tsx b/webapp/src/components/Inputs/InputAutoText.tsx index be77b2c..ee3be35 100644 --- a/webapp/src/components/Inputs/InputAutoText.tsx +++ b/webapp/src/components/Inputs/InputAutoText.tsx @@ -1,11 +1,15 @@ import TextField from "@mui/material/TextField"; import Autocomplete from "@mui/material/Autocomplete"; +import { ReactNode } from "react"; export interface InputAutoTextProps { label: string; value: string; + className?: string; options?: string[]; error?: string; + required?: boolean; + endAdornment?: ReactNode; onChange: (v: string) => void; } export default function InputAutoText({ @@ -13,28 +17,37 @@ export default function InputAutoText({ label, options = [], onChange, + className, + required, + endAdornment, error, }: InputAutoTextProps) { - - return onChange( - v === null ? "" : v - )} - renderInput={params => { - e.preventDefault(); - onChange( - e.target.value - ) - }} - />} - /> + console.log(options); + return ( + onChange(v === null ? "" : v)} + renderInput={(params) => ( + { + e.preventDefault(); + onChange(e.target.value); + }} + /> + )} + /> + ); } diff --git a/webapp/src/components/Inputs/extras.tsx b/webapp/src/components/Inputs/extras.tsx index 067f620..bd0d4fa 100644 --- a/webapp/src/components/Inputs/extras.tsx +++ b/webapp/src/components/Inputs/extras.tsx @@ -1,47 +1,80 @@ import "./extras.css"; import { FC } from "react"; -import parse from 'autosuggest-highlight/parse'; -import match from 'autosuggest-highlight/match'; - +import parse from "autosuggest-highlight/parse"; +import match from "autosuggest-highlight/match"; export const HighlightElm: FC<{ - text: string, - searchTerm: string -}> = ({ - text, - searchTerm -}) => { - const matches = match(text, searchTerm, { - "findAllOccurrences": true, - "insideWords": true - }); - const parts = parse(text, matches); - return <> - {parts.map((part, index) => ( - - {part.text} - - ))} - - } + text: string; + searchTerm: string; +}> = ({ text, searchTerm }) => { + const matches = match(text, searchTerm, { + findAllOccurrences: true, + insideWords: true, + }); + const parts = parse(text, matches); + return ( + <> + {parts.map((part, index) => ( + + {part.text} + + ))} + + ); +}; export const SearchItem: FC<{ - listProps: React.HTMLAttributes, - label: string, - id: string, - searchTerm: string, -}> = ({ - listProps, - label, - id, - searchTerm -}) =>
  • - - - -
  • \ No newline at end of file + listProps: React.HTMLAttributes; + label: string; + id: string; + searchTerm: string; + onlyShowLabel?: boolean; +}> = ({ listProps, label, id, searchTerm, onlyShowLabel = false }) => { + let inner = ( + <> + + + {" "} + + + + + ); + + if (onlyShowLabel) { + if (label) { + inner = ( + <> + + + {" "} + + ); + } else { + inner = ( + <> + + + + + ); + } + } + + return ( +
  • + {inner} +
  • + ); +}; diff --git a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx index 7c7db1e..d973d4f 100644 --- a/webapp/src/components/ServiceEditor/ServiceEditorField.tsx +++ b/webapp/src/components/ServiceEditor/ServiceEditorField.tsx @@ -6,6 +6,10 @@ import InputNumber from "components/Inputs/InputNumber"; import { IconButton } from "@mui/material"; import { RemoveCircle } from "@mui/icons-material"; import { InputEntity } from "components/Inputs/InputEntities"; +import InputAutoText from "components/Inputs/InputAutoText"; +import { InputAutoComplete } from "components/Inputs/InputAutoComplete"; +import { Option } from "haService"; +import { prettyName } from "utils/formatting"; export type ServiceEditorFieldProps = { option: ServiceEditorOption; @@ -59,6 +63,37 @@ export const ServiceEditorField: FC = ({ /> ); } + if (option.data.selector?.select) { + return ( + ((opt) => { + if (typeof opt === "string") { + return { + id: opt, + label: prettyName(opt), + }; + } + return { + id: opt.value, + label: opt.label, + }; + })} + getLabel={(opt) => { + if (typeof opt === "string") { + return prettyName(opt); + } + return opt.label; + }} + onlyShowLabel + /> + ); + } return ( = ({