functional modal

This commit is contained in:
ari committed 2021-12-15 13:18:42 -05:00
1 parent 48aae0bf98
commit 90d4fbf8f6
12 files changed
+80 -88

No files matched your search

@@ -19,7 +19,7 @@ export const ActionChooseState: OptionManager<ChooseAction> = {
},
renderOptionList: (state, setState) => {
const update = updateActionData(state, setState);
return <div className="state-manager-options">
return <>
<InputText
label="Description"
value={state.action_data.alias ?? ""}
@@ -35,6 +35,6 @@ export const ActionChooseState: OptionManager<ChooseAction> = {
value={state.action_data.default}
onChange={d => update({ default: d })}
/>
</div>
</>
}
}
@@ -32,7 +32,7 @@ export const ActionDeviceState: OptionManager<DeviceAction> = {
),
renderOptionList: (state, setState) => {
const update = updateActionData(state, setState);
return <div className="state-manager-options">
return <>
<InputText
label="Description"
value={state.action_data.alias ?? ""}
@@ -56,6 +56,6 @@ export const ActionDeviceState: OptionManager<DeviceAction> = {
value={state.action_data.entity_id ?? ""}
onChange={entity_id => update({ entity_id, domain: entity_id.split('.')[0] })}
/>
</div>
</>
}
}
@@ -20,7 +20,7 @@ export const ActionEventState: OptionManager<FireEventAction> = {
),
renderOptionList: (state, setState) => {
const update = updateActionData(state, setState);
return <div className="state-manager-options">
return <>
<InputText
label="Description"
value={state.action_data.alias ?? ""}
@@ -37,6 +37,6 @@ export const ActionEventState: OptionManager<FireEventAction> = {
value={state.action_data.event_data}
onChange={event_data => update({ event_data })}
/>
</div>
</>
}
}
@@ -26,7 +26,7 @@ export const ActionRepeatState: OptionManager<RepeatAction> = {
),
renderOptionList: (state, setState) => {
const update = updateActionData(state, setState);
return <div className="state-manager-options">
return <>
<InputText
label="Description"
value={state.action_data.alias ?? ""}
@@ -42,6 +42,6 @@ export const ActionRepeatState: OptionManager<RepeatAction> = {
value={state.action_data.repeat.sequence ?? []}
onChange={sequence => update({ repeat: { ...state.action_data.repeat, sequence } })}
/>
</div>
</>
}
}
@@ -23,7 +23,7 @@ export const ActionCallServiceState: OptionManager<ServiceAction> = {
renderOptionList: (state, setState) => {
const { service, alias = "", target } = state.action_data;
const update = updateActionData(state, setState);
return <div className="state-manager-options">
return <>
<InputText
label="Description"
value={alias}
@@ -40,6 +40,6 @@ export const ActionCallServiceState: OptionManager<ServiceAction> = {
value={target}
onChange={target => update({ target })}
/>
</div>
</>
}
}
@@ -23,10 +23,10 @@ export const ActionWaitState: OptionManager<WaitAction> = {
),
renderOptionList: (state, setState) => {
const update = updateActionData(state, setState);
return <div className="state-manager-options">
return <>
<InputText
label="Description"
value={state.action_data.alias}
value={state.action_data.alias ?? ""}
onChange={alias => update({ alias })}
/>
<InputText
@@ -45,6 +45,6 @@ export const ActionWaitState: OptionManager<WaitAction> = {
value={state.action_data.continue_on_timeout ?? false}
onChange={continue_on_timeout => update({ continue_on_timeout })}
/>
</div>
</>
}
}
@@ -13,11 +13,9 @@ export const ConditionLogicState: OptionManager<AutomationCondition> = {
}
}),
isReady: _ => true,
renderOptionList: (state, setState) => <div className="state-manager-options">
<ConditionEditor
condition={state}
onUpdate={setState}
onDelete={() => { }}
/>
</div>
renderOptionList: (state, setState) => <ConditionEditor
condition={state}
onUpdate={setState}
onDelete={() => { }}
/>
}
@@ -4,7 +4,7 @@ import { OptionManager } from "./OptionManager";
export const Generic: OptionManager<any> = {
defaultState: () => ({}),
isReady: _ => true,
renderOptionList: () => <div className="state-manager-options">
renderOptionList: () => <>
<b>THIS IS NOT SUPPORTED YET!</b>
</div>
</>
}
@@ -17,6 +17,9 @@ export const useEditorNodeState = (node: AutomationNode) => {
nodeType,
subType,
subTypes,
get data() {
return state
},
renderOptionList() {
return optionManager.renderOptionList(state, setState);
},
@@ -3,13 +3,26 @@ import { ComponentMeta, ComponentStory } from '@storybook/react';
import { NodeEditor } from ".";
import { usePageTheme } from "styles/page";
import { AutomationCondition } from "types/automations/conditions";
import { Button } from "components/Inputs/Button";
import { useState } from 'react';
import { Modal } from "components/Modal";
export default {
title: 'NodeEditor',
component: NodeEditor,
parameters: { actions: { argTypesRegex: '^on.*' } },
args: {}
args: {
node: {
$smType: 'action',
action: 'service',
action_data: {
'data': {},
'service': '',
'target': '',
}
}
}
} as ComponentMeta<typeof NodeEditor>
const Base: ComponentStory<typeof NodeEditor> = props => {
@@ -19,21 +32,9 @@ const Base: ComponentStory<typeof NodeEditor> = props => {
</div>
};
export const Action = Base.bind({})
Action.args = {
...Action.args,
node: {
$smType: 'action',
action: 'service',
action_data: {
'data': {},
'service': '',
'target': '',
}
}
}
export const SingleOption = Action.bind({})
SingleOption.args = {
...Action.args,
...SingleOption.args,
allowedTypes: ['action'],
}
export const Condition = Action.bind({})
@@ -66,3 +67,15 @@ Condition.args = {
}
} as AutomationCondition,
}
export const InAModal: ComponentStory<typeof NodeEditor> = props => {
const { classes } = usePageTheme({});
const [open, setOpen] = useState(false);
return <div className={classes.page}>
<Button onClick={() => setOpen(!open)}>Open Editor</Button>
<Modal open={open}>
<NodeEditor {...props} onClose={() => setOpen(!open)} />
</Modal>
</div>
};
+27 -24
View File
@@ -12,40 +12,43 @@ import { Button } from "components/Inputs/Button";
export interface Props {
node: AutomationNode
allowedTypes?: Array<keyof AutomationNodeMapping>;
onClose?: () => void;
onSave?: (n: AutomationNode) => void;
}
export const NodeEditor: FC<Props> = ({
node,
allowedTypes = ["action", "condition", "trigger"]
allowedTypes = ["action", "condition", "trigger"],
onClose = () => { },
onSave = () => { },
}) => {
const { classes } = useNodeEditorStyles({});
const state = useEditorNodeState(node);
return <div className={classes.root}>
<div className={classes.background} />
<div className={classes.inner}>
<div className={classes.title}>
<div className={classes.title}>
</div>
<div className={classes.body}>
{allowedTypes.length > 1 ? <InputList
label="Type"
current={state.nodeType}
options={allowedTypes}
onChange={state.setNodeType}
/> : <span>{state.nodeType}</span>}
{state.nodeType !== 'condition' ? <InputList
label={`${state.nodeType} type`}
current={state.subType as any}
options={state.subTypes}
onChange={state.setSubType}
/> : <></>}
{state.renderOptionList()}
</div>
<div className={classes.footer}>
<Button>Close</Button>
<Button disabled={!state.isReady()}>Save</Button>
</div>
</div>
<div className={classes.body}>
{allowedTypes.length > 1 ? <InputList
label="Type"
current={state.nodeType}
options={allowedTypes}
onChange={state.setNodeType}
/> : <span>{state.nodeType}</span>}
{state.nodeType !== 'condition' ? <InputList
label={`${state.nodeType} type`}
current={state.subType as any}
options={state.subTypes}
onChange={state.setSubType}
/> : <></>}
{state.renderOptionList()}
</div>
<div className={classes.footer}>
<Button onClick={onClose}>Close</Button>
<Button onClick={() => {
state.isReady() && onSave(state.data)
}} disabled={!state.isReady()}>Save</Button>
</div>
</div>
}
+3 -28
View File
@@ -5,36 +5,10 @@ export const useNodeEditorStyles = createAppUseStyles<{
}>(theme => ({
root: {
position: 'absolute',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: '100vw',
height: '100vh',
left: '0',
top: '0',
},
background: {
position: 'absolute',
zIndex: 0,
width: '100vw',
height: '100vw',
backgroundColor: Color(theme.secondary).alpha(0.5).hex(),
},
inner: {
zIndex: 1,
maxWidth: '80%',
maxHeight: '60%',
minHeight: '200px',
minWidth: '200px',
borderRadius: '20px',
padding: '15px',
display: 'flex',
flexDirection: 'column',
backgroundColor: theme.primary,
borderSize: '1px',
borderStyle: 'solid',
borderColor: theme.primaryAccent,
justifyContent: 'center',
alignItems: 'center',
},
title: {
textAlign: 'center',
@@ -52,5 +26,6 @@ export const useNodeEditorStyles = createAppUseStyles<{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
width: '100%',
}
}))