Skip to content

Commit 860712e

Browse files
authored
fix: SelectBox bug (#404)
1 parent 0c44353 commit 860712e

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

src/components/input-elements/Dropdown/Dropdown.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef, useState } from "react";
1+
import React, { useMemo, useRef, useState } from "react";
22
import { twMerge } from "tailwind-merge";
33

44
import { HoveredValueContext, SelectedValueContext } from "contexts";
@@ -55,7 +55,7 @@ const Dropdown = React.forwardRef<HTMLDivElement, DropdownProps>((props, ref) =>
5555
const dropdownRef = useRef(null);
5656

5757
const Icon = icon;
58-
const valueToNameMapping = constructValueToNameMapping(children);
58+
const valueToNameMapping = useMemo(() => constructValueToNameMapping(children), [children]);
5959
const optionValues = React.Children.map(
6060
children,
6161
(child: { props: DropdownItemProps }) => child.props.value,

src/components/input-elements/SelectBox/SelectBox.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef, useState } from "react";
1+
import React, { useEffect, useMemo, useRef, useState } from "react";
22
import { twMerge } from "tailwind-merge";
33

44
import { ArrowDownHeadIcon } from "assets";
@@ -55,16 +55,16 @@ const SelectBox = React.forwardRef<HTMLDivElement, SelectBoxProps>((props, ref)
5555
onKeyDown,
5656
...other
5757
} = props;
58+
const valueToNameMapping = useMemo(() => constructValueToNameMapping(children), [children]);
5859

5960
const [selectedValue, setSelectedValue] = useInternalState(defaultValue, value);
60-
const [inputValue, setInputValue] = useState("");
61+
const [inputValue, setInputValue] = useState(valueToNameMapping.get(selectedValue || "") || "");
6162
const [searchQuery, setSearchQuery] = useState("");
6263
const [isFocused, setIsFocused] = useState(false);
6364

6465
const dropdownRef = useRef<HTMLDivElement>(null);
6566
const inputRef = useRef<HTMLInputElement>(null);
6667
const Icon = icon;
67-
const valueToNameMapping = constructValueToNameMapping(children);
6868
const hasSelection = hasValue(selectedValue);
6969

7070
useEffect(() => {
@@ -85,13 +85,17 @@ const SelectBox = React.forwardRef<HTMLDivElement, SelectBoxProps>((props, ref)
8585
inputRef.current?.blur();
8686
} else {
8787
inputRef.current?.focus();
88+
if (inputRef.current) {
89+
inputRef.current.selectionStart = inputRef.current.value.length;
90+
inputRef.current.selectionEnd = inputRef.current.value.length;
91+
}
8892
}
8993
setIsFocused(isFocused);
9094
};
9195

9296
const handleValueChange = (value: string) => {
9397
setSearchQuery("");
94-
if (selectedValue !== undefined) setInputValue(valueToNameMapping.get(selectedValue) || "");
98+
if (value !== undefined) setInputValue(valueToNameMapping.get(value) || "");
9599
handleFocusChange(false);
96100
setSelectedValue(value);
97101
inputRef.current?.blur();

src/stories/input-elements/Dropdown.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ const WithControlledStateTemplate: ComponentStory<typeof Dropdown> = () => {
9292
</Dropdown>
9393
<Button onClick={() => setValue("")}>Reset</Button>
9494
<Button onClick={() => setValue("1")}>Set to One</Button>
95+
<Text>{value}</Text>
9596
</Card>
9697
);
9798
};
9899

99100
export const DefaultResponsive = ResponsiveTemplate.bind({});
101+
DefaultResponsive.args = {
102+
onValueChange: (v) => alert(v),
103+
};
100104

101105
export const WithFlexParent = FlexTemplate.bind({});
102106
WithFlexParent.args = {

src/stories/input-elements/SelectBox.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const WithControlledStateTemplate: ComponentStory<typeof SelectBox> = () => {
9494
</SelectBox>
9595
<Button onClick={() => setValue("")}>Reset</Button>
9696
<Button onClick={() => setValue("1")}>One</Button>
97+
<Text>{value}</Text>
9798
</Card>
9899
);
99100
};

0 commit comments

Comments
 (0)