From 4dffc47eb6fe1818df9da01e4a7c5292878c4237 Mon Sep 17 00:00:00 2001 From: James McKenzie Date: Fri, 23 Apr 2021 14:44:31 +1200 Subject: [PATCH] Adding and deleting of columns working now --- ReactLearning/kanban-board/src/App.tsx | 30 ++++++++++++--- .../kanban-board/src/components/Header.tsx | 8 +++- .../kanban-board/src/components/KBColumn.tsx | 45 ++++++++++++---------- 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/ReactLearning/kanban-board/src/App.tsx b/ReactLearning/kanban-board/src/App.tsx index cba76f2..2c44adf 100644 --- a/ReactLearning/kanban-board/src/App.tsx +++ b/ReactLearning/kanban-board/src/App.tsx @@ -11,12 +11,16 @@ type Column = { type State = { columns: Column[]; + iterator: number; }; class App extends React.Component<{}, State> { constructor(props: any) { super(props); + this.deleteColumn = this.deleteColumn.bind(this); + this.addColumn = this.addColumn.bind(this) this.state = { + iterator: 3, columns: [ { id: 1, @@ -25,7 +29,7 @@ class App extends React.Component<{}, State> { }, { id: 2, - name: 'Potter', + name: "Potter", color: "red", }, ], @@ -34,16 +38,32 @@ class App extends React.Component<{}, State> { deleteColumn(id: number) { const { columns } = { ...this.state }; - columns.filter((column) => column.id !== id); - this.setState({ columns }); + const filteredColumns = columns.filter((column) => column.id !== id); + this.setState({ columns: filteredColumns }); + console.log(this.state.columns); + } + addColumn() { + let columnId = this.state.iterator; + this.setState({ iterator: columnId + 1 }); + this.state.columns.push({ + id: columnId, + name: "tempName" + columnId, + color: "red", + }); } render() { return (
-
+
{this.state.columns.map((column, index) => ( - + ))}
); diff --git a/ReactLearning/kanban-board/src/components/Header.tsx b/ReactLearning/kanban-board/src/components/Header.tsx index f9ac7a9..bb601e9 100644 --- a/ReactLearning/kanban-board/src/components/Header.tsx +++ b/ReactLearning/kanban-board/src/components/Header.tsx @@ -1,12 +1,16 @@ import * as react from 'react' -const Header = () => { +type HeaderProps = { + addColumn: () => void +} +const Header = ( props: HeaderProps ) => { + return(
- +
) } diff --git a/ReactLearning/kanban-board/src/components/KBColumn.tsx b/ReactLearning/kanban-board/src/components/KBColumn.tsx index d7c7ee7..17396fb 100644 --- a/ReactLearning/kanban-board/src/components/KBColumn.tsx +++ b/ReactLearning/kanban-board/src/components/KBColumn.tsx @@ -1,22 +1,25 @@ -import * as react from 'react' +import * as react from "react"; type KBColumnProps = { - color?: string, - title?: string, - tasks?: string[] -} -const defaultProps :KBColumnProps = { - color: '#00cc00', - title: "waah", - tasks: [] -} -const KBColumn = ( props :KBColumnProps) => { - - const {color, title, tasks} = { ...defaultProps, ...props} - - return( -
-

{title}

-
- ) -} -export default KBColumn \ No newline at end of file + color?: string; + title?: string; + tasks?: string[]; + id: number; + onDelete?: (id: number) => void +}; +const defaultProps: KBColumnProps = { + color: "#00cc00", + title: "waah", + tasks: [], + id: -1, + onDelete: (id: number) => {} +}; +const KBColumn = (props: KBColumnProps) => { + const { color, title, tasks, onDelete, id } = { ...defaultProps, ...props }; + return ( +
+

{title}

+ +
+ ); +}; +export default KBColumn; \ No newline at end of file