-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathdropdown.options.tsx
More file actions
137 lines (124 loc) · 3.6 KB
/
dropdown.options.tsx
File metadata and controls
137 lines (124 loc) · 3.6 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
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
134
135
136
137
import React, { useEffect, useRef } from 'react';
import classes from '@ff-client/utils/classes';
import DOMPurify from 'dompurify';
import CheckIcon from './check.svg';
import type { DropdownProps } from './dropdown';
import {
CheckMark,
Item,
Label,
LabelContainer,
LabelValueDisplay,
List,
} from './dropdown.options.styles';
import { OptionIcon } from './dropdown.styles';
type Props = DropdownProps & {
focusIndex: number;
query?: string;
showValues?: boolean;
showHints?: boolean;
};
export const Options: React.FC<Props> = ({
value: selectedValue,
options,
query,
focusIndex,
showValues,
showHints,
onChange,
}) => {
const optionRefs = useRef<HTMLLIElement[]>([]);
useEffect(() => {
if (optionRefs.current[focusIndex]) {
optionRefs.current[focusIndex].scrollIntoView({
behavior: 'smooth',
block: 'nearest',
});
}
}, [focusIndex]);
return (
<List>
{options &&
options.map((option, idx) => {
let value: string;
let hint: string;
let shadowIndex: number;
if ('value' in option) {
value = option.value;
shadowIndex = option.shadowIndex;
}
if ('hint' in option) {
hint = option.hint;
}
let children;
if ('children' in option) {
children = option.children;
}
return (
<Item
ref={(el) => {
if (shadowIndex !== undefined) {
optionRefs.current[shadowIndex] = el;
}
}}
onClick={(event) => {
event.stopPropagation();
if (value !== undefined && onChange) {
onChange(value);
}
}}
key={idx}
className={classes(
children !== undefined && 'has-children',
value === selectedValue && 'selected',
value === '' && 'empty',
shadowIndex === focusIndex && 'focused'
)}
>
<Label
className={classes(children !== undefined && 'has-children')}
data-value={value}
>
{!children && selectedValue === value && (
<CheckMark>
<CheckIcon />
</CheckMark>
)}
<LabelContainer>
{option.icon && <OptionIcon>{option.icon}</OptionIcon>}
<div>
<span
dangerouslySetInnerHTML={{
__html: DOMPurify.sanitize(option.label),
}}
/>
</div>
</LabelContainer>
{!showValues && showHints && hint && (
<LabelValueDisplay>{hint}</LabelValueDisplay>
)}
{showValues &&
value !== '' &&
value !== undefined &&
value !== null &&
value !== option.label && (
<LabelValueDisplay>{value}</LabelValueDisplay>
)}
</Label>
{children && (
<Options
options={children}
value={selectedValue}
query={query}
focusIndex={focusIndex}
onChange={onChange}
showHints={showHints}
showValues={showValues}
/>
)}
</Item>
);
})}
</List>
);
};