|
1 | 1 | import type { ReactNode } from "react" |
2 | | -import { useMemo } from "react" |
| 2 | +import { useCallback } from "react" |
| 3 | +import { clsx } from "clsx" |
3 | 4 | import { CheckboxIcon } from "@/app/conf/_design-system/pixelarticons/checkbox-icon" |
4 | 5 |
|
5 | 6 | export interface CheckboxTreeItem { |
6 | 7 | id: string |
7 | 8 | label: string |
8 | 9 | value?: string |
9 | 10 | count?: number |
| 11 | + disabled?: boolean |
10 | 12 | children?: CheckboxTreeItem[] |
11 | 13 | } |
12 | 14 |
|
13 | 15 | interface CheckboxTreeProps { |
14 | 16 | items: CheckboxTreeItem[] |
15 | 17 | selectedValues: string[] |
16 | 18 | onSelectionChange: (next: string[]) => void |
17 | | - searchQuery?: string |
18 | 19 | emptyFallback?: ReactNode |
19 | 20 | } |
20 | 21 |
|
21 | | -type PreparedItem = CheckboxTreeItem & { depth: number } |
22 | | - |
23 | | -type PreparedTree = PreparedItem & { |
24 | | - children?: PreparedTree[] |
25 | | - matchesSearch: boolean |
26 | | - hasVisibleChildren: boolean |
27 | | -} |
28 | | - |
29 | 22 | export function CheckboxTree({ |
30 | 23 | items, |
31 | 24 | selectedValues, |
32 | 25 | onSelectionChange, |
33 | | - searchQuery, |
34 | 26 | emptyFallback, |
35 | 27 | }: CheckboxTreeProps) { |
36 | | - const normalizedSearch = searchQuery?.trim().toLowerCase() ?? "" |
37 | | - |
38 | | - const preparedItems = useMemo(() => { |
39 | | - function enhance( |
40 | | - itemsToEnhance: CheckboxTreeItem[], |
41 | | - depth: number, |
42 | | - ): PreparedTree[] { |
43 | | - return itemsToEnhance.map(item => { |
44 | | - const prepared: PreparedTree = { |
45 | | - ...item, |
46 | | - depth, |
47 | | - matchesSearch: normalizedSearch |
48 | | - ? item.label.toLowerCase().includes(normalizedSearch) |
49 | | - : true, |
50 | | - hasVisibleChildren: false, |
51 | | - } |
52 | | - |
53 | | - if (item.children && item.children.length > 0) { |
54 | | - prepared.children = enhance(item.children, depth + 1) |
55 | | - } |
56 | | - |
57 | | - return prepared |
58 | | - }) |
59 | | - } |
60 | | - |
61 | | - return enhance(items, 0) |
62 | | - }, [items, normalizedSearch]) |
63 | | - |
64 | | - const filteredTree = useMemo(() => { |
65 | | - function markVisibility(node: PreparedTree): PreparedTree | null { |
66 | | - const { children } = node |
67 | | - const visibleChildren = children |
68 | | - ?.map(child => markVisibility(child)) |
69 | | - .filter((child): child is PreparedTree => Boolean(child)) |
70 | | - |
71 | | - const hasVisibleChildren = Boolean(visibleChildren?.length) |
72 | | - |
73 | | - const shouldKeepNode = |
74 | | - node.matchesSearch || !normalizedSearch || hasVisibleChildren |
| 28 | + const toggleValue = useCallback( |
| 29 | + (value: string) => { |
| 30 | + const next = selectedValues.includes(value) |
| 31 | + ? selectedValues.filter(tag => tag !== value) |
| 32 | + : [...selectedValues, value] |
| 33 | + onSelectionChange(next) |
| 34 | + }, |
| 35 | + [selectedValues, onSelectionChange], |
| 36 | + ) |
| 37 | + |
| 38 | + const renderTree = (nodes: CheckboxTreeItem[], depth: number): ReactNode => { |
| 39 | + return nodes.map(node => { |
| 40 | + const isSelectable = Boolean(node.value) |
| 41 | + const isDisabled = node.disabled |
| 42 | + const isChecked = isSelectable |
| 43 | + ? selectedValues.includes(node.value!) |
| 44 | + : false |
| 45 | + const checkboxId = `checkbox-tree-${node.id}` |
75 | 46 |
|
76 | | - if (!shouldKeepNode) return null |
| 47 | + return ( |
| 48 | + <div key={node.id}> |
| 49 | + <div |
| 50 | + className="flex items-start gap-2 py-1" |
| 51 | + style={{ |
| 52 | + paddingInlineStart: depth > 0 ? (depth - 1) * 16 : 0, |
| 53 | + }} |
| 54 | + > |
| 55 | + {isSelectable ? ( |
| 56 | + <label |
| 57 | + htmlFor={checkboxId} |
| 58 | + className={clsx( |
| 59 | + "flex grow items-center gap-2", |
| 60 | + isDisabled |
| 61 | + ? "cursor-not-allowed text-neu-500" |
| 62 | + : "cursor-pointer", |
| 63 | + )} |
| 64 | + aria-disabled={isDisabled} |
| 65 | + > |
| 66 | + <span className="flex shrink-0 items-center"> |
| 67 | + <input |
| 68 | + id={checkboxId} |
| 69 | + type="checkbox" |
| 70 | + checked={isChecked} |
| 71 | + onChange={() => { |
| 72 | + if (!isDisabled) toggleValue(node.value!) |
| 73 | + }} |
| 74 | + disabled={isDisabled} |
| 75 | + className="peer sr-only" |
| 76 | + /> |
| 77 | + <CheckboxIcon |
| 78 | + checked={isChecked} |
| 79 | + className={clsx( |
| 80 | + "pointer-events-none size-5 transition-colors peer-focus-visible:outline peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-neu-900", |
| 81 | + isDisabled ? "text-neu-300" : undefined, |
| 82 | + )} |
| 83 | + aria-hidden |
| 84 | + /> |
| 85 | + </span> |
| 86 | + <span |
| 87 | + className={clsx( |
| 88 | + "min-w-0 grow truncate text-left", |
| 89 | + isDisabled ? "text-neu-500" : "text-neu-800", |
| 90 | + )} |
| 91 | + > |
| 92 | + {node.label} |
| 93 | + </span> |
| 94 | + {node.count ? ( // we intentionally don't display 0 |
| 95 | + <span |
| 96 | + className={clsx( |
| 97 | + "ml-auto shrink-0 text-xs", |
| 98 | + isDisabled ? "text-neu-400" : "text-neu-700", |
| 99 | + )} |
| 100 | + > |
| 101 | + {node.count} |
| 102 | + </span> |
| 103 | + ) : null} |
| 104 | + </label> |
| 105 | + ) : ( |
| 106 | + <div |
| 107 | + className={clsx( |
| 108 | + "typography-menu mt-4 text-sm xl:mt-10", |
| 109 | + isDisabled ? "text-neu-500" : "text-neu-900", |
| 110 | + )} |
| 111 | + aria-disabled={isDisabled} |
| 112 | + > |
| 113 | + {node.label} |
| 114 | + </div> |
| 115 | + )} |
| 116 | + </div> |
77 | 117 |
|
78 | | - return { |
79 | | - ...node, |
80 | | - children: visibleChildren, |
81 | | - hasVisibleChildren, |
82 | | - } |
83 | | - } |
| 118 | + {node.children && node.children.length > 0 ? ( |
| 119 | + <div>{renderTree(node.children, depth + 1)}</div> |
| 120 | + ) : null} |
| 121 | + </div> |
| 122 | + ) |
| 123 | + }) |
| 124 | + } |
84 | 125 |
|
85 | | - return preparedItems |
86 | | - .map(node => markVisibility(node)) |
87 | | - .filter((node): node is PreparedTree => Boolean(node)) |
88 | | - }, [preparedItems, normalizedSearch]) |
| 126 | + if (items.length === 0) { |
| 127 | + return ( |
| 128 | + <div className="py-4 text-sm text-neu-500"> |
| 129 | + {emptyFallback ?? "No matches"} |
| 130 | + </div> |
| 131 | + ) |
| 132 | + } |
89 | 133 |
|
90 | | - const toggleValue = (value: string) => { |
| 134 | + return <div>{renderTree(items, 0)}</div> |
| 135 | +} |
91 | 136 | if (selectedValues.includes(value)) { |
92 | 137 | onSelectionChange(selectedValues.filter(tag => tag !== value)) |
93 | 138 | } else { |
|
0 commit comments