Skip to content

Commit 795d6ab

Browse files
committed
fix: replace use of deprecated String.substr with String.startsWith (src/hooks/useMenuOptions.ts).
refactor: replace creation of Set object with Array object to hold collection of selected values (src/hooks/useMenuOptions.ts).
1 parent a53e542 commit 795d6ab

File tree

8 files changed

+26
-27
lines changed

8 files changed

+26
-27
lines changed

__stories__/helpers/styled/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ export const Li = styled.li`
131131

132132
export const TextHeader = styled.span`
133133
color: #476582;
134-
font-size: 95%;
134+
font-size: 94%;
135135
line-height: 1.7;
136136
border-radius: 4px;
137-
padding: .175em .5em;
137+
padding: .175em .475em;
138138
word-break: break-word;
139139
background-color: #f1f1f1;
140140
font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"@testing-library/react": "^13.4.0",
6969
"@testing-library/user-event": "^14.4.3",
7070
"@types/jest": "^29.2.1",
71-
"@types/node": "^18.11.8",
71+
"@types/node": "^18.11.9",
7272
"@types/react": "^18.0.24",
7373
"@types/react-dom": "^18.0.8",
7474
"@types/react-window": "^1.8.5",
@@ -78,7 +78,7 @@
7878
"babel-jest": "^29.2.2",
7979
"babel-loader": "^9.0.1",
8080
"babel-plugin-styled-components": "^2.0.7",
81-
"chromatic": "^6.11.3",
81+
"chromatic": "^6.11.4",
8282
"cross-env": "^7.0.3",
8383
"enzyme": "^3.11.0",
8484
"eslint": "^8.26.0",
@@ -94,10 +94,10 @@
9494
"react": "^18.2.0",
9595
"react-dom": "^18.2.0",
9696
"react-syntax-highlighter": "^15.5.0",
97-
"react-toastify": "^9.0.8",
97+
"react-toastify": "^9.1.0",
9898
"react-window": "^1.8.8",
9999
"rimraf": "^3.0.2",
100-
"rollup": "^3.2.4",
100+
"rollup": "^3.2.5",
101101
"rollup-plugin-terser": "^7.0.2",
102102
"styled-components": "^5.3.6",
103103
"typescript": "^4.8.4",
@@ -125,6 +125,6 @@
125125
"chromatic": "chromatic --force-rebuild --auto-accept-changes"
126126
},
127127
"dependencies": {
128-
"@babel/runtime": "^7.20.0"
128+
"@babel/runtime": "^7.20.1"
129129
}
130130
}

src/Select.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,6 @@ const Select = forwardRef<SelectRef, SelectProps>((
455455
*/
456456
useEffect(() => {
457457
const { current: isFunc } = onSearchChangeIsFunc;
458-
459458
if (isFunc && onChangeEvtValue.current) {
460459
onChangeEvtValue.current = false;
461460
onSearchChangeRef(debouncedInputValue);
@@ -563,7 +562,6 @@ const Select = forwardRef<SelectRef, SelectProps>((
563562
if (isDisabled) return;
564563

565564
const { key, shiftKey, defaultPrevented } = e;
566-
567565
if (onKeyDown) {
568566
onKeyDown(e, inputValue, focusedOption);
569567
if (defaultPrevented) return;

src/components/AutosizeInput/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ const AutosizeInput = memo(
7979
const [inputWidth, setInputWidth] = useState<number>(INPUT_MIN_WIDTH_PX);
8080

8181
useUpdateEffect(() => {
82-
const { current: el } = sizerRef;
83-
if (el) {
84-
setInputWidth(el.scrollWidth + INPUT_MIN_WIDTH_PX);
82+
if (sizerRef.current) {
83+
setInputWidth(sizerRef.current.scrollWidth + INPUT_MIN_WIDTH_PX);
8584
}
8685
}, [inputValue]);
8786

src/components/Menu/MenuList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const MenuList: FunctionComponent<MenuListProps> = ({
8181
>
8282
{Option}
8383
</FixedSizeList>
84-
{(!isArrayWithLength(menuOptions) && noOptionsMsg) && (
84+
{!isArrayWithLength(menuOptions) && noOptionsMsg && (
8585
<NoOptionsMsg>{noOptionsMsg}</NoOptionsMsg>
8686
)}
8787
</Fragment>

src/hooks/useMenuOptions.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,21 @@ const useMenuOptions = (
4242
const hideSelectedOptionsOrDefault = isBoolean(hideSelectedOptions) ? hideSelectedOptions : isMulti;
4343

4444
useEffect(() => {
45-
const selectedHash = selectedOption.length ? new Set(selectedOption.map((x) => x.value)) : null;
46-
const normalizedSearch = trimAndFormatFilterStr(searchValue, filterIgnoreCase, filterIgnoreAccents);
45+
const selectedValues = selectedOption.map((x) => x.value);
46+
const matchVal = trimAndFormatFilterStr(searchValue, filterIgnoreCase, filterIgnoreAccents);
4747

4848
const isOptionFilterMatch = (option: MenuOption): boolean => {
49-
if (!normalizedSearch) return true;
50-
49+
if (!matchVal) return true;
5150
const filterVal = getFilterOptionStringRef(option);
52-
const normalizedOptionLabel = trimAndFormatFilterStr(filterVal, filterIgnoreCase, filterIgnoreAccents);
53-
54-
return isFilterMatchAny
55-
? normalizedOptionLabel.includes(normalizedSearch)
56-
: normalizedOptionLabel.substr(0, normalizedSearch.length) === normalizedSearch;
51+
const filterValNrml = trimAndFormatFilterStr(filterVal, filterIgnoreCase, filterIgnoreAccents);
52+
return isFilterMatchAny ? filterValNrml.includes(matchVal) : filterValNrml.startsWith(matchVal);
5753
};
5854

5955
const parseMenuOption = (data: OptionData): MenuOption | undefined => {
6056
const value = getOptionValue(data);
6157
const label = getOptionLabel(data);
6258
const isDisabled = getIsOptionDisabledRef(data);
63-
const isSelected = selectedHash?.has(value) ?? false;
59+
const isSelected = selectedValues.includes(value);
6460
const menuOption: MenuOption = { data, value, label, isDisabled, isSelected };
6561

6662
if (!isOptionFilterMatch(menuOption) || (hideSelectedOptionsOrDefault && isSelected)) {

src/hooks/useMenuPositioner.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ const useMenuPositioner = (
3939
});
4040

4141
useEffect(() => {
42-
const { TOP, AUTO } = MenuPositionEnum;
43-
const isTopPos = menuPosition === TOP || (menuPosition === AUTO && !menuFitsBelowControl(menuRef.current));
42+
const isTopPos =
43+
menuPosition === MenuPositionEnum.TOP ||
44+
(menuPosition === MenuPositionEnum.AUTO && !menuFitsBelowControl(menuRef.current));
45+
4446
setIsMenuTopPosition(isTopPos);
4547
}, [menuRef, menuPosition]);
4648

@@ -55,7 +57,12 @@ const useMenuPositioner = (
5557
};
5658

5759
shouldScrollRef.current
58-
? scrollMenuIntoViewOnOpen(menuRef.current, menuScrollDuration, scrollMenuIntoView, handleOnMenuOpen)
60+
? scrollMenuIntoViewOnOpen(
61+
menuRef.current,
62+
menuScrollDuration,
63+
scrollMenuIntoView,
64+
handleOnMenuOpen
65+
)
5966
: handleOnMenuOpen();
6067
} else {
6168
onMenuCloseRef();

src/utils/common.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export const trimAndFormatFilterStr = (
4040
if (filterIgnoreCase) {
4141
trimVal = trimVal.toLowerCase();
4242
}
43-
4443
return !filterIgnoreAccents ? trimVal : stripDiacritics(trimVal);
4544
};
4645

0 commit comments

Comments
 (0)