wui-components-v2 1.2.49 → 1.2.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,12 @@ const theme = computed(() => themeStore.theme)
21
21
  const show = ref(false)
22
22
  const search = ref('')
23
23
  const formControlRef = ref()
24
- const entity = ref({})
24
+ // 外置 preferred 筛选字段的 form-control 实例
25
+ const preferredFormControlRef = ref()
26
+ // 外置 preferred 表单的刷新标识:弹窗提交后递增,强制外置 form-control 重挂载读取最新 entity
27
+ const preferredRefreshKey = ref(0)
28
+ // 共享实体:外置 preferred 字段与弹窗筛选字段通过它进行双向同步
29
+ const entity = ref<{ [key: string]: any }>({})
25
30
  // 判断字段实际渲染的控件类型是否为纯文本输入(优先 extControlType,回退 controlType)
26
31
  const isTextCriterion = (item: Fields) => ControlTypeSupportor.getControlType(item) === 'text'
27
32
  // 用于 wd-search 的搜索字段:primaryCriterion 存在时直接使用,否则取 criteria 中第一个文本类型的筛选项
@@ -33,79 +38,144 @@ const searchCriterion = computed(() => {
33
38
  })
34
39
  // 仅当 searchCriterion 存在时显示搜索框
35
40
  const showSearch = computed(() => searchCriterion.value !== null)
41
+ // tab 筛选字段(split2TabCriterias)的 sourceId,用于从普通筛选中排除
42
+ const split2TabSourceIds = computed(() => props.split2TabCriterias?.map(item => item.sourceId) || [])
43
+ // preferred=true 的字段外置到搜索框上方展示(排除 tab 筛选字段,以及已作为搜索框的 searchCriterion,避免重复展示)
44
+ const preferredCriteria = computed(() => {
45
+ const searchSourceId = showSearch.value ? searchCriterion.value?.sourceId : undefined
46
+ return props.criteria.filter(
47
+ item =>
48
+ item.preferred === true && !split2TabSourceIds.value.includes(item.sourceId) && item.sourceId !== searchSourceId
49
+ )
50
+ })
36
51
  const filterCriteria = computed(() => {
37
- const criteriaSourceIds = props.split2TabCriterias?.map(item => item.sourceId) || []
38
52
  // 仅当搜索框真正显示时,才把 searchCriterion 从筛选弹窗中排除;
39
53
  // 否则(extControlType 存在)该字段应回落到筛选弹窗中参与筛选
40
54
  const searchSourceId = showSearch.value ? searchCriterion.value?.sourceId : undefined
41
- if (!searchSourceId) return props.criteria.filter(item => !criteriaSourceIds.includes(item.sourceId))
42
- return props.criteria.filter(item => ![searchSourceId, ...criteriaSourceIds].includes(item.sourceId))
55
+ // 弹窗筛选需保留 preferred 字段(与外置字段双向绑定),因此不排除 preferredCriteria
56
+ if (!searchSourceId) return props.criteria.filter(item => !split2TabSourceIds.value.includes(item.sourceId))
57
+ return props.criteria.filter(item => ![searchSourceId, ...split2TabSourceIds.value].includes(item.sourceId))
43
58
  })
59
+ // 把外部/弹窗表单返回的数据归一化后再写入 entity:
60
+ // 数组值统一转成逗号分隔字符串,避免弹窗 initFormData 对数组调用 split 报错导致空白
61
+ function normalizeForEntity(data: { [key: string]: any }) {
62
+ const result: { [key: string]: any } = {}
63
+ for (const key in data) {
64
+ if (Object.prototype.hasOwnProperty.call(data, key)) {
65
+ const val = data[key]
66
+ result[key] = Array.isArray(val) ? val.join(',') : val
67
+ }
68
+ }
69
+ return result
70
+ }
71
+ // 打开筛选弹窗前,先把外置 preferred 字段的当前值同步进 entity,
72
+ // 再打开弹窗(await 保证先同步完,弹窗 form-control 重挂载时能读取到最新值,避免竞态空白)
73
+ async function handleShowPopup() {
74
+ if (preferredFormControlRef.value?.submit) {
75
+ try {
76
+ const pdata = await preferredFormControlRef.value.submit()
77
+ Object.assign(entity.value, normalizeForEntity(pdata))
78
+ } catch {
79
+ // 校验失败(如必填未填)时仍保留原 entity,不阻塞弹窗打开
80
+ }
81
+ }
82
+ console.log('[Search] handleShowPopup entity =', entity.value)
83
+ show.value = true
84
+ }
44
85
  function handleClose() {
45
86
  show.value = false
46
87
  }
88
+ // 把合并后的数据对象(fdata)拼接为查询条件字符串
89
+ function buildQueryData(fdata: { [key: string]: any }) {
90
+ let data = ''
91
+ if (showSearch.value && searchCriterion.value?.sourceId) {
92
+ data = `c_${searchCriterion.value.sourceId}=${search.value}`
93
+ }
94
+ filterCriteria.value.forEach(item => {
95
+ const element = fdata[item.sourceId]
96
+ // 空值(空字符串、undefined、null、空数组)仍拼接 `c_${sourceId}=` 空参数,
97
+ // 让父组件识别「该字段已被用户清空」,从而跳过 defaultCriteriaValue 的默认值兜底
98
+ if (element === undefined || element === null || element === '') {
99
+ data = `${data}&c_${item.sourceId}=`
100
+ return
101
+ }
102
+ if (Array.isArray(element) && element.length === 0) {
103
+ data = `${data}&c_${item.sourceId}=`
104
+ return
105
+ }
106
+ // 跳过用户未修改的默认值(时间字段常被自动填充当前时间,不应作为查询条件)
107
+ if (item.transDefaultValue && String(element) === String(item.transDefaultValue)) return
108
+ if (item.extControlType === 'relselectvalue') {
109
+ const elements = Array.isArray(element) ? element : [element]
110
+ data = `${data}&c_${item.sourceId}=${elements
111
+ .map((item: string) => {
112
+ return item.split('@R@')[1]
113
+ })
114
+ .join('@,@')}`
115
+ } else if (item.extControlType === 'multiselect') {
116
+ const elements = Array.isArray(element) ? element : [element]
117
+ elements.forEach((subitem: string) => {
118
+ data = `${data}&c_${item.sourceId}=${subitem}`
119
+ })
120
+ } else {
121
+ data = `${data}&c_${item.sourceId}=${element}`
122
+ }
123
+ })
124
+ return data
125
+ }
126
+ // 对外触发提交(点击搜索/确定时使用)
127
+ function emitSubmit(fdata: { [key: string]: any }) {
128
+ console.log('查询条件', buildQueryData(fdata))
129
+ emits('submit', buildQueryData(fdata))
130
+ handleClose()
131
+ }
47
132
  function handleSubmit() {
48
133
  if (!formControlRef.value) {
49
134
  if (!showSearch.value || !searchCriterion.value?.sourceId) return
50
- const data = `c_${searchCriterion.value.sourceId}=${search.value}`
51
- emits('submit', data)
52
- handleClose()
135
+ // 弹窗未挂载(从未打开或已关闭)时,entity 中仍保留着已选中的筛选条件,
136
+ // 不能只提交搜索框关键字,需把搜索框值与 entity 中的筛选值合并后一并提交
137
+ const merged = normalizeForEntity({ ...entity.value })
138
+ merged[searchCriterion.value.sourceId] = search.value
139
+ emitSubmit(merged)
53
140
  return
54
141
  }
55
142
  formControlRef.value.submit().then((fdata: any) => {
56
- entity.value = {
57
- ...fdata,
58
- ...(showSearch.value && searchCriterion.value?.sourceId
59
- ? { [searchCriterion.value.sourceId]: search.value }
60
- : {}),
61
- }
62
- let data = ''
63
-
143
+ // 双向绑定:弹窗内提交的值(含 preferred 字段)写回 entity,供外置字段同步;
144
+ // 数组值归一化为字符串,避免后续 initFormData 对数组 split 报错
145
+ Object.assign(entity.value, normalizeForEntity(fdata))
64
146
  if (showSearch.value && searchCriterion.value?.sourceId) {
65
- data = `c_${searchCriterion.value.sourceId}=${search.value}`
147
+ entity.value[searchCriterion.value.sourceId] = search.value
66
148
  }
67
-
68
- filterCriteria.value.forEach(item => {
69
- const element = fdata[item.sourceId]
70
- // 空值(空字符串、undefined、null、空数组)仍拼接 `c_${sourceId}=` 空参数,
71
- // 让父组件识别「该字段已被用户清空」,从而跳过 defaultCriteriaValue 的默认值兜底
72
- if (element === undefined || element === null || element === '') {
73
- data = `${data}&c_${item.sourceId}=`
74
- return
75
- }
76
- if (Array.isArray(element) && element.length === 0) {
77
- data = `${data}&c_${item.sourceId}=`
78
- return
79
- }
80
- // 跳过用户未修改的默认值(时间字段常被自动填充当前时间,不应作为查询条件)
81
- if (item.transDefaultValue && String(element) === String(item.transDefaultValue)) return
82
- if (item.extControlType === 'relselectvalue') {
83
- const elements = Array.isArray(element) ? element : [element]
84
- data = `${data}&c_${item.sourceId}=${elements
85
- .map((item: string) => {
86
- return item.split('@R@')[1]
87
- })
88
- .join('@,@')}`
89
- } else if (item.extControlType === 'multiselect') {
90
- const elements = Array.isArray(element) ? element : [element]
91
- elements.forEach((subitem: string) => {
92
- data = `${data}&c_${item.sourceId}=${subitem}`
93
- })
94
- } else {
95
- data = `${data}&c_${item.sourceId}=${element}`
96
- }
97
- })
98
-
99
- console.log('查询条件', data)
100
-
101
- emits('submit', data)
102
- handleClose()
149
+ // 递增刷新标识,强制外置 preferred 表单重挂载并读取最新 entity(弹窗 → 外置同步)
150
+ preferredRefreshKey.value++
151
+ emitSubmit(fdata)
103
152
  })
104
153
  }
105
154
  </script>
106
155
 
107
156
  <template>
108
157
  <wd-config-provider :theme="theme">
158
+ <!-- preferred=true 的筛选项外置在搜索框上方,常驻展示;与弹窗中的同名筛选双向绑定 -->
159
+ <view v-if="preferredCriteria.length" class="w-full box-border pt-2">
160
+ <formControl
161
+ ref="preferredFormControlRef"
162
+ :key="`preferred-${preferredRefreshKey}`"
163
+ :field-group="{
164
+ fields: preferredCriteria,
165
+ readOnly: false,
166
+ displayConfig: [],
167
+ id: '',
168
+ buttons: [],
169
+ title: '',
170
+ type: '',
171
+ pointSourceId: '',
172
+ mstrucId: '',
173
+ relationNames: [],
174
+ }"
175
+ :entity="entity"
176
+ :enum-column="enumColumn"
177
+ />
178
+ </view>
109
179
  <view v-if="showSearch" class="w-full">
110
180
  <wd-search
111
181
  v-model="search"
@@ -120,14 +190,14 @@ function handleSubmit() {
120
190
  name="filter"
121
191
  size="17px"
122
192
  class="text-gray-500 dark:text-[var(--wot-dark-color3)]"
123
- @click="show = true"
193
+ @click="handleShowPopup"
124
194
  />
125
195
  </view>
126
196
  </template>
127
197
  </wd-search>
128
198
  </view>
129
199
  <view v-else-if="filterCriteria.length" class="px-[8px] mt-2">
130
- <wd-button block custom-class="search-filter-btn" @click="show = true">
200
+ <wd-button block custom-class="search-filter-btn" @click="handleShowPopup">
131
201
  <wd-icon name="filter" size="17px" class="mr-1" />
132
202
  筛选
133
203
  </wd-button>
@@ -135,7 +205,9 @@ function handleSubmit() {
135
205
  <wd-popup v-model="show" position="bottom" custom-class="h-80vh" closable @close="handleClose">
136
206
  <view class="relative box-border h-full p-1 bg-white dark:bg-[var(--wot-dark-background)]">
137
207
  <view class="h-34px flex items-center justify-center text-black dark:text-[var(--wot-dark-color)]">筛选</view>
208
+ <!-- v-if="show" 保证每次打开弹窗都重新挂载,读取最新 entity(含外置 preferred 值),实现双向绑定 -->
138
209
  <formControl
210
+ v-if="show"
139
211
  ref="formControlRef"
140
212
  :field-group="{
141
213
  fields: filterCriteria,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wui-components-v2",
3
- "version": "1.2.49",
3
+ "version": "1.2.51",
4
4
  "description": "wui 组件库",
5
5
  "author": "wgxshh",
6
6
  "license": "MIT",
package/type.ts CHANGED
@@ -151,6 +151,8 @@ export interface Fields extends Columns {
151
151
  min?: string
152
152
  comparator?: Comparator
153
153
  rules?: FieldRule | FieldRule[]
154
+ // 为 true 时该筛选项会外置到搜索框上方展示(同时仍保留在筛选弹窗中,并与弹窗双向绑定)
155
+ preferred?: boolean
154
156
  }
155
157
 
156
158
  // 编辑按钮配置