Skip to content

Commit

Permalink
fix: simplify code logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gene9831 committed Dec 5, 2024
1 parent 05fe922 commit 679397f
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions packages/plugins/materials/src/meta/block/src/BlockGroupFilters.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
v-if="!filter.usingSelect"
v-model="state.checkGroup[filter.id]"
type="checkbox"
@change="getFilters($event, filter.id, filter.children)"
@change="getFilters($event, filter.id)"
>
<tiny-checkbox v-for="item in filter.children" :key="item.value" :label="item.name"></tiny-checkbox>
<tiny-checkbox
v-for="item in selectOptions[filter.id]"
:key="item.value"
:text="item.name"
:label="item.value"
></tiny-checkbox>
</tiny-checkbox-group>
<tiny-select
v-else
Expand All @@ -20,9 +25,9 @@
>
<tiny-option
v-for="item in selectOptions[filter.id]"
:key="item.name"
:key="item.value"
:label="item.name"
:value="item"
:value="item.value"
></tiny-option>
</tiny-select>
</div>
Expand Down Expand Up @@ -59,7 +64,7 @@ export default {
)
})
// 这里重新计算selectOptions的原因:tiny-option的value属性如果是一个对象,那么此对象内部需要有value属性
// 不同的filter,值所在的字段可能是id或者name。这里把实际的值都映射到value字段
const selectOptions = computed(() => {
return props.filters.reduce(
(result, filter) => ({
Expand All @@ -73,19 +78,9 @@ export default {
)
})
const getFilters = (checked, id, child) => {
filters[id] = []
const getFilters = (checked, id) => {
filters[id] = checked
// tiny-checkbox-group的选中值是一个字符串数组
if (typeof checked.at(0) === 'string') {
child.forEach((item) => {
if (checked.includes(item.name)) {
filters[id].push(item.id)
}
})
} else {
filters[id] = checked.map((item) => item.value)
}
emit('search', null, filters)
}
Expand Down

0 comments on commit 679397f

Please sign in to comment.