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(auto-complete): using lodash escapeRegExp transform keyword text #2943

Merged
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
4 changes: 3 additions & 1 deletion src/auto-complete/_example/filter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
</template>

<script>
import escapeRegExp from 'lodash/escapeRegExp';

const LIST = ['第一个 AutoComplete 默认联想词', '第二个 AutoComplete 默认联想词', '第三个 AutoComplete 默认联想词'];

export default {
Expand All @@ -39,7 +41,7 @@ export default {

methods: {
filterWords(keyword, option) {
const regExp = new RegExp(keyword);
const regExp = new RegExp(escapeRegExp(keyword));
return regExp.test(option.text);
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/auto-complete/highlight-option.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { computed, defineComponent } from '@vue/composition-api';
import escapeRegExp from 'lodash/escapeRegExp';
import { usePrefixClass } from '../hooks/useConfig';

export default defineComponent({
Expand All @@ -17,7 +18,7 @@ export default defineComponent({
const { content, keyword } = props;
if (!content) return { list: [] };
if (typeof content !== 'string' || !keyword) return { list: [content] };
const regExp = new RegExp(keyword, 'i');
const regExp = new RegExp(escapeRegExp(keyword), 'i');
const splitKeyword = content.match(regExp)?.[0];
return {
list: content.split(splitKeyword),
Expand Down
3 changes: 2 additions & 1 deletion src/auto-complete/option-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ref, computed, defineComponent, PropType, h, watch, onBeforeUnmount,
} from '@vue/composition-api';
import isFunction from 'lodash/isFunction';
import escapeRegExp from 'lodash/escapeRegExp';
import HighlightOption from './highlight-option';
import { CommonClassNameType } from '../hooks/useCommonClassName';
import { AutoCompleteOptionObj, TdAutoCompleteProps } from './type';
Expand Down Expand Up @@ -62,7 +63,7 @@ export default defineComponent({
options = options.filter((option) => props.filter(props.value, option));
} else if (props.filterable) {
// 默认过滤规则
const regExp = new RegExp(props.value, 'i');
const regExp = new RegExp(escapeRegExp(props.value), 'i');
options = options.filter((item) => regExp.test(item.text));
}
return options;
Expand Down
Loading