Skip to content

Commit

Permalink
chore: code optimization (#1016)
Browse files Browse the repository at this point in the history
* chore: code optimization

* demo: update demo
  • Loading branch information
li-jia-nan committed Dec 29, 2023
1 parent 2cb2682 commit 312c791
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
13 changes: 7 additions & 6 deletions src/BaseSelect.tsx
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
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
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
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

1 comment on commit 312c791

@vercel
Copy link

@vercel vercel bot commented on 312c791 Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

select – ./

select-react-component.vercel.app
select-git-master-react-component.vercel.app
select.vercel.app

Please sign in to comment.