input area
This commit is contained in:
1 parent
0fc3ea6146
commit
15c97b81b4
16 files changed
+699
-416
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { FC } from "react";
|
||||
import {
|
||||
InputAutoComplete,
|
||||
InputAutoCompletePropsBase,
|
||||
} from "./InputAutoComplete";
|
||||
import { useHAAreas } from "haService/AreaRegistry";
|
||||
|
||||
export type InputAreaProps = InputAutoCompletePropsBase & {
|
||||
restrictToDomain?: string[];
|
||||
};
|
||||
|
||||
export const InputArea: FC<InputAreaProps> = (props) => {
|
||||
// state
|
||||
const areas = useHAAreas();
|
||||
return (
|
||||
<InputAutoComplete
|
||||
{...props}
|
||||
validateOption={(v) => areas.validateOptions(v, props.restrictToDomain)}
|
||||
options={areas.getOptions(props.restrictToDomain)}
|
||||
getID={areas.getID}
|
||||
getLabel={areas.getLabel}
|
||||
onlyShowLabel
|
||||
/>
|
||||
);
|
||||
};
|
||||
@@ -32,4 +32,10 @@
|
||||
max-height: 5em;
|
||||
max-width: 100%;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
.input-autocomplete--chip small {
|
||||
max-width: 7em;
|
||||
text-overflow: ellipsis;
|
||||
overflow: hidden;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { computeSimilarity } from "./InputAutoComplete";
|
||||
|
||||
test("similarity score", () => {
|
||||
const searchTerms = [
|
||||
"lught",
|
||||
"light",
|
||||
"light balcn",
|
||||
"light bal",
|
||||
"light balcony",
|
||||
"Balcony lig",
|
||||
].map((x) => computeSimilarity(x, "light.balcony", "Balcony Light"));
|
||||
expect(searchTerms).toStrictEqual(searchTerms.sort());
|
||||
});
|
||||
@@ -1,5 +1,4 @@
|
||||
import "./InputAutoComplete.css";
|
||||
import { Option } from "haService";
|
||||
import Autocomplete from "@mui/material/Autocomplete";
|
||||
import TextField from "@mui/material/TextField";
|
||||
import Chip from "@mui/material/Chip";
|
||||
@@ -7,6 +6,11 @@ import { Badge } from "@mui/material";
|
||||
import { SearchItem } from "./extras";
|
||||
import { ReactNode } from "react";
|
||||
|
||||
export type BaseOption<T = {}> = {
|
||||
id: string;
|
||||
label: string;
|
||||
} & T;
|
||||
export type Option<T = {}> = string | BaseOption<T>;
|
||||
export type InputAutoCompleteProps<T extends Option<any>> = {
|
||||
validateOption?: (v: string[]) => string[] | null;
|
||||
groupBy?: (opt: T) => string;
|
||||
@@ -18,7 +22,6 @@ export type InputAutoCompleteProps<T extends Option<any>> = {
|
||||
required?: boolean;
|
||||
onlyShowLabel?: boolean;
|
||||
} & InputAutoCompletePropsBase;
|
||||
|
||||
export type InputAutoCompletePropsBase = {
|
||||
label?: string;
|
||||
className?: string;
|
||||
@@ -35,6 +38,25 @@ export type InputAutoCompletePropsBase = {
|
||||
}
|
||||
);
|
||||
|
||||
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 (lowCaseId.includes(segment)) {
|
||||
similarityScore += lowCaseLabel.length / lowCaseLabel.length;
|
||||
}
|
||||
}
|
||||
return similarityScore;
|
||||
}
|
||||
|
||||
export function InputAutoComplete<T extends Option>(
|
||||
props: InputAutoCompleteProps<T>
|
||||
) {
|
||||
@@ -120,32 +142,26 @@ export function InputAutoComplete<T extends Option>(
|
||||
onChange={onChange}
|
||||
getOptionLabel={getLabel as any}
|
||||
filterOptions={(opts, s) => {
|
||||
const searchTerm = s.inputValue.toLocaleLowerCase().trim();
|
||||
const searchTerm = s.inputValue;
|
||||
if (!searchTerm) {
|
||||
return opts;
|
||||
}
|
||||
return opts
|
||||
.map((opt) => {
|
||||
let similarityScore = 0;
|
||||
const eid = getID(opt);
|
||||
if (eid.toLocaleLowerCase().includes(searchTerm)) {
|
||||
similarityScore = Math.abs(eid.length - searchTerm.length);
|
||||
}
|
||||
const name = getLabel(opt);
|
||||
if (name.toLocaleLowerCase().includes(searchTerm)) {
|
||||
similarityScore = Math.abs(eid.length - searchTerm.length);
|
||||
}
|
||||
return {
|
||||
label: name,
|
||||
id: eid,
|
||||
domain: typeof opt === "string" ? "" : (opt as any).domain,
|
||||
similarityScore,
|
||||
opt,
|
||||
similarityScore: computeSimilarity(
|
||||
searchTerm,
|
||||
getID(opt),
|
||||
getLabel(opt)
|
||||
),
|
||||
};
|
||||
})
|
||||
.filter((opt) => opt.similarityScore > 0)
|
||||
.sort((a, b) => {
|
||||
return a.similarityScore > b.similarityScore ? 1 : -1;
|
||||
}) as any;
|
||||
return a.similarityScore < b.similarityScore ? 1 : -1;
|
||||
})
|
||||
.map(({ opt }) => opt);
|
||||
}}
|
||||
groupBy={props.groupBy}
|
||||
renderOption={(listProps, option, { inputValue }) => {
|
||||
@@ -153,14 +169,16 @@ export function InputAutoComplete<T extends Option>(
|
||||
const id = getID(option);
|
||||
|
||||
return (
|
||||
<SearchItem
|
||||
key={label + id}
|
||||
listProps={listProps}
|
||||
id={id}
|
||||
label={label}
|
||||
searchTerm={inputValue}
|
||||
onlyShowLabel={props.onlyShowLabel}
|
||||
/>
|
||||
<>
|
||||
<SearchItem
|
||||
key={label + id}
|
||||
listProps={listProps}
|
||||
id={id}
|
||||
label={label}
|
||||
searchTerm={inputValue}
|
||||
onlyShowLabel={props.onlyShowLabel}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}}
|
||||
renderInput={(params) => (
|
||||
|
||||
@@ -22,7 +22,6 @@ export default function InputAutoText({
|
||||
endAdornment,
|
||||
error,
|
||||
}: InputAutoTextProps) {
|
||||
console.log(options);
|
||||
return (
|
||||
<Autocomplete
|
||||
freeSolo
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
import { FC } from "react";
|
||||
import { useHADeviceRegistry } from 'haService';
|
||||
import { InputAutoComplete, InputAutoCompletePropsBase } from './InputAutoComplete';
|
||||
import { useHADeviceRegistry } from "haService";
|
||||
import {
|
||||
InputAutoComplete,
|
||||
InputAutoCompletePropsBase,
|
||||
} from "./InputAutoComplete";
|
||||
|
||||
export type InputDeviceProps = InputAutoCompletePropsBase & { multiple?: false };
|
||||
export type InputDeviceProps = InputAutoCompletePropsBase;
|
||||
|
||||
export const InputDevice: FC<InputDeviceProps> = props => {
|
||||
// state
|
||||
const dr = useHADeviceRegistry();
|
||||
export const InputDevice: FC<InputDeviceProps> = (props) => {
|
||||
// state
|
||||
const dr = useHADeviceRegistry();
|
||||
|
||||
return <InputAutoComplete
|
||||
{...props}
|
||||
label={props.label ?? "Device ID"}
|
||||
getID={dr.getID}
|
||||
getLabel={dr.getLabel}
|
||||
options={dr.getOptions()}
|
||||
return (
|
||||
<InputAutoComplete
|
||||
{...props}
|
||||
label={props.label ?? "Device ID"}
|
||||
getID={dr.getID}
|
||||
getLabel={dr.getLabel}
|
||||
options={dr.getOptions()}
|
||||
/>
|
||||
}
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user