Skip to content

Commit a01af8f

Browse files
committed
fix: emulator select value
1 parent e0c4313 commit a01af8f

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"typescript"
2323
],
2424
"repository": "https://github.com/zheeeng/react-device-frameset",
25-
"version": "1.0.8",
25+
"version": "1.0.9",
2626
"license": "MIT",
2727
"main": "lib/DeviceFrameset.js",
2828
"scripts": {

src/DeviceEmulator.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@ export type DeviceEmulatorProps = React.HTMLAttributes<HTMLDivElement> & {
99

1010
export const DeviceEmulator = React.memo<DeviceEmulatorProps>(function DeviceEmulator ({ children, banDevices = [], ...divProps }) {
1111
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] ?? '')
1313

1414
const handleSelectChange = useCallback(
1515
(event: React.ChangeEvent<HTMLSelectElement>) => {
16-
const index = +event.target.value
17-
setSelectedDeviceIndex(index)
16+
setDeviceName(event.target.value as DeviceName)
1817
},
1918
[],
2019
)
2120

22-
const deviceName = useMemo(() => deviceNames[selectedDeviceIndex], [selectedDeviceIndex])
23-
2421
const { colors, hasLandscape, width, height } = useMemo(() => DeviceOptions[deviceName], [deviceName])
2522

2623
const [selectedColorIndex, setSelectedColorIndex] = useState(0)
@@ -37,22 +34,25 @@ export const DeviceEmulator = React.memo<DeviceEmulatorProps>(function DeviceEmu
3734

3835
const selectedColor = useMemo(() => colors[selectedColorIndex], [colors, selectedColorIndex])
3936

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+
)
4747

4848
return (
4949
<div className="device-emulator" {...divProps}>
5050
<section>
5151
<select value={deviceName} onChange={handleSelectChange}>
52-
{deviceNames.map((devName, index) => (
52+
{deviceNames.map((devName) => (
5353
<option
5454
key={devName}
55-
value={index}
55+
value={devName}
5656
>{devName}</option>
5757
))}
5858
</select>
@@ -64,7 +64,7 @@ export const DeviceEmulator = React.memo<DeviceEmulatorProps>(function DeviceEmu
6464
</section>
6565

6666
<div className="device-emulator-container">
67-
{children(props)}
67+
{children(deviceFramesetProps)}
6868
</div>
6969
</div>
7070
)

src/DeviceSelector.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ export const DeviceSelector = React.memo<DeviceSelectorProps>(function DeviceSel
3030

3131
const selectedColor = useMemo(() => colors[selectedColorIndex], [colors, selectedColorIndex])
3232

33-
const props = {
34-
device: deviceName,
35-
color: selectedColor,
36-
landscape: isLandscape,
37-
width,
38-
height,
39-
} as DeviceFramesetProps
33+
const deviceFramesetProps = useMemo(
34+
() => ({
35+
device: deviceName,
36+
color: selectedColor,
37+
landscape: isLandscape,
38+
width,
39+
height,
40+
}) as DeviceFramesetProps,
41+
[deviceName, selectedColor, isLandscape, width, height],
42+
)
4043

4144
return (
4245
<div className="device-selector" {...divProps}>
@@ -87,7 +90,7 @@ export const DeviceSelector = React.memo<DeviceSelectorProps>(function DeviceSel
8790
</dl>
8891

8992
<div className="device-selector-container">
90-
{children(props)}
93+
{children(deviceFramesetProps)}
9194
</div>
9295
</div>
9396
)

0 commit comments

Comments
 (0)