Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: code optimization #1016

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/BaseSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { ScrollConfig, ScrollTo } from 'rc-virtual-list/lib/List';
import * as React from 'react';
import { useAllowClear } from './hooks/useAllowClear';
import { BaseSelectContext } from './hooks/useBaseProps';
import type { BaseSelectContextProps } from './hooks/useBaseProps';
import useDelayReset from './hooks/useDelayReset';
import useLock from './hooks/useLock';
import useSelectTriggerControl from './hooks/useSelectTriggerControl';
Expand Down Expand Up @@ -388,7 +389,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
);

// ============================= Search =============================
const tokenWithEnter = React.useMemo(
const tokenWithEnter = React.useMemo<boolean>(
() => (tokenSeparators || []).some((tokenSeparator) => ['\n', '\r\n'].includes(tokenSeparator)),
[tokenSeparators],
);
Expand Down Expand Up @@ -513,17 +514,17 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
}
}

if (mergedOpen && listRef.current) {
listRef.current.onKeyDown(event, ...rest);
if (mergedOpen) {
listRef.current?.onKeyDown(event, ...rest);
}

onKeyDown?.(event, ...rest);
};

// KeyUp
const onInternalKeyUp: React.KeyboardEventHandler<HTMLDivElement> = (event, ...rest) => {
if (mergedOpen && listRef.current) {
listRef.current.onKeyUp(event, ...rest);
if (mergedOpen) {
listRef.current?.onKeyUp(event, ...rest);
}

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

// ============================ Context =============================
const baseSelectContext = React.useMemo(
const baseSelectContext = React.useMemo<BaseSelectContextProps>(
() => ({
...props,
notFoundContent,
Expand Down
8 changes: 3 additions & 5 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,16 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
const listRef = React.useRef<ListRef>(null);

const overMaxCount = React.useMemo<boolean>(
() => multiple && typeof maxCount !== 'undefined' && rawValues.size >= maxCount,
[multiple, maxCount, rawValues.size],
() => multiple && typeof maxCount !== 'undefined' && rawValues?.size >= maxCount,
[multiple, maxCount, rawValues?.size],
);

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

const scrollIntoView = (args: number | ScrollConfig) => {
if (listRef.current) {
listRef.current.scrollTo(typeof args === 'number' ? { index: args } : args);
}
listRef.current?.scrollTo(typeof args === 'number' ? { index: args } : args);
};

// ========================== Active ==========================
Expand Down
5 changes: 2 additions & 3 deletions src/Selector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,8 @@ const Selector: React.ForwardRefRenderFunction<RefSelectorProps, SelectorProps>

const onInputPaste: React.ClipboardEventHandler = (e) => {
const { clipboardData } = e;
const value = clipboardData.getData('text');

pastedTextRef.current = value;
const value = clipboardData?.getData('text');
pastedTextRef.current = value || '';
};

const onClick = ({ target }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/valueUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function injectPropsWithOption<T extends object>(option: T): T {
return newOption;
}

export const getSeparatedContent = (text: string, tokens: string[]): string[] | null => {
export const getSeparatedContent = (text: string, tokens: string[]): string[] => {
if (!tokens || !tokens.length) {
return null;
}
Expand Down
Loading