Skip to content

Commit

Permalink
feat(react-formio): add possibility to give custom choices function t…
Browse files Browse the repository at this point in the history
…o column property
  • Loading branch information
Romakita committed Mar 22, 2024
1 parent 483ab93 commit 553a4e6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import React from "react";
import { Row } from "react-table";
import { Cell, Row } from "react-table";

export function DefaultCells<Data extends object = {}>({ row }: { row: Row<Data> }) {
return (
<>
{row.cells.map((cell, i) => {
{row.cells.map((cell: Cell<Data, any> & { column: { className?: string; style?: React.CSSProperties } }, i) => {
const { hidden, colspan } = cell.column as any;

if (hidden) {
return null;
}

return (
<td colSpan={colspan} {...cell.getCellProps()} key={`tableInstance.page.cells.${cell.value || "value"}.${i}`}>
<td
colSpan={colspan}
{...cell.getCellProps({
className: cell.column.className,
style: cell.column.style
})}
key={`tableInstance.page.cells.${cell.value || "value"}.${i}`}
>
{cell.render("Cell") as any}
</td>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,25 @@ describe("SelectColumnFilter", () => {
expect(screen.queryByText("select-choice-1")).toBeNull();
expect(screen.getByText("fake-choice")).toBeDefined();
});

it("should display select with custom choices (function)", async () => {
const mockSetFilter = jest.fn();
const props = {
name: "data.id",
setFilter: mockSetFilter,
column: {
id: "id",
preFilteredRows: [{ values: { id: "select-choice-1" } }, { values: { id: "select-choice-2" } }],
choices: () => [{ label: "fake-choice", value: "fake-choice" }]
}
};

render(
// @ts-ignore
<SelectColumnFilter {...props} />
);

expect(screen.queryByText("select-choice-1")).toBeNull();
expect(screen.getByText("fake-choice")).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,50 @@ import { FilterProps } from "react-table";

import { Select } from "../../select/select.component";

export function SelectColumnFilter<D extends Record<string, unknown> = {}>({ column }: FilterProps<D>) {
const { id, preFilteredRows, filterValue, setFilter } = column;
export function useSelectColumnFilter<D extends Record<string, unknown> = {}>(props: FilterProps<D>) {
const { column } = props;
const { id, preFilteredRows } = column;
const { choices: customChoices } = column as any;
const { filterValue, setFilter } = column;

const choices =
customChoices ||
[...new Set(preFilteredRows.map((row) => row.values[id]))].filter((value) => value).map((value) => ({ label: value, value }));
const choices = (() => {
if (customChoices) {
if (typeof customChoices === "function") {
return customChoices(props);
}
return customChoices;
}

return [...new Set(preFilteredRows.map((row) => row.values[id]))]
.filter((value) => value)
.map((value) => ({
label: value,
value
}));
})();

const onChange = (_: string, value: any) => {
setFilter(value || undefined);
};

return {
value: filterValue,
onChange,
choices: [{ value: "", label: "All" }].concat(choices)
};
}

export function SelectColumnFilter<D extends Record<string, unknown> = {}>(props: FilterProps<D>) {
const { value, choices, onChange } = useSelectColumnFilter(props);

return (
<Select
key={`filter-${column.id}`}
name={`filter-${column.id}`}
key={`filter-${props.column.id}`}
name={`filter-${props.column.id}`}
size={"sm"}
value={filterValue}
choices={[{ value: "", label: "All" }].concat(choices)}
onChange={(name, value) => {
setFilter(value || undefined);
}}
value={value}
choices={choices}
onChange={onChange}
/>
);
}

0 comments on commit 553a4e6

Please sign in to comment.