Skip to content

Commit

Permalink
feat: move add button to first column in the tables
Browse files Browse the repository at this point in the history
fix: assign unique keys to table headers and rows
  • Loading branch information
Dusan Mijatovic (Mac2023) committed Mar 18, 2024
1 parent af36510 commit 937a7c5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
8 changes: 8 additions & 0 deletions packages/form/src/table/TableFieldTemplate.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@
{
display: none;
}

.table .table-actions{
vertical-align: middle;
}

th > label{
font-size: 0.875rem;
}
44 changes: 26 additions & 18 deletions packages/form/src/table/TableFieldTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ export const TableFieldTemplate = (props: ArrayFieldTemplateProps): JSX.Element
let indexColumnCell = (_index: number): JSX.Element => <></>
const [indexable, indexLookup] = useIndexable(props.uiSchema)
if (indexable) {
indexColumnHeader = <th key='index-th' className='index-th' />
indexColumnHeader = <th key={`${props.title}-index-th`} className='index-th' />
indexColumnCell = (index: number) => <td style={{ verticalAlign: 'middle' }}>{indexLookup(index)}</td>
}

const headers = Object.entries(rowSchema).map(
([key, s]: [string, any], i: number) => {
const title = required.has(key)
Expand All @@ -59,18 +60,21 @@ export const TableFieldTemplate = (props: ArrayFieldTemplateProps): JSX.Element
const srequired = new Set(s.required)
const propDescs = Object.entries(s.properties)
.filter((d: any) => d[1].description)
.map(([skey, sschema]: any) => (
<li key={skey}>
<label className='control-label'>
{sschema.title}
{srequired.has(skey) && <span className='required'>*</span>}
</label>
<p
dangerouslySetInnerHTML={{ __html: sschema.description }}
className='field-description'
/>
</li>
))
.map(([skey, sschema]: any) => {
// debugger
return (
<li key={skey}>
<label className='control-label'>
{sschema.title}
{srequired.has(skey) && <span className='required'>*</span>}
</label>
<p
dangerouslySetInnerHTML={{ __html: sschema.description }}
className='field-description'
/>
</li>
)
})
description = (
<>
<span dangerouslySetInnerHTML={{ __html: s.description }} />
Expand All @@ -83,7 +87,7 @@ export const TableFieldTemplate = (props: ArrayFieldTemplateProps): JSX.Element
const width = widths[key]
return (
<th
key={key}
key={`${props.title}-${key}`}
title={s.description}
style={{ width }}
>
Expand All @@ -107,8 +111,10 @@ export const TableFieldTemplate = (props: ArrayFieldTemplateProps): JSX.Element
)
}
)

headers.unshift(indexColumnHeader)
headers.push(
// add button as first column
headers.unshift(
<th key='actions-th' className='actions-th'>
<Button
className='array-item-add'
Expand All @@ -124,10 +130,10 @@ export const TableFieldTemplate = (props: ArrayFieldTemplateProps): JSX.Element
let rows: JSX.Element[] = []
if ('items' in props) {
rows = props.items.map((element: any, index) => {
const unique: string = `${props.title}-${element?.key as string}-${index.toString()}`
return (
<tr key={element.key} className={element.className}>
{indexColumnCell(index)}
{element.children}
// remove button as first column
<tr key={unique} className={element.className}>
<td className='table-actions'>
<Button
type='danger'
Expand All @@ -140,6 +146,8 @@ export const TableFieldTemplate = (props: ArrayFieldTemplateProps): JSX.Element
<BsDash />
</Button>
</td>
{indexColumnCell(index)}
{element.children}
</tr>
)
})
Expand Down

0 comments on commit 937a7c5

Please sign in to comment.