Skip to content

Commit dcbf58e

Browse files
authored
Merge pull request #443 from NMFS-RADFish/208-new-table---set-default-sortable-column
208 new table set default sortable column
2 parents 6e9cafd + 903ea49 commit dcbf58e

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

examples/simple-table/src/pages/Home.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ const HomePage = () => {
132132
onPageChange: onPageChange,
133133
totalRows: data.length,
134134
}}
135-
onRowClick={(row) => {console.log("Row clicked:", row)}}
135+
onRowClick={(row) => {
136+
console.log("Row clicked:", row);
137+
}}
138+
defaultSort={[
139+
{ key: "price", direction: "asc" },
140+
{ key: "species", direction: "desc" },
141+
]}
136142
striped
137143
bordered
138144
/>

packages/react-radfish/table/Table.spec.jsx

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,7 @@ describe("Table", () => {
6969
{ key: "Age", label: "Age", sortable: true },
7070
];
7171

72-
const onPageChangeMock = vi.fn();
73-
74-
const paginationOptions = {
75-
pageSize: 4,
76-
currentPage: 1,
77-
onPageChange: onPageChangeMock,
78-
totalRows: data.length,
79-
};
80-
81-
render(<Table data={data} columns={columns} />);
72+
render(<Table data={data} columns={columns}/>);
8273

8374
// First click on Age sorts by Age ascending
8475
fireEvent.click(screen.getByText("Age"));
@@ -216,4 +207,62 @@ describe("Table", () => {
216207
// Restore the original console.error
217208
consoleErrorSpy.mockRestore();
218209
});
210+
211+
it("applies defaultSort correctly and renders rows in the expected order", () => {
212+
const data = [
213+
{ id: 1, firstName: "Alice", lastName: "Smith", age: 25 },
214+
{ id: 2, firstName: "Bob", lastName: "Jones", age: 30 },
215+
{ id: 3, firstName: "Charlie", lastName: "Smith", age: 20 },
216+
{ id: 4, firstName: "Dave", lastName: "Jones", age: 40 },
217+
{ id: 5, firstName: "Eva", lastName: "Brown", age: 35 },
218+
];
219+
220+
const columns = [
221+
{ key: "firstName", label: "First Name", sortable: true },
222+
{ key: "lastName", label: "Last Name", sortable: true },
223+
{ key: "age", label: "Age", sortable: true },
224+
];
225+
226+
const defaultSort = [
227+
{ key: "lastName", direction: "asc" },
228+
{ key: "firstName", direction: "desc" },
229+
];
230+
231+
const onPageChangeMock = vi.fn();
232+
233+
const paginationOptions = {
234+
pageSize: 5,
235+
currentPage: 1,
236+
onPageChange: onPageChangeMock,
237+
totalRows: data.length,
238+
};
239+
240+
render(
241+
<Table
242+
data={data}
243+
columns={columns}
244+
defaultSort={defaultSort}
245+
paginationOptions={paginationOptions}
246+
/>,
247+
);
248+
249+
// Get all data rows (excluding the header row)
250+
const rows = screen.getAllByRole("row").slice(1); // Remove header row
251+
252+
// Expected order based on defaultSort
253+
const expectedOrder = [
254+
{ firstName: "Eva", lastName: "Brown" },
255+
{ firstName: "Dave", lastName: "Jones" },
256+
{ firstName: "Bob", lastName: "Jones" },
257+
{ firstName: "Charlie", lastName: "Smith" },
258+
{ firstName: "Alice", lastName: "Smith" },
259+
];
260+
261+
// Assert that the rows are in the expected order
262+
expectedOrder.forEach((expectedData, index) => {
263+
const row = rows[index];
264+
expect(within(row).getByText(expectedData.firstName)).toBeInTheDocument();
265+
expect(within(row).getByText(expectedData.lastName)).toBeInTheDocument();
266+
});
267+
});
219268
});

packages/react-radfish/table/index.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,16 @@ const TableStructure = ({ data, columns, handleSort, sortState, onRowClick }) =>
8989
* @returns {JSX.Element} The rendered table component.
9090
*/
9191

92-
const RADFishTable = ({ data, columns, paginationOptions, className, onRowClick, ...props }) => {
93-
const [sortState, setSortState] = useState([]);
92+
const RADFishTable = ({
93+
data,
94+
columns,
95+
paginationOptions,
96+
onRowClick,
97+
defaultSort,
98+
className,
99+
...props
100+
}) => {
101+
const [sortState, setSortState] = useState(defaultSort || []);
94102
const [pageIndex, setPageIndex] = useState(
95103
paginationOptions?.currentPage ? paginationOptions.currentPage - 1 : 0,
96104
);

0 commit comments

Comments
 (0)