fix: clean up the code a bit
This commit is contained in:
parent
55c1483052
commit
ad3627491e
@ -4,7 +4,7 @@ import { Alignment, TableColumn, TableColumns } from "./TableMethodGenerator"
|
|||||||
|
|
||||||
interface TableColumnProps {
|
interface TableColumnProps {
|
||||||
value: TableColumn
|
value: TableColumn
|
||||||
onChange?: { (e: TableColumn): void }
|
onChange: { (e: TableColumn): void }
|
||||||
}
|
}
|
||||||
|
|
||||||
function TableDefinitionRow(props: TableColumnProps) {
|
function TableDefinitionRow(props: TableColumnProps) {
|
||||||
@ -12,36 +12,28 @@ function TableDefinitionRow(props: TableColumnProps) {
|
|||||||
const onChangeName = (e: ChangeEvent<HTMLInputElement>) => {
|
const onChangeName = (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
props.value.name = e.target.value
|
props.value.name = e.target.value
|
||||||
setName(props.value.name)
|
setName(props.value.name)
|
||||||
if (props.onChange) {
|
props.onChange(props.value)
|
||||||
props.onChange(props.value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let [, setField] = useState(props.value.field)
|
let [, setField] = useState(props.value.field)
|
||||||
const onChangeField = (e: ChangeEvent<HTMLInputElement>) => {
|
const onChangeField = (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
props.value.field = e.target.value
|
props.value.field = e.target.value
|
||||||
setField(props.value.field)
|
setField(props.value.field)
|
||||||
if (props.onChange) {
|
props.onChange(props.value)
|
||||||
props.onChange(props.value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let [, setWidth] = useState(props.value.width)
|
let [, setWidth] = useState(props.value.width)
|
||||||
const onChangeWidth = (e: ChangeEvent<HTMLInputElement>) => {
|
const onChangeWidth = (e: ChangeEvent<HTMLInputElement>) => {
|
||||||
props.value.width = parseFloat(e.target.value)
|
props.value.width = parseFloat(e.target.value)
|
||||||
setWidth(props.value.width)
|
setWidth(props.value.width)
|
||||||
if (props.onChange) {
|
props.onChange(props.value)
|
||||||
props.onChange(props.value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let [, setAlignment] = useState(props.value.alignment)
|
let [, setAlignment] = useState(props.value.alignment)
|
||||||
const onChangeAlignment = (e: ChangeEvent<HTMLSelectElement>) => {
|
const onChangeAlignment = (e: ChangeEvent<HTMLSelectElement>) => {
|
||||||
props.value.alignment = e.target.value as Alignment
|
props.value.alignment = e.target.value as Alignment
|
||||||
setAlignment(props.value.alignment)
|
setAlignment(props.value.alignment)
|
||||||
if (props.onChange) {
|
props.onChange(props.value)
|
||||||
props.onChange(props.value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -78,12 +70,10 @@ function TableDefinitionForm(props: TableDefinitionProps) {
|
|||||||
const updateRow = (i: number, column: TableColumn) => {
|
const updateRow = (i: number, column: TableColumn) => {
|
||||||
if (column.name === "") {
|
if (column.name === "") {
|
||||||
columns.splice(i, 1)
|
columns.splice(i, 1)
|
||||||
columns = [...columns]
|
setColumns([...columns])
|
||||||
setColumns(columns)
|
|
||||||
} else {
|
} else {
|
||||||
columns = [...columns]
|
|
||||||
columns[i] = column
|
columns[i] = column
|
||||||
setColumns(columns)
|
setColumns([...columns])
|
||||||
}
|
}
|
||||||
if (props.onChange !== undefined) {
|
if (props.onChange !== undefined) {
|
||||||
props.onChange(columns)
|
props.onChange(columns)
|
||||||
@ -100,11 +90,9 @@ function TableDefinitionForm(props: TableDefinitionProps) {
|
|||||||
onKeyPress={(e) => e.key === "Enter" && addRow()}
|
onKeyPress={(e) => e.key === "Enter" && addRow()}
|
||||||
/>
|
/>
|
||||||
<ol>
|
<ol>
|
||||||
{
|
{columns.map((item, i) =>
|
||||||
columns.map((item, i) =>
|
<TableDefinitionRow key={i} value={item} onChange={(e) => updateRow(i, e)}/>
|
||||||
<TableDefinitionRow key={i} value={item} onChange={(e) => updateRow(i, e)}/>
|
)}
|
||||||
)
|
|
||||||
}
|
|
||||||
</ol>
|
</ol>
|
||||||
</form>
|
</form>
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user