Skip to content

Commit 1b1e2db

Browse files
authored
Merge pull request #81 from Sebbben/betterUsabilityForCustomTableComponent
Better usability for custom table component
2 parents cbe876a + 6beb84d commit 1b1e2db

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

app/components/CustomTable.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ const booleanFilterOptions = [
5656
{ id: "false", name: "False" },
5757
]
5858

59-
function CustomTable({ headers, data, defaultFilterBy }) {
59+
function CustomTable({ headers, data, defaultFilterBy=null }) {
60+
let header = null;
61+
if (defaultFilterBy) {
62+
header = headers.filter(h => h.id == defaultFilterBy)[0];
63+
}
6064
const [sortBy, setSortBy] = useState(() => headers[0]?.sortBy || null);
6165
const [sortDirection, setSortDirection] = useState("DESC");
6266
const [rowsPerPage, setRowsPerPage] = useState(10);
@@ -66,11 +70,15 @@ function CustomTable({ headers, data, defaultFilterBy }) {
6670
const [searchString, setSearchString] = useState("");
6771
const [selectedDateTime, setSelectedDateTime] = useState(new Date());
6872

69-
const [availableFilterOptions, setAvailableFilterOptions] = useState([]);
70-
const [selectedFilterOption, setSelectedFilterOption] = useState(null);
71-
const [selectedSearchColumn, setSelectedSearchColumn] = useState(null);
73+
const [availableFilterOptions, setAvailableFilterOptions] = useState(
74+
header.type === "string" || header.type === "boolean" ? filterTableOptions.filter((e) => e.id === "contains") : filterTableOptions.filter((e) => e.id !== "contains")
75+
);
76+
const [selectedFilterOption, setSelectedFilterOption] = useState(availableFilterOptions[0]);
77+
const [selectedSearchColumn, setSelectedSearchColumn] = useState(header);
7278
const [selectedBooleanOption, setSelectedBooleanOption] = useState(null);
7379

80+
81+
7482
const sortedData = useMemo(() => {
7583
return [...data].sort((a, b) => sortTableRows(a, b, sortBy, sortDirection));
7684
}, [data, sortBy, sortDirection]);
@@ -160,7 +168,7 @@ function CustomTable({ headers, data, defaultFilterBy }) {
160168
setAvailableFilterOptions(filterTableOptions.filter((e) => e.id !== "contains"));
161169
}
162170

163-
setSelectedFilterOption(null);
171+
setSelectedFilterOption(availableFilterOptions.length > 0 ? availableFilterOptions[0] : null);
164172
setSelectedBooleanOption(null);
165173
setSearchString("");
166174

@@ -394,7 +402,7 @@ CustomTable.propTypes = {
394402
})
395403
).isRequired,
396404
data: PropTypes.array.isRequired,
397-
sortBy: PropTypes.string
405+
sortBy: PropTypes.string,
398406
};
399407

400408
export default CustomTable;

0 commit comments

Comments
 (0)