Created
October 15, 2020 13:36
-
-
Save manstis/c517249de8888bf64420ec94fc6367fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface CharacteristicsTableProps { | |
characteristics: Characteristic[]; | |
onRowClick: (index: number) => void; | |
onRowDelete: (index: number) => void; | |
onAddCharacteristic: () => void; | |
} | |
const columns: ICell[] = [ | |
{ title: "Name" }, | |
{ title: "Attributes" }, | |
{ title: "Reason Code" }, | |
{ title: "Baseline score" }, | |
{ title: "Delete" } | |
]; | |
export const CharacteristicsTable = (props: CharacteristicsTableProps) => { | |
const { characteristics, onRowClick, onRowDelete, onAddCharacteristic } = props; | |
interface DeleteCharacteristicProps { | |
index: number; | |
} | |
const DeleteCharacteristic = useCallback( | |
(_props: DeleteCharacteristicProps) => { | |
return ( | |
<Button | |
variant="link" | |
icon={<TrashIcon />} | |
onClick={e => { | |
e.stopPropagation(); | |
onRowDelete(_props.index); | |
}} | |
> | |
| |
</Button> | |
); | |
}, | |
[characteristics] | |
); | |
const rows: IRow[] = useMemo(() => { | |
return characteristics.map<IRow>((c, index) => { | |
return { | |
cells: [ | |
c.name, | |
<> | |
<Label key={index}>{c.Attribute.length}</Label> | |
</>, | |
c.reasonCode, | |
c.baselineScore, | |
<> | |
<DeleteCharacteristic key={index} index={index} /> | |
</> | |
] | |
}; | |
}); | |
}, [characteristics]); | |
const rowClickHandler = ( | |
event: React.MouseEvent, | |
row: IRow, | |
rowProps: IExtraRowData, | |
computedData: IComputedData | |
) => { | |
onRowClick(rowProps.rowIndex as number); | |
}; | |
return ( | |
<React.Fragment> | |
<Table aria-label="Characteristics" cells={columns} rows={rows}> | |
<TableHeader /> | |
<TableBody onRowClick={rowClickHandler} /> | |
</Table> | |
{rows.length === 0 && <EmptyStateNoCharacteristics createCharacteristic={onAddCharacteristic} />} | |
</React.Fragment> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment