device action builder
This commit is contained in:
1 parent
6be6005531
commit
029df7932e
16 files changed
+725
-434
No files matched your search
@@ -1,22 +1,48 @@
|
||||
import { FC } from "react";
|
||||
import { useHAEntities } from 'haService';
|
||||
import { InputAutoComplete, InputAutoCompletePropsBase } from './InputAutoComplete';
|
||||
import { useHAEntities } from "haService";
|
||||
import {
|
||||
InputAutoComplete,
|
||||
InputAutoCompletePropsBase,
|
||||
} from "./InputAutoComplete";
|
||||
import { prettyName } from "utils/formatting";
|
||||
|
||||
export type InputEntityProps = InputAutoCompletePropsBase & {
|
||||
restrictToDomain?: string[],
|
||||
}
|
||||
restrictToDomain?: string[];
|
||||
preSelectedEntityIds?: string[];
|
||||
};
|
||||
|
||||
export const InputEntity: FC<InputEntityProps> = props => {
|
||||
// state
|
||||
const entities = useHAEntities();
|
||||
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) : ''}
|
||||
export const InputEntity: FC<InputEntityProps> = (props) => {
|
||||
// state
|
||||
const entities = useHAEntities();
|
||||
let options = entities.getOptions(props.restrictToDomain);
|
||||
if (props.preSelectedEntityIds) {
|
||||
options = options.filter((opt) =>
|
||||
props.preSelectedEntityIds?.includes(entities.getID(opt))
|
||||
);
|
||||
}
|
||||
const validateOptions = (v: string[]): null | string[] => {
|
||||
const errors: string[] = [];
|
||||
if (props.preSelectedEntityIds) {
|
||||
v.forEach((eid) => {
|
||||
if (!props.preSelectedEntityIds?.includes(eid)) {
|
||||
errors.push(`"${eid}" is not a valid selection`);
|
||||
}
|
||||
});
|
||||
}
|
||||
errors.concat(entities.validateOptions(v, props.restrictToDomain) ?? []);
|
||||
if (errors.length > 0) {
|
||||
return errors;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
return (
|
||||
<InputAutoComplete
|
||||
{...props}
|
||||
validateOption={validateOptions}
|
||||
options={options}
|
||||
getID={entities.getID}
|
||||
getLabel={entities.getLabel}
|
||||
groupBy={(opt) => (typeof opt !== "string" ? prettyName(opt.domain) : "")}
|
||||
/>
|
||||
}
|
||||
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user