Skip to content

Commit

Permalink
feat: add select sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-belcher committed Nov 17, 2023
1 parent 9469391 commit 76ad120
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/services/ui/src/components/RHF/Slot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { format } from "date-fns";
import { RHFFieldArray } from "./FieldArray";
import { FieldGroup } from "./FieldGroup";
import type { RHFSlotProps, RHFComponentMap, FormGroup } from "./types";
import { useEffect } from "react";
import { useEffect, useMemo } from "react";

export const RHFSlot = <
TFieldValues extends FieldValues = FieldValues,
Expand Down Expand Up @@ -86,6 +86,17 @@ export const RHFSlot = <
{rhf === "Select" &&
(() => {
const hops = props as RHFComponentMap["Select"];
const opts = useMemo(() => {
if (hops.sort) {
const sorted = hops.options.sort((a, b) =>
a.label.localeCompare(b.label)
);
hops.sort === "descending" && sorted.reverse();
return sorted;
}
return hops.options;
}, [hops.options, hops.sort]);

return (
<Select
{...hops}
Expand All @@ -96,7 +107,7 @@ export const RHFSlot = <
<SelectValue {...hops} />
</SelectTrigger>
<SelectContent className="overflow-auto max-h-60">
{hops.options.map((OPT) => (
{opts.map((OPT) => (
<SelectItem key={`OPT-${OPT.value}`} value={OPT.value}>
{OPT.label}
</SelectItem>
Expand Down
2 changes: 1 addition & 1 deletion src/services/ui/src/components/RHF/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type RHFComponentMap = {
};
Textarea: TextareaProps;
Switch: SwitchProps;
Select: SelectProps;
Select: SelectProps & { sort?: "ascending" | "descending" };
Radio: RadioProps & {
options: RHFOption[];
};
Expand Down
1 change: 1 addition & 0 deletions src/services/ui/src/pages/form/proto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const ABP1: Document = {
},
label: "Eligibility group",
props: {
sort: "ascending",
className: "min-w-[300px]",
options: [
{
Expand Down

0 comments on commit 76ad120

Please sign in to comment.