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

feat(Select): 增加 filterTextHighlight 过滤文本是否高亮显示的配置项 #876

Merged
merged 1 commit into from
Aug 13, 2024
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
18 changes: 14 additions & 4 deletions components/select/optionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import VirtualList from '../virtual-list/virtualList';
import CheckOutlined from '../icon/CheckOutlined';
import { noop } from '../_util/utils';
import { useLocale } from '../config-provider/useLocale';
import TextHightlight from '../text-highlight';
import { PADDING_LEFT_BASE, PADDING_LEFT_INDENT } from './const';
import { selectProps } from './props';
import type { SelectOption, SelectValue } from './interface';
Expand Down Expand Up @@ -46,6 +47,8 @@ const optionListProps = {
renderOption: Function,
renderEmpty: Function,
hoverOptionValue: [String, Number, Object] as PropType<SelectValue>,
filterText: String,
filterTextHighlight: Boolean,
} as const satisfies ComponentObjectPropsOptions;

export default defineComponent({
Expand Down Expand Up @@ -91,7 +94,14 @@ export default defineComponent({
return (
<>
<Ellipsis class={`${prefixCls}-label`}>
{option.label}
{props.filterTextHighlight && props.filterText && !option.__cache
? (
<TextHightlight strict searchValues={[props.filterText]}>
{option.label}
</TextHightlight>
)
: option.label
}
{option.__cache && (
<span class={`${prefixCls}-label-tip`}>
- {t('select.tagOption')}
Expand Down Expand Up @@ -164,7 +174,7 @@ export default defineComponent({
const renderDefault = ({ source }: { source: SelectOption }) =>
source.__isGroup ? renderGroupOption(source) : renderOption(source);

const inValidValueKey = '_ALL_KEY_'
const inValidValueKey = '_ALL_KEY_';

return () =>
enableVirtualScroll.value
Expand Down Expand Up @@ -215,8 +225,8 @@ export default defineComponent({
: (
<div
class={[
`${props.prefixCls}-dropdown`,
`${props.prefixCls}-null`,
`${props.prefixCls}-dropdown`,
`${props.prefixCls}-null`,
]}
style={props.containerStyle}
>
Expand Down
4 changes: 4 additions & 0 deletions components/select/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export const selectProps = {
(pattern: string, option: object) => boolean
>,
},
filterTextHighlight: {
type: Boolean,
default: false,
},
collapseTags: {
type: Boolean,
default: false,
Expand Down
6 changes: 5 additions & 1 deletion components/select/select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
:renderOption="$slots.option"
:renderEmpty="$slots.empty"
:virtualScroll="virtualScroll"
:filterText="filterText"
:filterTextHighlight="filterTextHighlight"
@scroll="onScroll"
@mousedown.prevent
/>
Expand Down Expand Up @@ -95,7 +97,7 @@ export default defineComponent({
OptionList,
},
props: selectProps,
emits: [UPDATE_MODEL_EVENT, CHANGE_EVENT, 'removeTag', 'visibleChange', 'focus', 'blur', 'clear', 'scroll', 'search'],
emits: [UPDATE_MODEL_EVENT, CHANGE_EVENT, 'removeTag', 'visibleChange', 'focus', 'blur', 'clear', 'scroll', 'search', 'filter'],
setup(props, { emit }) {
useTheme();
const { validate, isError, isFormDisabled } = useFormAdaptor({
Expand Down Expand Up @@ -316,6 +318,7 @@ export default defineComponent({
},
) => {
filterText.value = val;
emit('filter', val);
// blur 自动清的 inputText 不触发 search
if (props.remote && !extraInfo?.isClear) {
emit('search', val);
Expand Down Expand Up @@ -420,6 +423,7 @@ export default defineComponent({
onHover,
onKeyDown,
warnDeprecatedSlot,
filterText,
};
},
});
Expand Down
143 changes: 110 additions & 33 deletions docs/.vitepress/components/select/filterable.vue
Original file line number Diff line number Diff line change
@@ -1,53 +1,130 @@
<template>
<FSpace>
<div>
默认:
<FSelect filterable clearable :options="optionList" />
</div>

<div>
自定义过滤函数:
<FSelect filterable :filter="filter" :options="optionList" />
</div>
</FSpace>
<FForm>
<FFormItem label="是否高亮:">
<FRadioGroup
v-model="filterTextHighlight"
:options="[
{ label: '否(默认)', value: false },
{ label: '是', value: true },
]"
/>
</FFormItem>
</FForm>

<FDivider />

<FForm :labelWidth="130">
<FFormItem label="默认:">
<FSelect filterable clearable :options="optionList" :filterTextHighlight="filterTextHighlight" />
</FFormItem>
<FFormItem label="自定义过滤函数:">
<FSelect filterable :filter="filter" :options="optionList" :filterTextHighlight="filterTextHighlight" />
</FFormItem>
<FFormItem label="创建新选项:">
<FSelect
style="width: 200px"
:options="cityOptions"
tag
filterable
multiple
filterTextHighlight
/>
</FFormItem>
<FFormItem label="自定义选项模板:">
<FSelect filterable @filter="(query) => { filterText = query }">
<FSelectGroupOption
v-for="group in cityOptions"
:key="group.label"
:label="group.label"
:disabled="group.disabled"
>
<FOption
v-for="item in group.children"
:key="item.label"
:value="item.value"
>
<FTextHighlight :searchValues="[filterText]" strict>{{ item.label }}</FTextHighlight>
</FOption>
</FSelectGroupOption>
</FSelect>
</FFormItem>
</FForm>
</template>

<script>
import { reactive } from 'vue';
<script setup>
import { reactive, ref } from 'vue';

const filterText = ref('');
const filterTextHighlight = ref(false);

const optionList = reactive([
{
value: 'HuNan',
label: '湖南',
},
{
value: 'HuBei',
label: '湖北',
disabled: true,
},
{
value: 'ZheJiang',
label: '浙江',
},
{
value: 'GuangDong',
label: '广东',
},
{
value: 'JiangSu',
label: '江苏',
},
]);

export default {
setup() {
const optionList = reactive([
const cityOptions = [
{
label: '华中地区',
children: [
{
value: 'HuNan',
value: '湖北',
label: '湖北',
},
{
value: '湖南',
label: '湖南',
},
{
value: 'HuBei',
label: '湖北',
disabled: true,
value: '河南',
label: '河南',
},
{
value: 'ZheJiang',
label: '浙江',
value: '江西',
label: '江西',
},
],
},
{
label: '华南地区',
disabled: true,
children: [
{
value: 'GuangDong',
value: '广东',
label: '广东',
},
{
value: 'JiangSu',
label: '江苏',
value: '广西',
label: '广西',
},
{
value: '海南',
label: '海南',
},
]);
const filter = (text, option) => {
return option.label.indexOf(text) !== -1;
};
return {
optionList,
filter,
};
],
},
];

const filter = (text, option) => {
return option.label.indexOf(text) !== -1;
};
</script>

Expand Down
Loading