From ad3627491e82f54d3821bd36b1c1bd54e9b5ee3d Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Mon, 7 Feb 2022 23:13:30 +0200 Subject: [PATCH] fix: clean up the code a bit --- src/TableDefinitionForm.tsx | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/TableDefinitionForm.tsx b/src/TableDefinitionForm.tsx index 5cb8570..54614a5 100644 --- a/src/TableDefinitionForm.tsx +++ b/src/TableDefinitionForm.tsx @@ -4,7 +4,7 @@ import { Alignment, TableColumn, TableColumns } from "./TableMethodGenerator" interface TableColumnProps { value: TableColumn - onChange?: { (e: TableColumn): void } + onChange: { (e: TableColumn): void } } function TableDefinitionRow(props: TableColumnProps) { @@ -12,36 +12,28 @@ function TableDefinitionRow(props: TableColumnProps) { const onChangeName = (e: ChangeEvent) => { props.value.name = e.target.value setName(props.value.name) - if (props.onChange) { - props.onChange(props.value) - } + props.onChange(props.value) } let [, setField] = useState(props.value.field) const onChangeField = (e: ChangeEvent) => { props.value.field = e.target.value setField(props.value.field) - if (props.onChange) { - props.onChange(props.value) - } + props.onChange(props.value) } let [, setWidth] = useState(props.value.width) const onChangeWidth = (e: ChangeEvent) => { props.value.width = parseFloat(e.target.value) setWidth(props.value.width) - if (props.onChange) { - props.onChange(props.value) - } + props.onChange(props.value) } let [, setAlignment] = useState(props.value.alignment) const onChangeAlignment = (e: ChangeEvent) => { props.value.alignment = e.target.value as Alignment setAlignment(props.value.alignment) - if (props.onChange) { - props.onChange(props.value) - } + props.onChange(props.value) } return ( @@ -78,12 +70,10 @@ function TableDefinitionForm(props: TableDefinitionProps) { const updateRow = (i: number, column: TableColumn) => { if (column.name === "") { columns.splice(i, 1) - columns = [...columns] - setColumns(columns) + setColumns([...columns]) } else { - columns = [...columns] columns[i] = column - setColumns(columns) + setColumns([...columns]) } if (props.onChange !== undefined) { props.onChange(columns) @@ -100,11 +90,9 @@ function TableDefinitionForm(props: TableDefinitionProps) { onKeyPress={(e) => e.key === "Enter" && addRow()} />
    - { - columns.map((item, i) => - updateRow(i, e)}/> - ) - } + {columns.map((item, i) => + updateRow(i, e)}/> + )}
)