Skip to content

Commit

Permalink
chore: add isValidCount (react-component#1017)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan authored Jan 3, 2024
1 parent 9a7fc96 commit c93ff10
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/BaseSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Selector from './Selector';
import type { RefTriggerProps } from './SelectTrigger';
import SelectTrigger from './SelectTrigger';
import TransBtn from './TransBtn';
import { getSeparatedContent } from './utils/valueUtil';
import { getSeparatedContent, isValidCount } from './utils/valueUtil';
import SelectContext from './SelectContext';
import type { SelectContextProps } from './SelectContext';

Expand Down Expand Up @@ -399,7 +399,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
const { maxCount, rawValues } = React.useContext<SelectContextProps>(SelectContext) || {};

const onInternalSearch = (searchText: string, fromTyping: boolean, isCompositing: boolean) => {
if (rawValues?.size >= maxCount) {
if (isValidCount(maxCount) && rawValues?.size >= maxCount) {
return;
}
let ret = true;
Expand All @@ -409,7 +409,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
const separatedList = getSeparatedContent(
searchText,
tokenSeparators,
maxCount && maxCount - rawValues.size,
isValidCount(maxCount) ? maxCount - rawValues.size : undefined,
);

// Check if match the `tokenSeparators`
Expand Down
3 changes: 2 additions & 1 deletion src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import TransBtn from './TransBtn';
import useBaseProps from './hooks/useBaseProps';
import type { FlattenOptionData } from './interface';
import { isPlatformMac } from './utils/platformUtil';
import { isValidCount } from './utils/valueUtil';

// export interface OptionListProps<OptionsType extends object[]> {
export type OptionListProps = Record<string, never>;
Expand Down Expand Up @@ -72,7 +73,7 @@ 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 && isValidCount(maxCount) && rawValues?.size >= maxCount,
[multiple, maxCount, rawValues?.size],
);

Expand Down
4 changes: 4 additions & 0 deletions src/utils/valueUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function getKey(data: BaseOptionType, index: number) {
return `rc-index-key-${index}`;
}

export function isValidCount(value?: number) {
return typeof value !== 'undefined' && !Number.isNaN(value);
}

export function fillFieldNames(fieldNames: FieldNames | undefined, childrenAsData: boolean) {
const { label, value, options, groupLabel } = fieldNames || {};
const mergedLabel = label || (childrenAsData ? 'children' : 'label');
Expand Down

0 comments on commit c93ff10

Please sign in to comment.