@@ -9,18 +9,15 @@ export type DeviceEmulatorProps = React.HTMLAttributes<HTMLDivElement> & {
9
9
10
10
export const DeviceEmulator = React . memo < DeviceEmulatorProps > ( function DeviceEmulator ( { children, banDevices = [ ] , ...divProps } ) {
11
11
const deviceNames = useMemo ( ( ) => DeviceNames . filter ( devName => ! banDevices . includes ( devName ) ) as Array < keyof typeof DeviceOptions > , [ ] )
12
- const [ selectedDeviceIndex , setSelectedDeviceIndex ] = useState ( 0 )
12
+ const [ deviceName , setDeviceName ] = useState < DeviceName > ( deviceNames [ 0 ] ?? '' )
13
13
14
14
const handleSelectChange = useCallback (
15
15
( event : React . ChangeEvent < HTMLSelectElement > ) => {
16
- const index = + event . target . value
17
- setSelectedDeviceIndex ( index )
16
+ setDeviceName ( event . target . value as DeviceName )
18
17
} ,
19
18
[ ] ,
20
19
)
21
20
22
- const deviceName = useMemo ( ( ) => deviceNames [ selectedDeviceIndex ] , [ selectedDeviceIndex ] )
23
-
24
21
const { colors, hasLandscape, width, height } = useMemo ( ( ) => DeviceOptions [ deviceName ] , [ deviceName ] )
25
22
26
23
const [ selectedColorIndex , setSelectedColorIndex ] = useState ( 0 )
@@ -37,22 +34,25 @@ export const DeviceEmulator = React.memo<DeviceEmulatorProps>(function DeviceEmu
37
34
38
35
const selectedColor = useMemo ( ( ) => colors [ selectedColorIndex ] , [ colors , selectedColorIndex ] )
39
36
40
- const props = {
41
- device : deviceName ,
42
- color : selectedColor ,
43
- landscape : isLandscape ,
44
- width,
45
- height,
46
- } as DeviceFramesetProps
37
+ const deviceFramesetProps = useMemo (
38
+ ( ) => ( {
39
+ device : deviceName ,
40
+ color : selectedColor ,
41
+ landscape : isLandscape ,
42
+ width,
43
+ height,
44
+ } ) as DeviceFramesetProps ,
45
+ [ deviceName , selectedColor , isLandscape , width , height ] ,
46
+ )
47
47
48
48
return (
49
49
< div className = "device-emulator" { ...divProps } >
50
50
< section >
51
51
< select value = { deviceName } onChange = { handleSelectChange } >
52
- { deviceNames . map ( ( devName , index ) => (
52
+ { deviceNames . map ( ( devName ) => (
53
53
< option
54
54
key = { devName }
55
- value = { index }
55
+ value = { devName }
56
56
> { devName } </ option >
57
57
) ) }
58
58
</ select >
@@ -64,7 +64,7 @@ export const DeviceEmulator = React.memo<DeviceEmulatorProps>(function DeviceEmu
64
64
</ section >
65
65
66
66
< div className = "device-emulator-container" >
67
- { children ( props ) }
67
+ { children ( deviceFramesetProps ) }
68
68
</ div >
69
69
</ div >
70
70
)
0 commit comments