selector.select

This commit is contained in:
ari committed 2022-06-15 10:43:14 -04:00
1 parent 596c3363e7
commit 0fc3ea6146
4 files changed
+155 -65

No files matched your search

@@ -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<T extends Option<any>> = {
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<T extends Option>(
id={id}
label={label}
searchTerm={inputValue}
onlyShowLabel={props.onlyShowLabel}
/>
);
}}
@@ -162,6 +167,7 @@ export function InputAutoComplete<T extends Option>(
<TextField
{...params}
variant="filled"
required={props.required}
label={props.label ?? "Entity ID"}
helperText={helperText}
error={error}
@@ -181,7 +187,9 @@ export function InputAutoComplete<T extends Option>(
label={
<>
{id === label ? <span>{label}</span> : <b>{label}</b>}
{id !== label && <small>{id}</small>}
{id !== label && !props.onlyShowLabel && (
<small>{id}</small>
)}
</>
}
{...tagProps}
+35 -22
View File
@@ -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 <Autocomplete
freeSolo
value={value}
options={options}
onChange={(_e, v) => onChange(
v === null ? "" : v
)}
renderInput={params => <TextField
{...params}
variant="filled"
label={label}
error={error !== undefined}
helperText={error}
onChange={e => {
e.preventDefault();
onChange(
e.target.value
)
}}
/>}
/>
console.log(options);
return (
<Autocomplete
freeSolo
className={className}
value={value}
options={options}
onChange={(_e, v) => onChange(v === null ? "" : v)}
renderInput={(params) => (
<TextField
{...params}
required={required}
variant="filled"
label={label}
error={error !== undefined}
helperText={error}
fullWidth
InputProps={{
endAdornment,
}}
onChange={(e) => {
e.preventDefault();
onChange(e.target.value);
}}
/>
)}
/>
);
}
+74 -41
View File
@@ -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) => (
<span
key={index}
style={{
textDecoration: part.highlight ? 'underline' : 'unset'
}}
>
{part.text}
</span>
))}
</>
}
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) => (
<span
key={index}
style={{
textDecoration: part.highlight ? "underline" : "unset",
}}
>
{part.text}
</span>
))}
</>
);
};
export const SearchItem: FC<{
listProps: React.HTMLAttributes<HTMLLIElement>,
label: string,
id: string,
searchTerm: string,
}> = ({
listProps,
label,
id,
searchTerm
}) => <li {...listProps} className={[listProps.className ?? "", 'input-extras--search-item--list'].join(' ')}>
<b><HighlightElm text={label} searchTerm={searchTerm} /></b> <small>
<HighlightElm text={id} searchTerm={searchTerm} />
</small>
</li>
listProps: React.HTMLAttributes<HTMLLIElement>;
label: string;
id: string;
searchTerm: string;
onlyShowLabel?: boolean;
}> = ({ listProps, label, id, searchTerm, onlyShowLabel = false }) => {
let inner = (
<>
<b>
<HighlightElm text={label} searchTerm={searchTerm} />
</b>{" "}
<small>
<HighlightElm text={id} searchTerm={searchTerm} />
</small>
</>
);
if (onlyShowLabel) {
if (label) {
inner = (
<>
<b>
<HighlightElm text={label} searchTerm={searchTerm} />
</b>{" "}
</>
);
} else {
inner = (
<>
<small>
<HighlightElm text={id} searchTerm={searchTerm} />
</small>
</>
);
}
}
return (
<li
{...listProps}
className={[
listProps.className ?? "",
"input-extras--search-item--list",
].join(" ")}
>
{inner}
</li>
);
};
@@ -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<ServiceEditorFieldProps> = ({
/>
);
}
if (option.data.selector?.select) {
return (
<InputAutoComplete
className="service-editor--field"
label={label}
value={value}
onChange={onChange}
endAdornment={removeIcon}
required={isRequired}
options={option.data.selector.select.options.map<Option>((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 (
<InputText
className="service-editor--field"
@@ -79,6 +114,7 @@ export const ServiceEditorField: FC<ServiceEditorFieldProps> = ({
<InputEntity
value={value}
onChange={onChange}
className="service-editor--field"
multiple
restrictToDomain={
option.data.domain ? [option.data.domain] : undefined