forked from dittofeed/dittofeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolled-select-reference.ts
More file actions
74 lines (64 loc) · 1.94 KB
/
controlled-select-reference.ts
File metadata and controls
74 lines (64 loc) · 1.94 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
import React, { useState, MouseEvent, TouchEvent, SyntheticEvent } from 'react';
import {
FormControl,
InputLabel,
Select,
MenuItem,
SelectChangeEvent,
} from '@mui/material';
const stopPropagation = (e: MouseEvent | TouchEvent | SyntheticEvent) => {
e.stopPropagation();
};
interface FlowSelectProps {
options: { value: string; label: string }[];
}
const FlowSelect: React.FC<FlowSelectProps> = ({ options }) => {
const [value, setValue] = useState<string>('');
const [open, setOpen] = useState<boolean>(false);
const handleChange = (e: SelectChangeEvent<string>) => {
setValue(e.target.value);
};
const handleOpen = (e: SyntheticEvent) => {
stopPropagation(e);
setOpen(true);
};
const handleClose = (e: SyntheticEvent) => {
stopPropagation(e);
setOpen(false);
};
return (
<FormControl fullWidth size="small">
<InputLabel id="flow-select-label">Choose</InputLabel>
<Select
labelId="flow-select-label"
id="flow-select"
value={value}
open={open}
onOpen={handleOpen}
onClose={handleClose}
onChange={handleChange}
// prevent React Flow from hijacking pointer events on the trigger
onMouseDownCapture={stopPropagation}
onTouchStartCapture={stopPropagation}
onPointerDownCapture={stopPropagation}
MenuProps={{
// render the menu inside the node so its events bubble through our wrapper
disablePortal: true,
PaperProps: {
// also block pointer events coming *out* of the menu itself
onMouseDownCapture: stopPropagation,
onTouchStartCapture: stopPropagation,
onPointerDownCapture: stopPropagation,
},
}}
>
{options.map((opt) => (
<MenuItem key={opt.value} value={opt.value}>
{opt.label}
</MenuItem>
))}
</Select>
</FormControl>
);
};
export default FlowSelect;