Skip to content

Commit 312c791

Browse files
authored
chore: code optimization (#1016)
* chore: code optimization * demo: update demo
1 parent 2cb2682 commit 312c791

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

src/BaseSelect.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { ScrollConfig, ScrollTo } from 'rc-virtual-list/lib/List';
99
import * as React from 'react';
1010
import { useAllowClear } from './hooks/useAllowClear';
1111
import { BaseSelectContext } from './hooks/useBaseProps';
12+
import type { BaseSelectContextProps } from './hooks/useBaseProps';
1213
import useDelayReset from './hooks/useDelayReset';
1314
import useLock from './hooks/useLock';
1415
import useSelectTriggerControl from './hooks/useSelectTriggerControl';
@@ -388,7 +389,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
388389
);
389390

390391
// ============================= Search =============================
391-
const tokenWithEnter = React.useMemo(
392+
const tokenWithEnter = React.useMemo<boolean>(
392393
() => (tokenSeparators || []).some((tokenSeparator) => ['\n', '\r\n'].includes(tokenSeparator)),
393394
[tokenSeparators],
394395
);
@@ -513,17 +514,17 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
513514
}
514515
}
515516

516-
if (mergedOpen && listRef.current) {
517-
listRef.current.onKeyDown(event, ...rest);
517+
if (mergedOpen) {
518+
listRef.current?.onKeyDown(event, ...rest);
518519
}
519520

520521
onKeyDown?.(event, ...rest);
521522
};
522523

523524
// KeyUp
524525
const onInternalKeyUp: React.KeyboardEventHandler<HTMLDivElement> = (event, ...rest) => {
525-
if (mergedOpen && listRef.current) {
526-
listRef.current.onKeyUp(event, ...rest);
526+
if (mergedOpen) {
527+
listRef.current?.onKeyUp(event, ...rest);
527528
}
528529

529530
onKeyUp?.(event, ...rest);
@@ -649,7 +650,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
649650
);
650651

651652
// ============================ Context =============================
652-
const baseSelectContext = React.useMemo(
653+
const baseSelectContext = React.useMemo<BaseSelectContextProps>(
653654
() => ({
654655
...props,
655656
notFoundContent,

src/OptionList.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,16 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
7272
const listRef = React.useRef<ListRef>(null);
7373

7474
const overMaxCount = React.useMemo<boolean>(
75-
() => multiple && typeof maxCount !== 'undefined' && rawValues.size >= maxCount,
76-
[multiple, maxCount, rawValues.size],
75+
() => multiple && typeof maxCount !== 'undefined' && rawValues?.size >= maxCount,
76+
[multiple, maxCount, rawValues?.size],
7777
);
7878

7979
const onListMouseDown: React.MouseEventHandler<HTMLDivElement> = (event) => {
8080
event.preventDefault();
8181
};
8282

8383
const scrollIntoView = (args: number | ScrollConfig) => {
84-
if (listRef.current) {
85-
listRef.current.scrollTo(typeof args === 'number' ? { index: args } : args);
86-
}
84+
listRef.current?.scrollTo(typeof args === 'number' ? { index: args } : args);
8785
};
8886

8987
// ========================== Active ==========================

src/Selector/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,8 @@ const Selector: React.ForwardRefRenderFunction<RefSelectorProps, SelectorProps>
207207

208208
const onInputPaste: React.ClipboardEventHandler = (e) => {
209209
const { clipboardData } = e;
210-
const value = clipboardData.getData('text');
211-
212-
pastedTextRef.current = value;
210+
const value = clipboardData?.getData('text');
211+
pastedTextRef.current = value || '';
213212
};
214213

215214
const onClick = ({ target }) => {

src/utils/valueUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export function injectPropsWithOption<T extends object>(option: T): T {
111111
return newOption;
112112
}
113113

114-
export const getSeparatedContent = (text: string, tokens: string[]): string[] | null => {
114+
export const getSeparatedContent = (text: string, tokens: string[]): string[] => {
115115
if (!tokens || !tokens.length) {
116116
return null;
117117
}

0 commit comments

Comments
 (0)