diff --git a/src/AddConstraint.tsx b/src/AddConstraint.tsx new file mode 100644 index 0000000..2c35dfd --- /dev/null +++ b/src/AddConstraint.tsx @@ -0,0 +1,35 @@ +import React, { useContext, useState } from "react"; +import { Dialog, DialogTitle, DialogContent, DialogActions, Button, TextField } from "@material-ui/core"; +import { LayoutContext } from "./LayoutContext"; + +export const AddConstraint = () => { + const { addingComponent, toggleAddingComponent, addComponent } = useContext(LayoutContext); + const [name, setName] = useState("") + const close=() =>{ + toggleAddingComponent() + setName("") + } + const save=() =>{ + addComponent(name) + close() + } + + return ( + + + Add a new Component + + + setName(event.currentTarget.value)} label="Component Name" /> + + + + + + + ) +}; \ No newline at end of file diff --git a/src/Layout.tsx b/src/Layout.tsx index 1eef4e2..4e0705c 100644 --- a/src/Layout.tsx +++ b/src/Layout.tsx @@ -6,6 +6,7 @@ import { createLayoutComponent } from "./constraints/generator"; import { LayoutContext, LayoutProvider } from "./LayoutContext"; import { DebugElement } from "./DebugComponent"; import { EditConstraint } from "./EditConstraint"; +import { AddConstraint } from "./AddConstraint"; const F = ""; @@ -61,7 +62,7 @@ const ComponentList: FunctionComponent<{ box: string; item: string }> = ({ box, item }) => { - const { layout, addComponent } = useContext(LayoutContext); + const { layout, toggleAddingComponent } = useContext(LayoutContext); return ( <> @@ -73,13 +74,7 @@ const ComponentList: FunctionComponent<{ box: string; item: string }> = ({ ))} - addComponent( - [...Array(10)] - .map(i => (~~(Math.random() * 36)).toString(36)) - .join("") - ) - } + onClick={toggleAddingComponent} > Add component @@ -95,10 +90,10 @@ const ComponentView = () => { Object.keys(layout).forEach(name => { props[name] = ; }); - + return ( - { - setSize({width: size.width, height: size.height}) + { + setSize({ width: size.width, height: size.height }) }}> { return ( +
diff --git a/src/LayoutContext.tsx b/src/LayoutContext.tsx index 93b2c55..bea804f 100644 --- a/src/LayoutContext.tsx +++ b/src/LayoutContext.tsx @@ -11,9 +11,12 @@ import { ConstraintDefinition } from "./constraints/definition"; + export interface LayoutContextInterface { layout: LayoutDefinition; addComponent: (name: string) => void; + addingComponent: boolean; + toggleAddingComponent: () => void; changeConstraint: (constraint: ConstraintDefinition) => void; selectedComponent: string; setSelectedComponent: Dispatch>; @@ -54,6 +57,7 @@ const defaultConstraints: ConstraintDefinition[] = [ export const LayoutProvider: FunctionComponent = ({ children }) => { const [layout, setLayout] = useState({}); + const [addingComponent, setAddingComponent] = useState(false) const [selectedComponent, setSelectedComponent] = useState(""); const [editConstraint, setEditConstraint] = useState(""); const addComponent = (name: string) => @@ -84,6 +88,8 @@ export const LayoutProvider: FunctionComponent = ({ children }) => { value={{ layout, addComponent, + addingComponent, + toggleAddingComponent: () => setAddingComponent(!addingComponent), changeConstraint, selectedComponent, setSelectedComponent,