chore: improve filter (#8262)

This commit is contained in:
neverland 2021-03-04 11:27:39 +08:00 committed by GitHub
parent 9fd71923f9
commit 12be46fad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 31 additions and 37 deletions

View File

@ -139,7 +139,7 @@ export default createComponent({
if (province && province === city) { if (province && province === city) {
arr.splice(1, 1); arr.splice(1, 1);
} }
return arr.filter((text) => text).join('/'); return arr.filter(Boolean).join('/');
} }
return ''; return '';
}); });
@ -223,7 +223,7 @@ export default createComponent({
}; };
const onAreaConfirm = (values: AreaColumnOption[]) => { const onAreaConfirm = (values: AreaColumnOption[]) => {
values = values.filter((value) => !!value); values = values.filter(Boolean);
if (values.some((value) => !value.code)) { if (values.some((value) => !value.code)) {
Toast(t('areaEmpty')); Toast(t('areaEmpty'));

View File

@ -221,9 +221,7 @@ export default createComponent({
const getValues = () => { const getValues = () => {
if (pickerRef.value) { if (pickerRef.value) {
const values = pickerRef.value const values = pickerRef.value.getValues().filter(Boolean);
.getValues()
.filter((value: AreaColumnOption) => !!value);
return parseValues(values); return parseValues(values);
} }
return []; return [];
@ -244,7 +242,7 @@ export default createComponent({
} }
const names = values.map((item) => item.name); const names = values.map((item) => item.name);
const validValues = values.filter((value) => !!value.code); const validValues = values.filter((value) => value.code);
area.code = validValues.length area.code = validValues.length
? validValues[validValues.length - 1].code ? validValues[validValues.length - 1].code

View File

@ -98,11 +98,11 @@ export default createComponent({
selectedOption: option, selectedOption: option,
}; };
const next = optionsCursor.filter( const next = optionsCursor.find(
(item) => item[valueKey] === option[valueKey] (item) => item[valueKey] === option[valueKey]
); );
if (next.length) { if (next) {
optionsCursor = next[0][childrenKey]; optionsCursor = next[childrenKey];
} }
return tab; return tab;
@ -157,7 +157,7 @@ export default createComponent({
const selectedOptions = state.tabs const selectedOptions = state.tabs
.map((tab) => tab.selectedOption) .map((tab) => tab.selectedOption)
.filter((item) => !!item); .filter(Boolean);
const eventParams = { const eventParams = {
value: option[valueKey], value: option[valueKey],

View File

@ -167,10 +167,9 @@ export default createComponent({
const originColumns = computed(() => const originColumns = computed(() =>
ranges.value.map(({ type, range: rangeArr }) => { ranges.value.map(({ type, range: rangeArr }) => {
let values = times(rangeArr[1] - rangeArr[0] + 1, (index) => { let values = times(rangeArr[1] - rangeArr[0] + 1, (index) =>
const value = padZero(rangeArr[0] + index); padZero(rangeArr[0] + index)
return value; );
});
if (props.filter) { if (props.filter) {
values = props.filter(type, values); values = props.filter(type, values);
@ -290,10 +289,7 @@ export default createComponent({
emit('update:modelValue', oldValue ? value : null) emit('update:modelValue', oldValue ? value : null)
); );
watch( watch(() => [props.filter, props.minDate, props.maxDate], updateInnerValue);
[() => props.filter, () => props.minDate, () => props.maxDate],
updateInnerValue
);
watch( watch(
() => props.modelValue, () => props.modelValue,

View File

@ -137,12 +137,12 @@ export default createComponent({
watch(columns, updateColumnValue); watch(columns, updateColumnValue);
watch( watch(
[ () => [
() => props.filter, props.filter,
() => props.minHour, props.minHour,
() => props.maxHour, props.maxHour,
() => props.minMinute, props.minMinute,
() => props.maxMinute, props.maxMinute,
], ],
updateInnerValue updateInnerValue
); );

View File

@ -106,11 +106,11 @@ export default createComponent({
return props.title; return props.title;
} }
const match = props.options.filter( const match = props.options.find(
(option) => option.value === props.modelValue (option) => option.value === props.modelValue
); );
return match.length ? match[0].text : ''; return match ? match.text : '';
}; };
const renderOption = (option: DropdownItemOption) => { const renderOption = (option: DropdownItemOption) => {

View File

@ -87,7 +87,7 @@ export default createComponent({
new Promise<void>((resolve, reject) => { new Promise<void>((resolve, reject) => {
const fields = getFieldsByNames(names); const fields = getFieldsByNames(names);
Promise.all(fields.map((item) => item.validate())).then((errors) => { Promise.all(fields.map((item) => item.validate())).then((errors) => {
errors = errors.filter((item) => item); errors = errors.filter(Boolean);
if (errors.length) { if (errors.length) {
reject(errors); reject(errors);
@ -98,11 +98,11 @@ export default createComponent({
}); });
const validateField = (name: string) => { const validateField = (name: string) => {
const matched = children.filter((item) => item.name === name); const matched = children.find((item) => item.name === name);
if (matched.length) { if (matched) {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
matched[0].validate().then((error?: FieldValidateError) => { matched.validate().then((error?: FieldValidateError) => {
if (error) { if (error) {
reject(error); reject(error);
} else { } else {

View File

@ -206,16 +206,16 @@ export default createComponent({
return; return;
} }
const match = children.filter((item) => String(item.index) === index); const match = children.find((item) => String(item.index) === index);
if (match[0]) { if (match) {
match[0].$el.scrollIntoView(); match.$el.scrollIntoView();
if (props.sticky && props.stickyOffsetTop) { if (props.sticky && props.stickyOffsetTop) {
setRootScrollTop(getRootScrollTop() - props.stickyOffsetTop); setRootScrollTop(getRootScrollTop() - props.stickyOffsetTop);
} }
emit('select', match[0].index); emit('select', match.index);
} }
}; };

View File

@ -256,11 +256,11 @@ export default createComponent({
// correct the index of active tab // correct the index of active tab
const setCurrentIndexByName = (name: number | string) => { const setCurrentIndexByName = (name: number | string) => {
const matched = children.filter( const matched = children.find(
(tab, index) => getTabName(tab, index) === name (tab, index) => getTabName(tab, index) === name
); );
const index = matched[0] ? children.indexOf(matched[0]) : 0; const index = matched ? children.indexOf(matched) : 0;
setCurrentIndex(index); setCurrentIndex(index);
}; };

View File

@ -240,7 +240,7 @@ export default createComponent({
const imageFiles = props.modelValue.filter(isImageFile); const imageFiles = props.modelValue.filter(isImageFile);
const images = imageFiles const images = imageFiles
.map((item) => item.content || item.url) .map((item) => item.content || item.url)
.filter((item) => !!item) as string[]; .filter(Boolean) as string[];
imagePreview = ImagePreview({ imagePreview = ImagePreview({
images, images,