fix: Unexpected error on simple filter (#22814)

This commit is contained in:
Michael S. Molina 2023-01-23 10:25:28 -05:00 committed by GitHub
parent 0b31b2cb87
commit d479009e35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 11 deletions

View File

@ -808,14 +808,21 @@ test('"Select All" is checked when unchecking a newly added option and all the o
});
test('does not render "Select All" when there are 0 or 1 options', async () => {
render(
const { rerender } = render(
<Select {...defaultProps} options={[]} mode="multiple" allowNewOptions />,
);
await open();
expect(screen.queryByText(selectAllOptionLabel(0))).not.toBeInTheDocument();
await type(`${NEW_OPTION}{enter}`);
rerender(
<Select
{...defaultProps}
options={OPTIONS.slice(0, 1)}
mode="multiple"
allowNewOptions
/>,
);
expect(screen.queryByText(selectAllOptionLabel(1))).not.toBeInTheDocument();
await type(`Kyle2{enter}`);
await type(`${NEW_OPTION}{enter}`);
expect(screen.queryByText(selectAllOptionLabel(2))).toBeInTheDocument();
});

View File

@ -178,8 +178,17 @@ const Select = forwardRef(
}, [selectOptions, selectValue]);
const selectAllEnabled = useMemo(
() => !isSingleMode && fullSelectOptions.length > 1 && !inputValue,
[fullSelectOptions, isSingleMode, inputValue],
() =>
!isSingleMode &&
selectOptions.length > 0 &&
fullSelectOptions.length > 1 &&
!inputValue,
[
isSingleMode,
selectOptions.length,
fullSelectOptions.length,
inputValue,
],
);
const selectAllMode = useMemo(
@ -329,7 +338,7 @@ const Select = forwardRef(
if (
!isSingleMode &&
ensureIsArray(value).length === fullSelectOptions.length &&
fullSelectOptions.length > 0
selectOptions.length > 0
) {
setSelectValue(
labelInValue
@ -340,18 +349,24 @@ const Select = forwardRef(
] as AntdLabeledValue[]),
);
}
}, [value, isSingleMode, labelInValue, fullSelectOptions.length]);
}, [
value,
isSingleMode,
labelInValue,
fullSelectOptions.length,
selectOptions.length,
]);
useEffect(() => {
const checkSelectAll = ensureIsArray(selectValue).some(
v => getValue(v) === SELECT_ALL_VALUE,
);
if (checkSelectAll && !selectAllMode) {
setSelectValue(
labelInValue
? ([...fullSelectOptions, selectAllOption] as AntdLabeledValue[])
: ([...fullSelectOptions, SELECT_ALL_VALUE] as AntdLabeledValue[]),
const optionsToSelect = fullSelectOptions.map(option =>
labelInValue ? option : option.value,
);
optionsToSelect.push(labelInValue ? selectAllOption : SELECT_ALL_VALUE);
setSelectValue(optionsToSelect);
}
}, [selectValue, selectAllMode, labelInValue, fullSelectOptions]);