Skip to content

Commit

Permalink
feat: add components name
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianCraig committed Mar 10, 2020
1 parent 9fdaf4f commit 9576735
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
35 changes: 35 additions & 0 deletions src/AddConstraint.tsx
Original file line number Diff line number Diff line change
@@ -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<string>("")
const close=() =>{
toggleAddingComponent()
setName("")
}
const save=() =>{
addComponent(name)
close()
}

return (
<Dialog open={addingComponent} onClose={toggleAddingComponent}>
<DialogTitle>
Add a new Component
</DialogTitle>
<DialogContent>
<TextField value={name} onChange={(event) => setName(event.currentTarget.value)} label="Component Name" />
</DialogContent>
<DialogActions>
<Button onClick={close}>
Cancel
</Button>
<Button onClick={save} color="primary">
Add
</Button>
</DialogActions>
</Dialog>
)
};
18 changes: 7 additions & 11 deletions src/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";

Expand Down Expand Up @@ -61,7 +62,7 @@ const ComponentList: FunctionComponent<{ box: string; item: string }> = ({
box,
item
}) => {
const { layout, addComponent } = useContext(LayoutContext);
const { layout, toggleAddingComponent } = useContext(LayoutContext);

return (
<>
Expand All @@ -73,13 +74,7 @@ const ComponentList: FunctionComponent<{ box: string; item: string }> = ({
</div>
))}
<Typography
onClick={() =>
addComponent(
[...Array(10)]
.map(i => (~~(Math.random() * 36)).toString(36))
.join("")
)
}
onClick={toggleAddingComponent}
>
Add component
</Typography>
Expand All @@ -95,10 +90,10 @@ const ComponentView = () => {
Object.keys(layout).forEach(name => {
props[name] = <DebugElement name={name} />;
});

return (
<ResizableBox height={size.height} width={size.width} onResize={(event, { size, handle}) => {
setSize({width: size.width, height: size.height})
<ResizableBox height={size.height} width={size.width} onResize={(event, { size, handle }) => {
setSize({ width: size.width, height: size.height })
}}>
<Comp
width={size.width}
Expand All @@ -115,6 +110,7 @@ export const Layout: React.FC = () => {
return (
<LayoutProvider>
<EditConstraint />
<AddConstraint />
<div className={classes.root}>
<div className={classes.leftBlock}>
<div className={classes.titleBox}>
Expand Down
6 changes: 6 additions & 0 deletions src/LayoutContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<SetStateAction<string>>;
Expand Down Expand Up @@ -54,6 +57,7 @@ const defaultConstraints: ConstraintDefinition[] = [

export const LayoutProvider: FunctionComponent = ({ children }) => {
const [layout, setLayout] = useState<LayoutDefinition>({});
const [addingComponent, setAddingComponent] = useState<boolean>(false)
const [selectedComponent, setSelectedComponent] = useState<string>("");
const [editConstraint, setEditConstraint] = useState<string>("");
const addComponent = (name: string) =>
Expand Down Expand Up @@ -84,6 +88,8 @@ export const LayoutProvider: FunctionComponent = ({ children }) => {
value={{
layout,
addComponent,
addingComponent,
toggleAddingComponent: () => setAddingComponent(!addingComponent),
changeConstraint,
selectedComponent,
setSelectedComponent,
Expand Down

0 comments on commit 9576735

Please sign in to comment.