Skip to content
Snippets Groups Projects
Commit 647555cd authored by Alissa Cheng's avatar Alissa Cheng
Browse files

fix: icons in table stories

parent 469db3a6
No related branches found
No related tags found
1 merge request!43Empty states (table)
Pipeline #77519 passed
...@@ -63,7 +63,7 @@ const rows = [ ...@@ -63,7 +63,7 @@ const rows = [
name: "Tony", name: "Tony",
role: "CEO", role: "CEO",
status: <Badge text="Active" />, status: <Badge text="Active" />,
action: <Button label="Edit" color="secondary" icon="edit" />, action: <Button label="Edit" color="secondary" icon="Edit" />,
}, },
{ {
key: "2", key: "2",
...@@ -72,8 +72,8 @@ const rows = [ ...@@ -72,8 +72,8 @@ const rows = [
status: <Badge text="Paused" color="warning" />, status: <Badge text="Paused" color="warning" />,
action: ( action: (
<div className="flex flex-row gap-3 w-fit"> <div className="flex flex-row gap-3 w-fit">
<Button label="Edit" color="secondary" icon="edit" /> <Button label="Edit" color="secondary" icon="Edit" />
<Button label="Delete" color="negative" icon="cross" /> <Button label="Delete" color="negative" icon="Cross" />
</div> </div>
), ),
}, },
...@@ -84,8 +84,8 @@ const rows = [ ...@@ -84,8 +84,8 @@ const rows = [
status: <Badge text="Inactive" color="negative" />, status: <Badge text="Inactive" color="negative" />,
action: ( action: (
<div className="flex flex-row gap-3"> <div className="flex flex-row gap-3">
<Button label="Edit" color="secondary" icon="edit" /> <Button label="Edit" color="secondary" icon="Edit" />
<Button label="Delete" color="negative" icon="cross" /> <Button label="Delete" color="negative" icon="Cross" />
</div> </div>
), ),
}, },
...@@ -96,8 +96,8 @@ const rows = [ ...@@ -96,8 +96,8 @@ const rows = [
status: <Badge text="Vacation" color="neutral" />, status: <Badge text="Vacation" color="neutral" />,
action: ( action: (
<div className="flex flex-row gap-3"> <div className="flex flex-row gap-3">
<Button label="Edit" color="secondary" icon="edit" /> <Button label="Edit" color="secondary" icon="Edit" />
<Button label="View" color="primary" icon="eye" /> <Button label="View" color="primary" icon="Eye" />
</div> </div>
), ),
}, },
...@@ -110,14 +110,21 @@ export const TableWithText: StoryObj = { ...@@ -110,14 +110,21 @@ export const TableWithText: StoryObj = {
}, },
}; };
export const EmptyTable: StoryObj = {
args: {
columns: textColumns,
rows: [],
},
};
export const TableWithComponentsInHeader: StoryObj = { export const TableWithComponentsInHeader: StoryObj = {
args: { args: {
columns: textColumns, columns: textColumns,
rows: textRows, rows: textRows,
headerContent: ( headerContent: (
<div className="flex flex-row gap-2 pb-[10px]"> <div className="flex flex-row gap-2 pb-[10px]">
<Button label="Create New" color="positive" icon="plus" /> <Button label="Create New" color="positive" icon="Plus" />
<Button label="Edit" color="secondary" icon="edit" /> <Button label="Edit" color="secondary" icon="Edit" />
</div> </div>
), ),
}, },
...@@ -175,8 +182,6 @@ export const TableWithReorderableRows = () => { ...@@ -175,8 +182,6 @@ export const TableWithReorderableRows = () => {
); );
}; };
type Row = { type Row = {
row_id: string; row_id: string;
name: string; name: string;
...@@ -184,8 +189,7 @@ type Row = { ...@@ -184,8 +189,7 @@ type Row = {
salary: number; salary: number;
notes: string; notes: string;
action: JSX.Element; action: JSX.Element;
} };
const editableColumnRows = [ const editableColumnRows = [
{ {
...@@ -208,24 +212,38 @@ const editableColumnRows = [ ...@@ -208,24 +212,38 @@ const editableColumnRows = [
role: "Project manager", role: "Project manager",
salary: 2000000, salary: 2000000,
notes: "Micro manager", notes: "Micro manager",
} },
] as Row[]; ] as Row[];
/** Cells can be edited, cell values can be validated */ /** Cells can be edited, cell values can be validated */
export const TableWithEditableCells = () => { export const TableWithEditableCells = () => {
const textEditor = (options: ColumnEditorOptions) => { const textEditor = (options: ColumnEditorOptions) => {
return <TextInput value={options.value as string} onValueChange={(e) => options.editorCallback && options.editorCallback(e)}></TextInput> return (
<TextInput
value={options.value as string}
onValueChange={(e) =>
options.editorCallback && options.editorCallback(e)
}
></TextInput>
);
}; };
const numberEditor = (options: ColumnEditorOptions) => { const numberEditor = (options: ColumnEditorOptions) => {
return <TextInput type="number" value={options.value as string} onValueChange={(e) => options.editorCallback && options.editorCallback(e)}></TextInput> return (
<TextInput
type="number"
value={options.value as string}
onValueChange={(e) =>
options.editorCallback && options.editorCallback(e)
}
></TextInput>
);
}; };
const onCellEditComplete = (e: ColumnEvent) => { const onCellEditComplete = (e: ColumnEvent) => {
const { rowData, newValue, field } = e; const { rowData, newValue, field } = e;
rowData[field] = newValue; rowData[field] = newValue;
} };
const isPositiveInteger = (val: unknown) => { const isPositiveInteger = (val: unknown) => {
let str = String(val); let str = String(val);
...@@ -236,7 +254,7 @@ export const TableWithEditableCells = () => { ...@@ -236,7 +254,7 @@ export const TableWithEditableCells = () => {
return false; return false;
} }
str = str.replace(/^0+/, '') || '0'; str = str.replace(/^0+/, "") || "0";
const n = Math.floor(Number(str)); const n = Math.floor(Number(str));
return n !== Infinity && String(n) === str && n >= 0; return n !== Infinity && String(n) === str && n >= 0;
...@@ -249,13 +267,25 @@ export const TableWithEditableCells = () => { ...@@ -249,13 +267,25 @@ export const TableWithEditableCells = () => {
} else { } else {
event.preventDefault(); event.preventDefault();
} }
} };
const editableColumns = [ const editableColumns = [
{ field: "row_id", header: "ID", style: { width: "30%" } }, { field: "row_id", header: "ID", style: { width: "30%" } },
{ field: "name", header: "Name", style: { width: "20%" }, editor: textEditor, onCellEditComplete: onCellEditComplete }, {
field: "name",
header: "Name",
style: { width: "20%" },
editor: textEditor,
onCellEditComplete: onCellEditComplete,
},
{ field: "role", header: "Role", style: { width: "20%" } }, { field: "role", header: "Role", style: { width: "20%" } },
{ field: "salary", header: "Salary", style: { width: "10%" }, editor: numberEditor, onCellEditComplete: onSalaryEditComplete}, {
field: "salary",
header: "Salary",
style: { width: "10%" },
editor: numberEditor,
onCellEditComplete: onSalaryEditComplete,
},
{ field: "notes", header: "Notes", style: { width: "10%" } }, { field: "notes", header: "Notes", style: { width: "10%" } },
{ field: "action", header: "", style: { width: "10%" } }, { field: "action", header: "", style: { width: "10%" } },
]; ];
...@@ -263,29 +293,52 @@ export const TableWithEditableCells = () => { ...@@ -263,29 +293,52 @@ export const TableWithEditableCells = () => {
const [editableRows, setEditableRows] = useState<Row[]>([]); const [editableRows, setEditableRows] = useState<Row[]>([]);
useEffect(() => { useEffect(() => {
setEditableRows(editableColumnRows) setEditableRows(editableColumnRows);
}, []); }, []);
const deleteButton = (row: Row) => <Button label={"Delete"} icon={"cross"} onClick={() => { deleteEmployee(row) }} /> const deleteButton = (row: Row) => (
<Button
label="Delete"
icon="Cross"
onClick={() => {
deleteEmployee(row);
}}
/>
);
for (const row of editableRows) { for (const row of editableRows) {
row.action = deleteButton(row); row.action = deleteButton(row);
} }
const addEmployee = () => { const addEmployee = () => {
const newRow = { row_id: crypto.randomUUID(), name: "", role: "", salary: 0, notes: "" } as Row; const newRow = {
row_id: crypto.randomUUID(),
name: "",
role: "",
salary: 0,
notes: "",
} as Row;
newRow.action = deleteButton(newRow); newRow.action = deleteButton(newRow);
setEditableRows(old => [...old, newRow]) setEditableRows((old) => [...old, newRow]);
}; };
const deleteEmployee = (row: Row) => { const deleteEmployee = (row: Row) => {
const newRows = editableRows.filter(r => r !== row); const newRows = editableRows.filter((r) => r !== row);
setEditableRows(newRows); setEditableRows(newRows);
}; };
return ( return (
<Table <Table
headerContent={<div className="flex flex-row gap-2 pb-[10px]"><Button color="positive" icon="plus" label="Add Employee" onClick={addEmployee}/></div>} headerContent={
<div className="flex flex-row gap-2 pb-[10px]">
<Button
color="positive"
icon="Plus"
label="Add Employee"
onClick={addEmployee}
/>
</div>
}
columns={editableColumns} columns={editableColumns}
rows={editableRows} rows={editableRows}
editMode="cell" editMode="cell"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment