-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathDropdown.tsx
More file actions
73 lines (68 loc) · 2 KB
/
Dropdown.tsx
File metadata and controls
73 lines (68 loc) · 2 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
import type { ChangeEvent } from "react";
import { styled } from "styled-components";
const DISABLED_VALUE = "-";
const StyledDropdown = styled.select`
width: 100%;
height: calc(var(--input-height) * 1px);
background: var(--vscode-dropdown-background);
color: var(--vscode-foreground);
border-width: 0 5px 0 0;
padding: 2px 6px 2px 8px;
opacity: ${(props) =>
props.disabled ? "var(--disabled-opacity)" : "inherit"};
`;
type Props = {
value: string | undefined;
options: Array<{ value: string; label: string }>;
disabled?: boolean;
className?: string;
disabledPlaceholder?: string;
onChange?: (event: ChangeEvent<HTMLSelectElement>) => void;
"aria-label"?: string;
};
const stopClickPropagation = (e: React.MouseEvent) => {
e.stopPropagation();
};
/**
* A dropdown implementation styled to look like `VSCodeDropdown`.
*
* The reason for doing this is that `VSCodeDropdown` doesn't handle fitting into
* available space and truncating content, and this leads to breaking the
* `VSCodeDataGrid` layout. This version using `select` directly will truncate the
* content as necessary and fit into whatever space is available.
* See https://github.com/github/vscode-codeql/pull/2582#issuecomment-1622164429
* for more info on the problem and other potential solutions.
*/
export function Dropdown({
value,
options,
disabled,
disabledPlaceholder,
className,
onChange,
...props
}: Props) {
const disabledValue = disabledPlaceholder ?? DISABLED_VALUE;
return (
<StyledDropdown
value={disabled ? disabledValue : value}
disabled={disabled}
onChange={onChange}
onClick={stopClickPropagation}
className={className}
{...props}
>
{disabled ? (
<option key={disabledValue} value={disabledValue}>
{disabledValue}
</option>
) : (
options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))
)}
</StyledDropdown>
);
}