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

fix(select): checkAll will check all filtered data of reserveKeyword #5012

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/components/select/hooks/useSelectOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const useSelectOptions = (props: TdSelectProps, keys: Ref<KeysType>, inpu
if (isFunction(props.filter)) {
return props.filter(`${inputValue.value}`, option);
}

if ((option as TdOptionProps)?.checkAll === true) return true;
return option.label?.toLowerCase?.().indexOf(`${inputValue.value}`.toLowerCase()) > -1;
};

Expand Down
41 changes: 28 additions & 13 deletions packages/components/select/select.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { defineComponent, provide, computed, toRefs, watch, ref, nextTick, PropType } from 'vue';
import { pick as picker } from 'lodash-es';
import { isArray } from 'lodash-es';
import { isFunction } from 'lodash-es';
import { debounce } from 'lodash-es';
import { cloneDeep } from 'lodash-es';
import { get } from 'lodash-es';
import { intersection } from 'lodash-es';
import { pick as picker, isArray, isFunction, debounce, cloneDeep, get, intersection } from 'lodash-es';
import FakeArrow from '../common-components/fake-arrow';
import SelectInput from '../select-input';
import SelectPanel from './select-panel';
Expand Down Expand Up @@ -215,6 +209,24 @@ export default defineComponent({
max: props.max,
});

/**
* 获取过滤后可以全选的选项
* @returns 可选选项的列表的 value
*/
const getFilteredOptions = computed(() => {
const inputValue = innerInputValue.value?.toLowerCase();
return optionalList.value
.filter((option) => {
if (option.disabled) return false;
if (typeof props.filter === 'function') {
return props.filter(innerInputValue.value, option);
}

return option.label?.toLowerCase().includes(inputValue);
})
.map((option) => option.value);
});

/*
* 全选逻辑:
* 根据 checked 的值计算最终选中的值:
Expand All @@ -224,23 +236,26 @@ export default defineComponent({
const onCheckAllChange = (checked: boolean) => {
if (!props.multiple) return;
const lockedValues = innerValue.value.filter((value: string | number | boolean) => {
return optionsList.value.find((item) => item.value === value && item.disabled);
return optionsList.value.some((item) => item.value === value && item.disabled);
});
const activeValues = optionalList.value.map((option) => option.value);
const values = checked ? [...new Set([...activeValues, ...lockedValues])] : [...lockedValues];
const values = checked ? [...new Set([...getFilteredOptions.value, ...lockedValues])] : lockedValues;
setInnerValue(values, { selectedOptions: getSelectedOptions(values), trigger: checked ? 'check' : 'clear' });
};

// 已选的长度
const intersectionLen = computed<number>(() => {
const intersectionLen = computed(() => {
const values = optionalList.value.map((item) => item.value);
const n = intersection(innerValue.value, values);
return n.length;
});

// 全选
const isCheckAll = computed<boolean>(() => {
return intersectionLen.value === optionalList.value.length;
const isCheckAll = computed(() => {
const filtered = getFilteredOptions.value;
if (filtered.length === 0) return false;

const selectedSet = new Set(innerValue.value);
return filtered.every((v) => selectedSet.has(v));
});

// 半选
Expand Down