service autocomplete
This commit is contained in:
1 parent
78494ee74d
commit
4c3b34195a
14 files changed
+437
-274
No files matched your search
+7
-7
@@ -1,26 +1,26 @@
|
||||
.input-entities .BaseBadge-root {
|
||||
.input-autocomplete .BaseBadge-root {
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.input-entities--chip>span {
|
||||
.input-autocomplete--chip>span {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
gap: 0.15em;
|
||||
}
|
||||
|
||||
.input-entities--chip>span>small::before {
|
||||
.input-autocomplete--chip>span>small::before {
|
||||
content: "/ ";
|
||||
}
|
||||
|
||||
.input-entities--chip>span>small {
|
||||
.input-autocomplete--chip>span>small {
|
||||
size: 0.15em;
|
||||
}
|
||||
|
||||
.input-entities--errors {
|
||||
.input-autocomplete--errors {
|
||||
margin-top: 0.75em;
|
||||
}
|
||||
|
||||
.input-entities--errors ul {
|
||||
.input-autocomplete--errors ul {
|
||||
padding: 0 0 0 1em;
|
||||
margin: 0;
|
||||
max-height: 2.5em;
|
||||
@@ -28,7 +28,7 @@
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
.input-entities--tags {
|
||||
.input-autocomplete--tags {
|
||||
max-height: 5em;
|
||||
max-width: 100%;
|
||||
overflow: auto;
|
||||
@@ -0,0 +1,153 @@
|
||||
import "./InputAutoComplete.css";
|
||||
import { FC } from "react";
|
||||
import { Option } from 'haService';
|
||||
import Autocomplete from '@mui/material/Autocomplete';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import Chip from '@mui/material/Chip';
|
||||
import { prettyName } from "utils/formatting";
|
||||
import { Badge } from "@mui/material";
|
||||
import { SearchItem } from "./extras";
|
||||
|
||||
export type InputAutoCompleteProps<T extends Option<any>> = {
|
||||
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;
|
||||
} & ({
|
||||
value: string | null;
|
||||
multiple?: false;
|
||||
onChange: (v: string | null) => void;
|
||||
} | {
|
||||
value: string | string[];
|
||||
multiple: true;
|
||||
onChange: (v: string[]) => void;
|
||||
})
|
||||
|
||||
export const InputAutoComplete: FC<InputAutoCompleteProps<any>> = props => {
|
||||
// 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.sort((a, b) => {
|
||||
return props.getID(a) > props.getID(b) ? 1 : -1;
|
||||
});
|
||||
|
||||
// processing of errors
|
||||
let helperText = <></>;
|
||||
let error: boolean = false;
|
||||
if (errors !== null) {
|
||||
error = true
|
||||
if (errors.length > 0) {
|
||||
helperText = <Badge
|
||||
className="input-autocomplete--errors"
|
||||
badgeContent={errors.length} color="error"
|
||||
>
|
||||
<ul title={errors.join(', and')}>{errors.map((msg, i) => <li key={i}>
|
||||
{msg}
|
||||
</li>)}</ul>
|
||||
</Badge>
|
||||
}
|
||||
}
|
||||
// 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 <Autocomplete
|
||||
className="input-autocomplete"
|
||||
multiple={true}
|
||||
freeSolo
|
||||
value={value}
|
||||
options={options}
|
||||
onChange={onChange}
|
||||
getOptionLabel={props.getLabel}
|
||||
filterOptions={(opts, s) => {
|
||||
const searchTerm = s.inputValue.toLocaleLowerCase().trim();
|
||||
if (!searchTerm) {
|
||||
return opts;
|
||||
}
|
||||
return opts.map(opt => {
|
||||
let similarityScore = 0;
|
||||
const eid = props.getID(opt);
|
||||
if (eid.toLocaleLowerCase().includes(searchTerm)) {
|
||||
similarityScore = Math.abs(eid.length - searchTerm.length)
|
||||
}
|
||||
const name = props.getLabel(opt);
|
||||
if (name.toLocaleLowerCase().includes(searchTerm)) {
|
||||
similarityScore = Math.abs(eid.length - searchTerm.length)
|
||||
}
|
||||
return {
|
||||
label: name,
|
||||
id: eid,
|
||||
domain: (typeof opt === 'string') ? '' : opt.domain,
|
||||
similarityScore
|
||||
}
|
||||
}).filter(opt => opt.similarityScore > 0).sort((a, b) => {
|
||||
return (a.similarityScore > b.similarityScore) ? 1 : -1
|
||||
})
|
||||
}}
|
||||
groupBy={props.groupBy}
|
||||
renderOption={(listProps, option, { inputValue }) => {
|
||||
const label = props.getLabel(option);
|
||||
const id = props.getID(option);
|
||||
|
||||
return <SearchItem
|
||||
listProps={listProps}
|
||||
id={id}
|
||||
label={label}
|
||||
searchTerm={inputValue}
|
||||
/>
|
||||
}}
|
||||
renderInput={params => <TextField
|
||||
{...params}
|
||||
variant="filled"
|
||||
label={props.label ?? "Entity ID"}
|
||||
helperText={helperText}
|
||||
error={error}
|
||||
/>}
|
||||
renderTags={(tagValue, getTagProps) => {
|
||||
const inner = <div className="input-autocomplete--tags">
|
||||
{tagValue.map((option, index) => {
|
||||
const label = props.getLabel(option);
|
||||
const id = props.getID(option);
|
||||
const tagProps = getTagProps({ index });
|
||||
tagProps.className += ' input-autocomplete--chip'
|
||||
return (
|
||||
<Chip
|
||||
size="medium"
|
||||
label={<>
|
||||
{(id === label) ? <span>{label}</span> : <b>{label}</b>}
|
||||
{(id !== label) && <small>{id}</small>}
|
||||
</>}
|
||||
{...tagProps}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
if (props.multiple) {
|
||||
return <Badge badgeContent={tagValue.length} color="info">
|
||||
{inner}
|
||||
</Badge>
|
||||
} else {
|
||||
return inner
|
||||
}
|
||||
}
|
||||
}
|
||||
/>
|
||||
}
|
||||
|
||||
@@ -32,5 +32,5 @@ export const Single = Template.bind({})
|
||||
Single.args = {
|
||||
...Single.args,
|
||||
multiple: false,
|
||||
value: 'sensor.bathroom'
|
||||
value: 'sensor.humidity_bathroom'
|
||||
} as any
|
||||
@@ -1,117 +1,18 @@
|
||||
import "./InputEntities.css";
|
||||
import { FC } from "react";
|
||||
import { useHADeviceRegistry } from 'haService';
|
||||
import Autocomplete from '@mui/material/Autocomplete';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import Chip from '@mui/material/Chip';
|
||||
import { EntityId } from 'types/automations/common';
|
||||
import { Badge } from "@mui/material";
|
||||
import { SearchItem } from "./extras";
|
||||
import { prettyName } from 'utils/formatting';
|
||||
import { InputAutoComplete, InputAutoCompletePropsBase } from './InputAutoComplete';
|
||||
|
||||
export type InputEntityProps = {
|
||||
label?: string,
|
||||
} & ({
|
||||
value: string | null;
|
||||
multiple?: false;
|
||||
onChange: (v: string | null) => void;
|
||||
} | {
|
||||
value: EntityId;
|
||||
multiple: true;
|
||||
onChange: (v: string[]) => void;
|
||||
})
|
||||
export type InputDeviceProps = InputAutoCompletePropsBase & { multiple?: false };
|
||||
|
||||
export const InputDevice: FC<InputEntityProps> = props => {
|
||||
export const InputDevice: FC<InputDeviceProps> = props => {
|
||||
// state
|
||||
const dr = useHADeviceRegistry();
|
||||
|
||||
// processing of inputs
|
||||
const options = dr.getOptions();
|
||||
const value = Array.isArray(props.value) ? props.value : !!props.value ? [props.value] : [];
|
||||
|
||||
// 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 <Autocomplete
|
||||
className="input-entities"
|
||||
multiple={true}
|
||||
freeSolo
|
||||
value={value}
|
||||
options={options}
|
||||
onChange={onChange}
|
||||
getOptionLabel={dr.getLabel}
|
||||
groupBy={opt => (typeof opt !== 'string') ? prettyName(opt.manufacturer) : ''}
|
||||
filterOptions={(opts, s) => {
|
||||
const searchTerm = s.inputValue.toLocaleLowerCase();
|
||||
return opts.map(opt => {
|
||||
let similarityScore = 0;
|
||||
const eid = dr.getID(opt);
|
||||
if (eid.toLocaleLowerCase().includes(searchTerm)) {
|
||||
similarityScore = Math.abs(eid.length - searchTerm.length)
|
||||
}
|
||||
const name = dr.getLabel(opt);
|
||||
if (name.toLocaleLowerCase().includes(searchTerm)) {
|
||||
similarityScore = Math.abs(eid.length - searchTerm.length)
|
||||
}
|
||||
return {
|
||||
label: name,
|
||||
id: eid,
|
||||
similarityScore,
|
||||
manufacturer: (typeof opt === 'string') ? '' : opt.manufacturer ?? ""
|
||||
}
|
||||
}).filter(opt => opt.similarityScore > 0).sort((a, b) => {
|
||||
return (a.similarityScore > b.similarityScore) ? 1 : -1
|
||||
})
|
||||
}}
|
||||
renderOption={(prop, option, { inputValue }) => {
|
||||
const label = dr.getLabel(option);
|
||||
const id = dr.getID(option);
|
||||
|
||||
return <SearchItem
|
||||
listProps={prop}
|
||||
id={id}
|
||||
label={label}
|
||||
searchTerm={inputValue}
|
||||
/>
|
||||
}}
|
||||
renderInput={params => <TextField
|
||||
{...params}
|
||||
variant="filled"
|
||||
label={props.label ?? "Device"}
|
||||
/>}
|
||||
renderTags={(tagValue, getTagProps) => <Badge badgeContent={tagValue.length} color="info">
|
||||
<div className="input-entities--tags">
|
||||
{tagValue.map((option, index) => {
|
||||
const label = dr.getLabel(option);
|
||||
const id = dr.getID(option);
|
||||
const props = getTagProps({ index });
|
||||
props.className += ' input-entities--chip'
|
||||
return (
|
||||
<Chip
|
||||
size="medium"
|
||||
label={<>
|
||||
{(id === label) ? <span>{label}</span> : <b>{label}</b>}
|
||||
{(id !== label) && <small>{id}</small>}
|
||||
</>}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</Badge>
|
||||
}
|
||||
return <InputAutoComplete
|
||||
{...props}
|
||||
label={props.label ?? "Device ID"}
|
||||
getID={dr.getID}
|
||||
getLabel={dr.getLabel}
|
||||
options={dr.getOptions()}
|
||||
/>
|
||||
}
|
||||
@@ -1,138 +1,22 @@
|
||||
import "./InputEntities.css";
|
||||
import { FC } from "react";
|
||||
import { useHAEntities } from 'haService';
|
||||
import Autocomplete from '@mui/material/Autocomplete';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import Chip from '@mui/material/Chip';
|
||||
import { InputAutoComplete, InputAutoCompletePropsBase } from './InputAutoComplete';
|
||||
import { prettyName } from "utils/formatting";
|
||||
import { EntityId } from 'types/automations/common';
|
||||
import { Badge } from "@mui/material";
|
||||
import { SearchItem } from "./extras";
|
||||
|
||||
export type InputEntityProps = {
|
||||
export type InputEntityProps = InputAutoCompletePropsBase & {
|
||||
restrictToDomain?: string[],
|
||||
label?: string,
|
||||
} & ({
|
||||
value: string | null;
|
||||
multiple?: false;
|
||||
onChange: (v: string | null) => void;
|
||||
} | {
|
||||
value: EntityId;
|
||||
multiple: true;
|
||||
onChange: (v: string[]) => void;
|
||||
})
|
||||
}
|
||||
|
||||
export const InputEntity: FC<InputEntityProps> = props => {
|
||||
// state
|
||||
const entities = useHAEntities();
|
||||
|
||||
// processing of inputs
|
||||
const options = entities.getOptions(props.restrictToDomain);
|
||||
const value = Array.isArray(props.value) ? props.value : !!props.value ? [props.value] : [];
|
||||
const errors = entities.validateOptions(value, props.restrictToDomain);
|
||||
|
||||
// processing of errors
|
||||
let helperText = <></>;
|
||||
let error: boolean = false;
|
||||
if (errors !== null) {
|
||||
error = true
|
||||
if (errors.length > 0) {
|
||||
helperText = <Badge
|
||||
className="input-entities--errors"
|
||||
badgeContent={errors.length} color="error"
|
||||
>
|
||||
<ul title={errors.join(', and')}>{errors.map((msg, i) => <li key={i}>
|
||||
{msg}
|
||||
</li>)}</ul>
|
||||
</Badge>
|
||||
}
|
||||
}
|
||||
// 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 <Autocomplete
|
||||
className="input-entities"
|
||||
multiple={true}
|
||||
freeSolo
|
||||
value={value}
|
||||
options={options}
|
||||
onChange={onChange}
|
||||
getOptionLabel={entities.getLabel}
|
||||
filterOptions={(opts, s) => {
|
||||
const searchTerm = s.inputValue.toLocaleLowerCase();
|
||||
return opts.map(opt => {
|
||||
let similarityScore = 0;
|
||||
const eid = entities.getID(opt);
|
||||
if (eid.toLocaleLowerCase().includes(searchTerm)) {
|
||||
similarityScore = Math.abs(eid.length - searchTerm.length)
|
||||
}
|
||||
const name = entities.getLabel(opt);
|
||||
if (name.toLocaleLowerCase().includes(searchTerm)) {
|
||||
similarityScore = Math.abs(eid.length - searchTerm.length)
|
||||
}
|
||||
return {
|
||||
label: name,
|
||||
id: eid,
|
||||
domain: (typeof opt === 'string') ? '' : opt.domain,
|
||||
similarityScore
|
||||
}
|
||||
}).filter(opt => opt.similarityScore > 0).sort((a, b) => {
|
||||
return (a.similarityScore > b.similarityScore) ? 1 : -1
|
||||
})
|
||||
}}
|
||||
return <InputAutoComplete
|
||||
{...props}
|
||||
validateOption={v => entities.validateOptions(v, props.restrictToDomain)}
|
||||
options={entities.getOptions(props.restrictToDomain)}
|
||||
getID={entities.getID}
|
||||
getLabel={entities.getLabel}
|
||||
groupBy={opt => (typeof opt !== 'string') ? prettyName(opt.domain) : ''}
|
||||
renderOption={(prop, option, { inputValue }) => {
|
||||
const label = entities.getLabel(option);
|
||||
const id = entities.getID(option);
|
||||
|
||||
return <SearchItem
|
||||
listProps={prop}
|
||||
id={id}
|
||||
label={label}
|
||||
searchTerm={inputValue}
|
||||
/>
|
||||
}}
|
||||
renderInput={params => <TextField
|
||||
{...params}
|
||||
variant="filled"
|
||||
label={props.label ?? "Entity ID"}
|
||||
helperText={helperText}
|
||||
error={error}
|
||||
/>}
|
||||
renderTags={(tagValue, getTagProps) => <Badge badgeContent={tagValue.length} color="info">
|
||||
<div className="input-entities--tags">
|
||||
{tagValue.map((option, index) => {
|
||||
const label = entities.getLabel(option);
|
||||
const id = entities.getID(option);
|
||||
const props = getTagProps({ index });
|
||||
props.className += ' input-entities--chip'
|
||||
return (
|
||||
<Chip
|
||||
size="medium"
|
||||
label={<>
|
||||
{(id === label) ? <span>{label}</span> : <b>{label}</b>}
|
||||
{(id !== label) && <small>{id}</small>}
|
||||
</>}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</Badge>
|
||||
}
|
||||
/>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { useState } from "react";
|
||||
|
||||
import { InputService } from "./InputService";
|
||||
|
||||
export default {
|
||||
title: 'Inputs/InputService',
|
||||
component: InputService,
|
||||
parameters: { actions: { argTypesRegex: '^on.*' } },
|
||||
argTypes: {}
|
||||
} as ComponentMeta<typeof InputService>;
|
||||
|
||||
|
||||
|
||||
const Template: ComponentStory<typeof InputService> = args => {
|
||||
const [value, setValue] = useState(args.value)
|
||||
return <Page>
|
||||
<div style={{
|
||||
maxWidth: 200
|
||||
}}>
|
||||
<InputService {...args} value={value as any} onChange={(v: any) => {
|
||||
setValue(v)
|
||||
args.onChange(v as any)
|
||||
}} />
|
||||
</div>
|
||||
</Page>
|
||||
|
||||
}
|
||||
|
||||
export const Single = Template.bind({})
|
||||
Single.args = {
|
||||
...Single.args,
|
||||
multiple: false,
|
||||
value: ''
|
||||
} as any
|
||||
@@ -0,0 +1,19 @@
|
||||
import { FC } from "react";
|
||||
import { ServiceOption, useHAServices } from 'haService';
|
||||
import { InputAutoComplete, InputAutoCompletePropsBase } from './InputAutoComplete';
|
||||
import { prettyName } from 'utils/formatting';
|
||||
|
||||
export type InputServiceProps = InputAutoCompletePropsBase & { multiple?: false };
|
||||
|
||||
export const InputService: FC<InputServiceProps> = props => {
|
||||
const services = useHAServices();
|
||||
return <InputAutoComplete
|
||||
{...props}
|
||||
label={props.label ?? "Service"}
|
||||
options={services.getOptions()}
|
||||
getID={services.getID}
|
||||
getLabel={services.getLabel}
|
||||
groupBy={opt => (typeof opt !== 'string') ? prettyName(opt.domain) : ''}
|
||||
/>
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { InputService } from "components/Inputs/InputService";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import InputYaml from 'components/Inputs/InputYaml';
|
||||
import { ServiceAction } from "types/automations/actions";
|
||||
@@ -19,11 +20,9 @@ export const ActionCallServiceState: OptionManager<ServiceAction> = {
|
||||
const { service, target, data } = state;
|
||||
const update = updateActionData(state, setState);
|
||||
return <>
|
||||
<InputText
|
||||
textBoxFor="service"
|
||||
label="Service"
|
||||
<InputService
|
||||
value={service}
|
||||
onChange={service => update({ service })}
|
||||
onChange={(service: string | null) => update({ service: service ?? "" })}
|
||||
/>
|
||||
<InputYaml
|
||||
label="Target"
|
||||
|
||||
@@ -84,3 +84,14 @@ export const InAModal: ComponentStory<typeof NodeEditor> = props => {
|
||||
</Modal>
|
||||
</Page>
|
||||
};
|
||||
|
||||
|
||||
export const ServiceExample = Action.bind({})
|
||||
ServiceExample.args = {
|
||||
...ServiceExample.args,
|
||||
node: {
|
||||
"target": {},
|
||||
"data": {},
|
||||
"service": "",
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user