v1 of service editor

This commit is contained in:
ari committed 2022-06-14 15:06:02 -04:00
1 parent 0cecae5faa
commit 7b816c3fbf
16 files changed
+1185 -435

No files matched your search

+190 -138
View File
@@ -1,152 +1,204 @@
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 { Option } from "haService";
import Autocomplete from "@mui/material/Autocomplete";
import TextField from "@mui/material/TextField";
import Chip from "@mui/material/Chip";
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;
className?: string;
validateOption?: (v: string[]) => string[] | null;
groupBy?: (opt: T) => string;
options: T[];
getLabel?: (opt: T) => string;
getID?: (opt: T) => string;
helperText?: 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>
}
label?: string;
} & (
| {
value: string | null;
multiple?: false;
onChange: (v: string | null) => void;
}
// 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];
| {
value: string | string[];
multiple: true;
onChange: (v: string[]) => void;
}
);
export function InputAutoComplete<T extends Option>(
props: InputAutoCompleteProps<T>
) {
// aliases
const getID = (opt: T): string => {
if (props.getID) {
return props.getID(opt);
}
if (typeof opt === "string") {
return opt;
}
return opt.id;
};
const getLabel = (opt: T): string => {
if (props.getLabel) {
return props.getLabel(opt);
}
if (typeof opt === "string") {
return opt;
}
return opt.label;
};
// 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 getID(a) > getID(b) ? 1 : -1;
});
// processing of errors
let helperText = props.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", props.className ?? ""].join(" ")}
multiple={true}
freeSolo
value={value}
options={options}
onChange={onChange}
getOptionLabel={getLabel as any}
filterOptions={(opts, s) => {
const searchTerm = s.inputValue.toLocaleLowerCase().trim();
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,
};
})
.filter((opt) => opt.similarityScore > 0)
.sort((a, b) => {
return a.similarityScore > b.similarityScore ? 1 : -1;
}) as any;
}}
groupBy={props.groupBy}
renderOption={(listProps, option, { inputValue }) => {
const label = getLabel(option);
const id = getID(option);
return (
<SearchItem
key={label + id}
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 = getLabel(option);
const id = 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) {
props.onChange(cleanValue);
return (
<Badge badgeContent={tagValue.length} color="info">
{inner}
</Badge>
);
} 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
}
}
return inner;
}
}}
/>
);
}
+55 -34
View File
@@ -1,20 +1,26 @@
import FormHelperText from '@mui/material/FormHelperText';
import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import Select from '@mui/material/Select';
import FormHelperText from "@mui/material/FormHelperText";
import InputLabel from "@mui/material/InputLabel";
import MenuItem from "@mui/material/MenuItem";
import FormControl from "@mui/material/FormControl";
import Select from "@mui/material/Select";
import { prettyName } from "utils/formatting";
import { ReactNode } from "react";
export interface Props<T extends string> {
current: T;
export type Props<T extends string | Object> = {
current?: T;
options: readonly T[];
onChange: (n: T) => void;
label: string;
id?: string;
className?: string;
prettyOptionLabels?: boolean;
}
export function InputList<T extends string>({
getKey?: (n: T) => string;
placeholder?: any;
title?: string;
helperText?: string;
endAdornment?: ReactNode;
};
export function InputList<T extends string | Object>({
current,
options,
onChange,
@@ -22,32 +28,47 @@ export function InputList<T extends string>({
id,
className,
prettyOptionLabels,
getKey = String,
placeholder,
title,
helperText,
endAdornment,
}: Props<T>) {
const invalidCurrent = current && !options.includes(current);
return <FormControl className={className} variant="filled" sx={{ marginRight: "0.25em", minWidth: `${label.length * 0.75}em` }}>
<InputLabel>{label}</InputLabel>
<Select
autoWidth
id={id}
value={current}
onChange={(e) => {
onChange(e.target.value as T)
}}
label={label}
const invalidCurrent =
current && !options.map(getKey).includes(getKey(current));
return (
<FormControl
className={className}
variant="filled"
placeholder={label}
error={invalidCurrent}
sx={{ marginRight: "0.25em", minWidth: `${label.length * 0.75}em` }}
>
{options.map((t, i) => (
<MenuItem
key={i}
value={t}
>
{prettyOptionLabels ? prettyName(t) : t}
</MenuItem>
))}
</Select>
<FormHelperText>{invalidCurrent ? `'${current}' is not a valid selection` : ''}</FormHelperText>
</FormControl>
<InputLabel>{label}</InputLabel>
<Select
title={title}
placeholder={placeholder ?? label}
endAdornment={endAdornment}
autoWidth
id={id}
value={current ? getKey(current) : ""}
onChange={(e) => {
const key = e.target.value;
const option = options.filter((opt) => getKey(opt) === key)[0];
onChange(option);
}}
label={label}
variant="filled"
error={invalidCurrent}
fullWidth
>
{options.map((t, i) => (
<MenuItem key={i} value={getKey(t)}>
{prettyOptionLabels ? prettyName(getKey(t)) : getKey(t)}
</MenuItem>
))}
</Select>
<FormHelperText variant="filled" error={invalidCurrent}>
{invalidCurrent ? `'${current}' is not a valid selection` : helperText}
</FormHelperText>
</FormControl>
);
}
+43 -25
View File
@@ -5,39 +5,57 @@ import InputAdornment from "@mui/material/InputAdornment";
import Edit from "@mui/icons-material/Edit";
export interface Props {
label: string;
label: string | JSX.Element;
value: number | undefined;
onChange: (v: number | undefined) => void;
className?: string;
min?: number,
max?: number,
step?: number,
min?: number;
max?: number;
step?: number;
unit?: string;
placeholder?: any;
required?: boolean;
}
export default function InputNumber({
label,
value = undefined,
onChange,
className = "",
min, max, step,
min,
max,
step,
unit,
placeholder,
required,
}: Props) {
const disabled = (value === undefined) || (value === null);
return <TextField
variant="filled"
label={label}
value={value}
defaultValue={value}
onChange={e => onChange(Number(e.target.value ?? 0))}
disabled={disabled}
className={className}
InputProps={{
endAdornment: <InputAdornment position="end">
<IconButton onClick={() => onChange(disabled ? 0 : undefined)}>
{disabled ? <Edit /> : <Close />}
</IconButton>
</InputAdornment>,
inputProps: {
min, max, type: 'number'
}
}}
/>
const disabled = value === undefined || value === null;
return (
<TextField
variant="filled"
label={label}
value={value}
defaultValue={value}
onChange={(e) => onChange(Number(e.target.value ?? 0))}
disabled={disabled}
className={className}
placeholder={placeholder}
required={required}
InputProps={{
endAdornment: (
<InputAdornment position="end">
{unit}
<IconButton onClick={() => onChange(disabled ? 0 : undefined)}>
{disabled ? <Edit /> : <Close />}
</IconButton>
</InputAdornment>
),
inputProps: {
min,
max,
type: "number",
step,
},
}}
/>
);
}
+63 -36
View File
@@ -1,11 +1,11 @@
import { PropsWithChildren, useState } from "react";
import React, { PropsWithChildren, ReactNode, useState } from "react";
import { useToolTip } from "tooltip/context";
import TextField from '@mui/material/TextField';
import TextField from "@mui/material/TextField";
import InputWrapper from "./InputWrapper";
export interface Props {
textBoxFor?: string;
label: string;
label: string | JSX.Element;
value: string;
onChange: (v: string) => void;
onEnter?: () => void;
@@ -13,50 +13,77 @@ export interface Props {
multiline?: boolean;
maxRows?: number;
className?: string;
placeholder?: any;
title?: string;
helperText?: string;
endAdornment?: ReactNode;
required?: boolean;
}
export default function InputText({
label,
title,
helperText,
textBoxFor,
value = "",
onChange,
onEnter = () => { },
endAdornment,
onEnter = () => {},
additionalTooltipFilters = {},
multiline = false,
maxRows = 4,
children,
className,
placeholder,
required,
}: PropsWithChildren<Props>) {
const tooltip = useToolTip();
const [isFocused, setIsFocused] = useState(false)
return <InputWrapper className={className} label="">
<TextField multiline={multiline} maxRows={maxRows} fullWidth inputMode="text" variant="filled" focused={isFocused} label={label} value={value}
onKeyDown={e => {
if (e.key === 'Enter') {
onEnter();
}
}}
onChange={e => {
e.preventDefault();
onChange(e.target.value)
}}
onBlur={() => {
setIsFocused(false)
}}
onFocus={e => {
setIsFocused(true)
if (textBoxFor) {
tooltip.setFocus(
e.target.getBoundingClientRect(),
{
searchObject: textBoxFor,
searchText: value,
filterObjects: additionalTooltipFilters,
},
onChange
)
}
}}
/>
{children}
</InputWrapper>
const [isFocused, setIsFocused] = useState(false);
return (
<InputWrapper className={className} label="">
<TextField
multiline={multiline}
maxRows={maxRows}
fullWidth
inputMode="text"
variant="filled"
focused={isFocused}
label={label}
value={value}
title={title}
helperText={helperText}
placeholder={placeholder}
InputProps={{
endAdornment,
}}
required={required}
onKeyDown={(e) => {
if (e.key === "Enter") {
onEnter();
}
}}
onChange={(e) => {
e.preventDefault();
onChange(e.target.value);
}}
onBlur={() => {
setIsFocused(false);
}}
onFocus={(e) => {
setIsFocused(true);
if (textBoxFor) {
tooltip.setFocus(
e.target.getBoundingClientRect(),
{
searchObject: textBoxFor,
searchText: value,
filterObjects: additionalTooltipFilters,
},
onChange
);
}
}}
/>
{children}
</InputWrapper>
);
}