-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionArea.tsx
133 lines (122 loc) · 3.28 KB
/
SelectionArea.tsx
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React, { useCallback, useMemo, CSSProperties } from "react";
import GlobalStyles from "../../styles/_global.module.css";
import SelectionAreaStyles from "../../styles/SelectionArea.module.css";
import { useDefaultPalette } from "../hooks/useDefaultPalette";
/**
* SelectionArea component
* @param {SelectionAreaProps} props
* @returns {JSX.Element}
* @example
* <SelectionArea
* selections={["Item 1", 10, { name: "Item 6", value: 6 }]}
* selected={10}
* setSelected={(value: any) => { ... }}
* />
*/
export interface SelectionAreaProps {
/**
* Array of selection items
* @type {SelectionItem[]}
* @example ['Item 1', 10, { name: 'Item 6', value: 6 }]
*/
selections: SelectionItem[];
/**
* Selected value
* @type {any}
* @example (someData: any) => { ... } || 10 || 'Item 1'
*/
selected: any;
/**
* Function for setting selected value
* @type {(value: any) => void}
* @example (value: any) => { ... }
*/
setSelected: (value: any) => void;
/**
* Dimension for selection area
*/
dimension?: {
/**
* Width for selection area
* @type {string}
*/
width?: string;
/**
* Height for selection area
* @type {string}
*/
height?: string;
};
/**
* Flag for whether to filter selections for unique values
*/
// unique?: boolean;
/**
* Flag for making selection area searchable
*/
// search?: boolean;
/**
* Selection area id
*/
id?: string;
/**
* Selection area class name
*/
className?: string;
/**
* Selection area style
*/
style?: CSSProperties;
}
/**
* Type for selection items
* @type {string | number | { name: string; value: any;}} SelectionItem
*/
export type SelectionItem = string | number | { name: string; value: any };
function SelectionArea(props: SelectionAreaProps) {
const [palette] = useDefaultPalette(props.style);
palette.width = props.dimension?.width || "400px";
palette.height = props.dimension?.height || "200px";
const selectionName = (selection: SelectionItem) => {
if (typeof selection === "object") {
return selection.name;
}
return selection;
};
const selectionAreaClass = `${GlobalStyles.global} ${SelectionAreaStyles.reuseSelectionArea} ${props.className}`;
const selectionClass = useCallback(
(selection: SelectionItem) => {
if (typeof selection === "object") {
if (selection.name === props.selected.name) {
return `${SelectionAreaStyles.reuseSelection} ${SelectionAreaStyles.reuseSelected}`;
}
} else {
if (selection === props.selected) {
return `${SelectionAreaStyles.reuseSelection} ${SelectionAreaStyles.reuseSelected}`;
}
}
return SelectionAreaStyles.reuseSelection;
},
[props.selected]
);
const handleSelection = (selection: SelectionItem) => {
if (props.setSelected) {
props.setSelected(selection);
}
};
const selectionElements = useMemo(() => {
return props.selections.map((selection: SelectionItem, idx: number) => {
return (
<div key={idx} className={selectionClass(selection)} onClick={() => handleSelection(selection)}>
{selectionName(selection)}
</div>
);
});
}, [props.selections, props.selected]);
return (
<div id={props.id} className={selectionAreaClass} style={palette}>
<div className={SelectionAreaStyles.reuseSelections}>{selectionElements}</div>
</div>
);
}
export { SelectionArea };