moving things around

This commit is contained in:
ari committed 2022-08-05 22:04:31 -04:00
1 parent 549f52655b
commit d22bba8c4d
41 files changed
+227 -337

No files matched your search

@@ -0,0 +1,27 @@
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { useState } from "react";
import InputBoolean from "./InputBoolean";
export default {
title: 'Inputs/InputBoolean',
component: InputBoolean,
parameters: { actions: { argTypesRegex: '^on.*' } },
} as ComponentMeta<typeof InputBoolean>;
const Template: ComponentStory<typeof InputBoolean> = args => {
const [state, onChange] = useState(args.value)
return <Page>
<InputBoolean {...args} value={state} onChange={v => {
args.onChange(v)
onChange(v)
}} />
</Page>
}
export const SimpleText = Template.bind({})
SimpleText.args = {
label: "Template",
}
@@ -0,0 +1,77 @@
import Checkbox from "@mui/material/Checkbox";
import FormGroup from "@mui/material/FormGroup";
import FormControlLabel from "@mui/material/FormControlLabel";
import { ReactNode } from "react";
import FormHelperText from "@mui/material/FormHelperText";
import InputAdornment from "@mui/material/InputAdornment";
export interface Props {
label: string;
value: boolean;
title?: string;
helperText?: ReactNode;
onChange: (v: boolean) => void;
disabled?: boolean;
className?: string;
required?: boolean;
placeholder?: string;
endAdornment?: ReactNode;
}
export default function InputBoolean({
label,
value = false,
title,
helperText,
onChange,
disabled = false,
className,
required,
placeholder,
endAdornment,
}: Props) {
return (
<FormGroup
className={className}
title={title}
style={{
alignItems: "flex-start",
}}
>
<FormControlLabel
disabled={disabled}
control={
<Checkbox
checked={value}
onChange={() => onChange(!value)}
required={required}
placeholder={placeholder}
/>
}
label={
endAdornment ? (
<div
style={{
display: "flex",
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
}}
>
<span
style={{
flex: 1,
}}
>
{label}
</span>
<InputAdornment position="end">{endAdornment}</InputAdornment>
</div>
) : (
label
)
}
/>
<FormHelperText>{helperText}</FormHelperText>
</FormGroup>
);
}
@@ -0,0 +1,3 @@
.input-number .MuiButtonBase-root.icon-button[type="button"].icon-button {
border-style: none;
}
@@ -0,0 +1,32 @@
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { useState } from "react";
import InputNumber from "./InputNumber";
export default {
title: 'Inputs/InputNumber',
component: InputNumber,
parameters: { actions: { argTypesRegex: '^on.*' } },
argTypes: {
label: {
defaultValue: 'Sample Title'
}
}
} as ComponentMeta<typeof InputNumber>;
export const NullStart: ComponentStory<typeof InputNumber> = args => {
const [value, setValue] = useState<number | undefined>(undefined)
return <Page>
<InputNumber {...args} value={value} onChange={setValue} />
</Page>
}
export const SomeStuffAtStart: ComponentStory<typeof InputNumber> = args => {
const [value, setValue] = useState<number | undefined>(10)
return <Page>
<InputNumber {...args} value={value} onChange={setValue} />
</Page>
}
@@ -0,0 +1,86 @@
import "./InputNumber.css";
import Close from "@mui/icons-material/Close";
import TextField from "@mui/material/TextField";
import InputAdornment from "@mui/material/InputAdornment";
import Edit from "@mui/icons-material/Edit";
import { ReactNode } from "react";
import { ButtonIcon } from "components/Icons/ButtonIcons";
export interface Props {
label: string | JSX.Element;
value: number | undefined;
onChange: (v: number | undefined) => void;
className?: string;
min?: number;
max?: number;
step?: number;
unit?: string;
placeholder?: any;
required?: boolean;
title?: string;
helperText?: ReactNode;
endAdornment?: ReactNode;
}
export default function InputNumber({
label,
value = undefined,
onChange,
className = "",
min,
max,
step,
unit,
placeholder,
required,
endAdornment,
helperText,
title,
}: Props) {
const disabled = value === undefined || value === null;
let labelElm = <></>;
let unitElm = <></>;
if (label) {
labelElm = <span>{label}</span>;
}
if (unit) {
unitElm = <small>({unit})</small>;
}
return (
<TextField
variant="filled"
label={
<div className="input-number--label">
{labelElm} {unitElm}
</div>
}
title={title}
value={value}
onChange={(e) => onChange(Number(e.target.value ?? 0))}
disabled={disabled}
className={["input-number", className ?? ""].join(" ")}
placeholder={placeholder}
required={required}
fullWidth
helperText={helperText}
InputProps={{
endAdornment: endAdornment ? (
endAdornment
) : (
<InputAdornment position="end">
<ButtonIcon
onClick={() => onChange(disabled ? 0 : undefined)}
icon={disabled ? <Edit /> : <Close />}
/>
</InputAdornment>
),
inputProps: {
min,
max,
type: "number",
step,
},
}}
/>
);
}
@@ -0,0 +1,25 @@
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { useState } from "react";
import InputText from "./InputText";
export default {
title: 'Inputs/InputText',
component: InputText,
parameters: { actions: { argTypesRegex: '^on.*' } },
} as ComponentMeta<typeof InputText>;
const Template: ComponentStory<typeof InputText> = args => {
const [value, setValue] = useState('')
return <Page>
<InputText {...args} value={value} onChange={setValue} />
</Page>
}
export const SimpleText = Template.bind({})
SimpleText.args = {
label: "Entity ID",
}
@@ -0,0 +1,85 @@
import React, { ReactNode, useState } from "react";
import { useToolTip } from "tooltip/context";
import TextField from "@mui/material/TextField";
export interface Props {
textBoxFor?: string;
label?: string | JSX.Element;
value: string;
onChange: (v: string) => void;
onEnter?: () => void;
additionalTooltipFilters?: Record<string, string>;
multiline?: boolean;
maxRows?: number;
className?: string;
placeholder?: any;
title?: string;
helperText?: ReactNode;
endAdornment?: ReactNode;
required?: boolean;
}
export default function InputText({
label,
title,
helperText,
textBoxFor,
value = "",
onChange,
endAdornment,
onEnter = () => {},
additionalTooltipFilters = {},
multiline = false,
maxRows = 4,
className,
placeholder,
required,
}: Props) {
const tooltip = useToolTip();
const [isFocused, setIsFocused] = useState(false);
return (
<TextField
className={className}
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
);
}
}}
/>
);
}
@@ -0,0 +1,15 @@
.input-time>.input-time--inner {
display: flex;
gap: 0;
flex-direction: row;
justify-content: center;
align-items: center;
}
.input-time>.input-time--inner>.icon-button {
border: none;
}
.input-time>.input-time--inner>.MuiFormControl-root {
width: 4em;
}
@@ -0,0 +1,40 @@
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { useState } from "react";
import { AutomationTime } from "types/automations/common";
import { InputTime } from "./InputTime";
export default {
title: "Inputs/InputTime",
component: InputTime,
parameters: { actions: { argTypesRegex: "^on.*" } },
} as ComponentMeta<typeof InputTime>;
const Template: ComponentStory<typeof InputTime> = (args) => {
const [value, setValue] = useState<AutomationTime | undefined>(undefined);
return (
<Page>
<div
style={{
padding: "1em",
}}
>
<InputTime
{...args}
value={args.restrictEmpty ? "00:00:00" : value}
onChange={(t) => {
setValue(t);
args.onChange(t);
}}
/>
</div>
</Page>
);
};
export const SimpleText = Template.bind({});
SimpleText.args = {
label: "Time Stamp",
value: "10",
};
@@ -0,0 +1,154 @@
import "./InputTime.css";
import { useState, useEffect, FC } from "react";
import TextField from "@mui/material/TextField";
import EditIcon from "@mui/icons-material/Edit";
import CloseIcon from "@mui/icons-material/Close";
import { AutomationTime, AutomationTimeObject } from "types/automations/common";
import {
convertAutomationTimeToTimeObject,
convertObjectToAutomationTimeString,
convertAutomationTimeToTimeString,
} from "utils/time";
import { prettyName } from "utils/formatting";
import { ButtonIcon } from "components/Icons/ButtonIcons";
// constants
const fields: Array<keyof AutomationTimeObject> = [
"hours",
"minutes",
"seconds",
"milliseconds",
];
export type InputTimeProps = {
label: string;
onChange: (v?: AutomationTimeObject) => void;
className?: string;
value?: AutomationTime;
restrictEmpty?: boolean;
};
export const InputTime: FC<InputTimeProps> = (props) => {
// state
const [disabled, setDisabled] = useState(!props.value);
const [displayValue, setDisplayValue] = useState(
convertAutomationTimeToTimeObject(props.value)
);
// process display input
const updateOne = (k: keyof AutomationTimeObject) => (v: string) => {
if (Number(v) < 0) {
setDisplayValue({
...displayValue,
[k]: 0,
});
} else if (k === "hours") {
setDisplayValue({
...displayValue,
[k]: Number(v),
});
} else if (k === "minutes") {
let minutes = Number(v);
let { hours = 0 } = displayValue;
if (minutes > 59) {
const additonalHours = Math.floor(minutes / 60);
hours += additonalHours;
minutes = minutes - additonalHours * 60;
}
setDisplayValue({
...displayValue,
minutes,
hours,
});
} else if (k === "seconds") {
let seconds = Number(v);
let { hours = 0, minutes = 0 } = displayValue;
if (seconds > 59) {
const additonalHours = Math.floor(seconds / (60 * 60));
hours += additonalHours;
seconds = seconds - additonalHours * 60;
}
if (seconds > 59) {
const additionalMinutes = Math.floor(seconds / 60);
minutes += additionalMinutes;
seconds = seconds - additionalMinutes * 60;
}
setDisplayValue({
...displayValue,
minutes,
hours,
seconds,
});
} else if (k === "milliseconds") {
let milliseconds = Number(v);
let { hours = 0, minutes = 0, seconds = 0 } = displayValue;
if (milliseconds > 1000) {
const additonalHours = Math.floor(milliseconds / (60 * 60 * 1000));
hours += additonalHours;
milliseconds = milliseconds - additonalHours * 60 * 60 * 1000;
}
if (milliseconds > 1000) {
const additionalMinutes = Math.floor(milliseconds / (60 * 1000));
minutes += additionalMinutes;
milliseconds = milliseconds - additionalMinutes * 60 * 1000;
}
if (milliseconds > 1000) {
const additionalSeconds = Math.floor(milliseconds / 1000);
seconds += additionalSeconds;
milliseconds = milliseconds - additionalSeconds * 1000;
}
setDisplayValue({
...displayValue,
minutes,
hours,
seconds,
milliseconds,
});
}
};
// effect
useEffect(() => {
if (disabled && !props.value) {
return;
}
if (disabled) {
props.onChange(undefined);
} else {
const pVal = convertAutomationTimeToTimeString(props.value);
const cVal = convertObjectToAutomationTimeString(displayValue);
if (pVal !== cVal) {
props.onChange(displayValue);
}
}
// eslint-disable-next-line
}, [disabled, displayValue]);
return (
<div className={["input-time", props.className ?? ""].join(" ")}>
{/* <InputLabel >{props.label}</InputLabel> */}
<div className="input-time--inner">
{fields.map((name) => (
<TextField
key={name}
variant="filled"
label={prettyName(name)}
defaultValue="00"
InputProps={{
disabled,
type: "number",
title: prettyName(name),
}}
value={displayValue[name] ?? 0}
onChange={(e) => updateOne(name)(e.target.value)}
/>
))}
{!props.restrictEmpty && (
<ButtonIcon
onClick={() => setDisabled(!disabled)}
icon={disabled ? <EditIcon /> : <CloseIcon />}
/>
)}
</div>
</div>
);
};
@@ -0,0 +1,41 @@
.input-yaml {
align-items: flex-start;
position: relative;
}
.input-yaml label {
position: initial;
margin-bottom: 1em;
}
.input-yaml [lang=yaml] {
flex: 1;
width: 100%;
min-width: 100%;
}
.input-yaml .MuiFormHelperText-root {
width: 100%;
max-width: 100%;
display: block;
overflow: hidden;
margin: 0;
padding: 0.25em 0.1em;
}
.input-yaml .MuiInputAdornment-positionEnd {
position: absolute;
right: 0.5em;
top: 3.5em;
}
.input-yaml .input-yaml--placeholder {
position: absolute;
left: 2em;
top: 2.5em;
color: var(--mui-grey-400);
}
.input-yaml .MuiButtonBase-root.icon-button[type="button"].icon-button {
border-style: none;
}
@@ -0,0 +1,27 @@
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { useState } from "react";
import InputYaml from "./InputYaml";
export default {
title: 'Inputs/InputYaml',
component: InputYaml,
parameters: { actions: { argTypesRegex: '^on.*' } },
} as ComponentMeta<typeof InputYaml>;
const Template: ComponentStory<typeof InputYaml> = args => {
const [value, setValue] = useState({
"test": "data"
})
return <Page>
<InputYaml {...args} value={value} onChange={setValue} />
</Page>
}
export const SimpleText = Template.bind({})
SimpleText.args = {
label: "Template",
}
@@ -0,0 +1,111 @@
import "./InputYaml.css";
import * as yaml from "js-yaml";
import { useEffect, useState, ReactNode } from "react";
import CodeMirror from "@uiw/react-codemirror";
import FormControl from "@mui/material/FormControl";
import InputLabel from "@mui/material/InputLabel";
import FormHelperText from "@mui/material/FormHelperText";
import InputAdornment from "@mui/material/InputAdornment";
import { Save } from "@mui/icons-material";
import { ButtonIcon } from "components/Icons/ButtonIcons";
export interface Props<T extends {}> {
label: string;
value: T;
onChange: (v: T) => void;
error?: JSX.Element | string;
className?: string;
placeholder?: any;
required?: boolean;
title?: string;
helperText?: ReactNode;
endAdornment?: ReactNode;
}
export default function InputYaml<T extends {}>({
label,
error: incmError,
value = {} as any,
onChange,
className,
placeholder,
required,
title,
helperText,
endAdornment,
}: Props<T>) {
const originalText = yaml.dump(value);
const [{ text, error }, setState] = useState<{
text: string;
error: undefined | JSX.Element | string;
}>({
text: originalText,
error: incmError,
});
const hasChanged = text !== originalText;
const setText = (t: string) => {
try {
yaml.load(t);
setState({ text: t, error: undefined });
} catch (_) {
setState({ text: t, error: "! Invalid Yaml !" });
}
};
const onSave = () => {
if (!error && yaml.dump(value) !== text) {
if (text) {
onChange(yaml.load(text) as any);
} else if (Array.isArray(value)) {
onChange([] as any);
} else {
onChange({} as any);
}
}
};
useEffect(() => {
setState({
text: yaml.dump(value),
error: incmError,
});
}, [value, incmError]);
return (
<FormControl
className={["input-yaml", className ?? ""].join(" ")}
fullWidth
title={title}
>
<InputLabel>
{label} {required ? "*" : ""}
</InputLabel>
<CodeMirror
theme={"dark"}
value={text}
lang="yaml"
onChange={setText}
maxWidth="100%"
width="100%"
minHeight="3.5em"
minWidth="100%"
/>
<span className="input-yaml--placeholder">
{!!text ? "" : placeholder}
</span>
<InputAdornment position="end">
<ButtonIcon
disabled={!hasChanged}
onClick={() => onSave()}
title={!!error ? "Invalid YAML" : hasChanged ? "Save" : ""}
icon={
<Save
color={hasChanged ? (!!error ? "error" : "success") : "disabled"}
/>
}
/>
{endAdornment}
</InputAdornment>
<FormHelperText variant="filled" error={!!error}>
{error ? error : helperText}
</FormHelperText>
</FormControl>
);
}