-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathColumnPickerTreeGroup.tsx
More file actions
52 lines (46 loc) · 1.55 KB
/
ColumnPickerTreeGroup.tsx
File metadata and controls
52 lines (46 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { FC, ReactNode } from 'react';
import IndentedCheckbox from 'lib/components/core/IndentedCheckbox';
import { ColumnPickerRenderCtx } from '../builder';
interface ColumnPickerTreeGroupProps {
label: string;
/** All leaf column ids that belong to this group (used to derive parent state). */
childIds: string[];
ctx: ColumnPickerRenderCtx;
/** Ids that are locked visible — parent checkbox is disabled when all children are locked. */
locked?: string[];
indentLevel?: number;
children: ReactNode;
}
/**
* Renders a parent checkbox whose checked/indeterminate state mirrors its children's
* visibility, and whose onChange bulk-toggles all children via ctx.setManyVisible.
* Children are rendered below (not inline), giving a vertical tree layout.
*/
const ColumnPickerTreeGroup: FC<ColumnPickerTreeGroupProps> = ({
label,
childIds,
ctx,
locked = [],
indentLevel = 0,
children,
}) => {
const visibleCount = childIds.filter((id) => ctx.isVisible(id)).length;
const allVisible = childIds.length > 0 && visibleCount === childIds.length;
const someVisible = visibleCount > 0 && !allVisible;
const allLocked =
childIds.length > 0 && childIds.every((id) => locked.includes(id));
return (
<div>
<IndentedCheckbox
checked={allVisible}
disabled={allLocked}
indentLevel={indentLevel}
indeterminate={someVisible}
label={label}
onChange={(e) => ctx.setManyVisible(childIds, e.target.checked)}
/>
<div>{children}</div>
</div>
);
};
export default ColumnPickerTreeGroup;