web-component-gallery 2.0.22 → 2.0.23

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.
@@ -11,16 +11,16 @@
11
11
  <Select
12
12
  show-search
13
13
  :style="{ width: width || '100%' }"
14
- :mode="mode"
14
+ :mode="mode"
15
15
  :open="isSelectOpen"
16
16
  :placeholder="placeholder"
17
- :filterOption="false"
17
+ :filterOption="listPageHandler ? false : filterOption"
18
18
  :getPopupContainer="tirggerNode => tirggerNode.parentNode"
19
19
  @select="handleSelect"
20
20
  @search="handleSearch"
21
21
  @popupScroll="handleScroll"
22
22
  v-bind="attrs"
23
- v-model="childSelectedValue"
23
+ v-model="innerValue"
24
24
  >
25
25
  <!-- v-on="$listeners" -->
26
26
  <template v-for="(index, name) in $slots" v-slot:[name]>
@@ -28,16 +28,16 @@
28
28
  </template>
29
29
  <template v-for="(index, name) in $scopedSlots" v-slot:[name]="data">
30
30
  <slot :name="name" v-bind="data"></slot>
31
- </template>
31
+ </template>
32
32
  <div slot="dropdownRender" slot-scope="menu">
33
- <Checkbox v-if="mode" :checked="selectChecked" @change="handleSelectAll">全选</Checkbox>
33
+ <Checkbox v-if="mode" :checked="checkedAll" @change="handleSelectAll">全选</Checkbox>
34
34
  <v-nodes :vnodes="menu" />
35
35
  <div v-if="isLoadingMore" class="loading-more">
36
36
  <Icon type="loading" />
37
37
  加载中...
38
38
  </div>
39
39
  </div>
40
- <SelectOption v-for="(item, index) in options" :key="index" :value="item[valueKey]">
40
+ <SelectOption v-for="(item, index) in innerOptions" :key="index" :value="item[valueKey]">
41
41
  {{ customLabel ? customLabelHandler(item) : item[labelKey] }}
42
42
  </SelectOption>
43
43
  </Select>
@@ -61,53 +61,72 @@ export default {
61
61
  Checkbox
62
62
  },
63
63
  props: {
64
+ // 基础配置
64
65
  value: {
65
66
  type: [String, Number, Array, Boolean, Object],
66
67
  default: undefined
67
68
  },
68
- // 是否多选
69
69
  mode: String,
70
- // 是否支持输入保留字符
71
70
  isInput: {
72
71
  type: Boolean,
73
72
  default: false
74
73
  },
75
- // 提示文字
76
74
  placeholder: {
77
75
  type: String,
78
76
  default: '请选择'
79
77
  },
80
78
  width: String,
81
- // 根据valueKey来进行自定义label
82
- customLabel: String,
79
+
80
+ // 数据映射
83
81
  valueKey: {
84
82
  type: String,
85
83
  default: 'value'
86
84
  },
87
85
  labelKey: {
88
86
  type: String,
89
- default: 'label'
87
+ default: 'label'
90
88
  },
89
+ customLabel: String,
90
+
91
91
  // 数据源
92
92
  options: {
93
93
  type: Array,
94
- default: () => ([])
94
+ default: () => []
95
+ },
96
+ listPageHandler: Function,
97
+
98
+ // 分页配置
99
+ pSize: {
100
+ type: Number,
101
+ default: 50
95
102
  },
96
- // 分页页数
97
- pSize: Number,
98
- // 分页总数,如要实现完整分页功能要传递接口总数
99
- pTotal: Number
103
+ searchKey: {
104
+ type: String,
105
+ default: 'keyword'
106
+ }
100
107
  },
108
+
101
109
  data() {
102
110
  return {
111
+ // 分页状态
112
+ pagination: {
113
+ current: 1,
114
+ size: 10,
115
+ total: 0
116
+ },
117
+
118
+ // 当前数据
119
+ innerOptions: [],
120
+
121
+ // UI状态
103
122
  isSelectOpen: false,
104
- currentPage: 1,
105
123
  isLoadingMore: false,
106
- searchKeyword: ''
124
+ searchValue: ''
107
125
  }
108
126
  },
127
+
109
128
  computed: {
110
- childSelectedValue: {
129
+ innerValue: {
111
130
  get() {
112
131
  return this.value
113
132
  },
@@ -115,7 +134,7 @@ export default {
115
134
  this.$emit('input', val)
116
135
  }
117
136
  },
118
- attrs() {
137
+ attrs() {
119
138
  return {
120
139
  allowClear: true,
121
140
  showSearch: true,
@@ -123,95 +142,142 @@ export default {
123
142
  }
124
143
  },
125
144
  hasMore() {
126
- return this.options.length < this.pTotal
145
+ return this.innerOptions.length < this.pagination.total
127
146
  },
128
- selectChecked: {
129
- get() {
130
- return this.childSelectedValue?.length === this.options?.length
147
+ checkedAll() {
148
+ return this.innerValue?.length === this.innerOptions?.length
149
+ }
150
+ },
151
+
152
+ watch: {
153
+ options: {
154
+ handler(newVal) {
155
+ this.innerOptions = [...newVal]
131
156
  },
132
- set(val) {
133
- this.$emit('input', val)
134
- }
157
+ immediate: true
135
158
  }
136
159
  },
160
+
137
161
  created() {
138
- this.debouncedSearch = debounce(this.handleSearch, 800)
139
- this.debouncedLoadMore = debounce(this.loadMoreData, 300)
162
+ this.debouncedSearch = debounce(this.handleSearch, 800, { leading: true, trailing: false })
163
+ this.debouncedLoadMore = debounce(this.loadMoreData, 300, { leading: false, trailing: true })
140
164
  },
165
+
141
166
  mounted() {
142
- document.addEventListener('click', this.bodyCloseMenus)
167
+ this.pagination.size = this.pSize
168
+ this.fetchData()
169
+ document.addEventListener('click', this.handleDocumentClick)
143
170
  },
171
+
144
172
  beforeDestroy() {
145
- document.removeEventListener('click', this.bodyCloseMenus)
173
+ document.removeEventListener('click', this.handleDocumentClick)
174
+ // 清理debounce定时器
175
+ this.debouncedSearch.cancel()
176
+ this.debouncedLoadMore.cancel()
146
177
  },
178
+
147
179
  methods: {
180
+ async fetchData() {
181
+ if (!this.listPageHandler) return
182
+
183
+ try {
184
+ this.isLoadingMore = true
185
+ const { total, records } = await this.listPageHandler({
186
+ ...this.pagination,
187
+ [this.searchKey]: this.searchValue
188
+ })
189
+
190
+ this.pagination.current === 1 ?
191
+ this.innerOptions = [...records] :
192
+ this.innerOptions = [...this.innerOptions, ...records]
193
+
194
+ this.pagination.total = total
195
+ } catch (error) {
196
+ this.pagination.current--
197
+ console.error('分页加载失败:', error)
198
+ } finally {
199
+ this.isLoadingMore = false
200
+ }
201
+ },
202
+
148
203
  // 下拉滚动加载更多
149
204
  handleScroll(e) {
150
205
  const { scrollTop, scrollHeight, clientHeight } = e.target
151
206
  const reachBottom = scrollTop + clientHeight >= scrollHeight - 10
152
207
  if (reachBottom && !this.isLoadingMore && this.hasMore) this.debouncedLoadMore()
153
208
  },
154
- // 搜索事件
209
+
210
+ // 搜索事件处理
155
211
  handleSearch(value) {
156
- this.isInput && (this.childSelectedValue = value)
157
- this.searchKeyword = value
158
- this.currentPage = 1
159
-
160
- // 根据是否有分页决定emit参数格式
161
- this.$emit('search', this.pTotal ? {
162
- keyword: value,
163
- current: this.currentPage,
164
- size: this.pSize
165
- } : value)
166
- },
167
- // 点击选择器外内容下拉回收
168
- bodyCloseMenus(e) {
212
+ this.searchValue = value
213
+ this.pagination.current = 1
214
+
215
+ this.fetchData()
216
+ },
217
+
218
+ // 本地搜索
219
+ filterOption(input, option) {
220
+ this.isInput && (this.innerValue = input)
221
+ return option.componentOptions.children[0].text
222
+ .toLowerCase()
223
+ .indexOf(input.toLowerCase()) >= 0
224
+ },
225
+
226
+ // 点击选择器外内容关闭下拉
227
+ handleDocumentClick(e) {
169
228
  if (this.$refs.main && !this.$refs.main.contains(e.target)) this.isSelectOpen = false
170
229
  },
171
- // 自定义label显示
230
+
231
+ // 安全的自定义label处理
172
232
  customLabelHandler(item) {
233
+ if (!this.customLabel) return item[this.labelKey]
234
+
173
235
  try {
174
- // 使用函数式替代eval
175
- const func = new Function('item', `return ${this.customLabel}`)
176
- return func(item)
236
+ // 使用安全的模板字符串解析
237
+ const template = this.customLabel
238
+ .replace(/{{([^{}]+)}}/g, (match, key) => {
239
+ const path = key.trim().split('.')
240
+ let value = item
241
+ for (const p of path) {
242
+ if (value == null) return ''
243
+ value = value[p]
244
+ }
245
+ return value == null ? '' : value
246
+ })
247
+ return template
177
248
  } catch (e) {
178
249
  console.error('自定义label解析错误:', e)
179
250
  return item[this.labelKey]
180
251
  }
181
252
  },
182
- // 选中全部
183
- handleSelectAll(event) {
184
- const isChecked = event.target.checked
185
- const selectOptions = isChecked ? this.options.map(item => item[this.valueKey]) : []
186
- this.$emit('input', selectOptions)
253
+
254
+ // 选中全部选项
255
+ handleSelectAll({target}) {
256
+ const selectOptions = target.checked
257
+ ? this.innerOptions.map(item => item[this.valueKey])
258
+ : []
259
+
187
260
  this.isSelectOpen = false
261
+ this.$emit('input', selectOptions)
188
262
  },
189
- // 选中事件
263
+
264
+ // 选中单个选项
190
265
  handleSelect(value, option) {
191
266
  if (!value) return
267
+
192
268
  this.isSelectOpen = false
193
- const selectedOption = this.options.find(item => item[this.valueKey] === value)
269
+ const selectedOption = this.innerOptions.find(
270
+ item => item[this.valueKey] === value
271
+ )
194
272
  selectedOption && this.$emit('select', value, selectedOption)
195
273
  },
196
- // 加载更多
197
- loadMoreData() {
274
+
275
+ // 加载更多数据
276
+ async loadMoreData() {
198
277
  if (this.isLoadingMore || !this.hasMore) return
199
278
 
200
- this.isLoadingMore = true
201
- this.currentPage++
202
-
203
- try {
204
- this.$emit('load-more', {
205
- keyword: this.searchKeyword,
206
- current: this.currentPage,
207
- size: this.pSize
208
- })
209
- } catch (error) {
210
- this.currentPage--
211
- console.error('加载更多失败:', error)
212
- } finally {
213
- this.isLoadingMore = false
214
- }
279
+ this.pagination.current++
280
+ await this.fetchData()
215
281
  }
216
282
  }
217
283
  }
@@ -7,7 +7,7 @@ export default {
7
7
  model: {
8
8
  prop: 'value',
9
9
  event: 'Change:Model'
10
- },
10
+ },
11
11
  data() {
12
12
  return {
13
13
  formRules: {}
@@ -17,7 +17,7 @@ export default {
17
17
  value: {
18
18
  type: Object,
19
19
  default: () => ({})
20
- },
20
+ },
21
21
  /* Form布局方式 */
22
22
  layout: {
23
23
  type: String,
@@ -32,17 +32,7 @@ export default {
32
32
  }
33
33
  },
34
34
  /* 表单项配置 */
35
- formSetting: {
36
- type: Array,
37
- default: () => [],
38
- validator: value => {
39
- return value.every(item => {
40
- return typeof item === 'object' &&
41
- 'model' in item &&
42
- 'type' in item
43
- })
44
- }
45
- }
35
+ formSetting: Array
46
36
  },
47
37
  computed: {
48
38
  form: {
@@ -57,6 +47,16 @@ export default {
57
47
  this.$emit('Change:Model', value)
58
48
  }
59
49
  },
50
+ formAttrs() {
51
+ const attr =
52
+ this.layout === 'vertical' ?
53
+ this.$attrs :
54
+ {
55
+ ...getDefaultFormAttrs(),
56
+ ...this.$attrs
57
+ }
58
+ return attr
59
+ },
60
60
  filterSetting() {
61
61
  return this.formSetting.filter(settingItem => !settingItem.hidden)
62
62
  }
@@ -82,36 +82,43 @@ export default {
82
82
  }
83
83
  },
84
84
 
85
- renderSingleFormItem(props) {
85
+ renderSingleFormItem(h, props) {
86
86
  return setFormItem.call(this, h, this.form, props)
87
- },
87
+ },
88
88
 
89
- renderDynamicFormItems(props) {
90
- return props.multipleConfig.map((configItem, key) => {
91
- const childAttrs = {
92
- key,
93
- prop: `${props.model}.${key}.${configItem.model}`,
94
- style: getFormWidth.call(this, configItem, this.layoutSize ?? props.layoutSize),
95
- rules: setFormItemRule.call(this, configItem, props),
96
- parentModel: props.model
97
- }
98
- return setFormItem.call(this, h, this.form[props.model][key], configItem, childAttrs)
89
+ renderDynamicFormItems(h, dynamicModel, props) {
90
+ // multipleConfig为配置的动态项数据
91
+ return dynamicModel.map((modelItem, key) => {
92
+ // 为动态项时重新定义绑定key、prop等来进行检验
93
+ props.multipleConfig.map((configItem) => {
94
+ const childAttrs = {
95
+ key,
96
+ prop: `${props.model}.${key}.${configItem.model}`,
97
+ style: getFormWidth.call(this, configItem, this.layoutSize ?? props.layoutSize),
98
+ rules: setFormItemRule.call(this, configItem, props),
99
+ parentModel: props.model
100
+ }
101
+ return setFormItem.call(this, h, modelItem, configItem, childAttrs)
102
+ })
99
103
  })
100
104
  },
101
105
 
102
- setModelRender(props) {
106
+ setModelRender(h, props) {
103
107
  const dynamicModel = this.form[props.model]
104
108
  return (
105
109
  <div
106
110
  class={props.multiple && ['MultipleForm']}
107
- {...{ attrs: { style: getFormWidth.call(this, props, this.layoutSize, props.multiple ? 24 : 0) } }}
111
+ {...{ attrs: { style: getFormWidth.call(this, props, this.layoutSize, props.multiple ? 0 : 24) } }}
108
112
  >
113
+ {/* 添加分层提示信息及其他隔层操作 */}
109
114
  {this.$scopedSlots[`${props.model}Tips`] && this.$scopedSlots[`${props.model}Tips`](props)}
110
115
  {
116
+ // multiple为动态多项(使用场景:处理动态增减表单
111
117
  dynamicModel instanceof Array && props.multiple
112
- ? this.renderDynamicFormItems(props)
113
- : this.renderSingleFormItem(props)
118
+ ? this.renderDynamicFormItems(h, dynamicModel, props)
119
+ : this.renderSingleFormItem(h, props)
114
120
  }
121
+ {/* 使用场景:如另起一行添加其他额外信息及操作 */}
115
122
  {this.$scopedSlots[`${props.model}Operate`] && this.$scopedSlots[`${props.model}Operate`](props)}
116
123
  </div>
117
124
  )
@@ -119,7 +126,7 @@ export default {
119
126
 
120
127
  getFormWidth: _.memoize(function(props, layoutSize, multiple) {
121
128
  return getFormWidth.call(this, props, layoutSize, multiple ? 24 : 0)
122
- }),
129
+ }),
123
130
 
124
131
  formSubmit() {
125
132
  return new Promise((resolve, reject) => {
@@ -138,7 +145,7 @@ export default {
138
145
  })
139
146
  }
140
147
  },
141
- render() {
148
+ render(h) {
142
149
  const {layout, formAttrs, setModelRender} = this
143
150
 
144
151
  return (
@@ -154,7 +161,7 @@ export default {
154
161
  >
155
162
  {
156
163
  this.$slots.default ??
157
- this.filterSetting.map(props => setModelRender(props))
164
+ this.filterSetting.map(props => setModelRender(h, props))
158
165
  }
159
166
  </FormModel>
160
167
  )
@@ -9,13 +9,11 @@ const FormModelItem = FormModel.Item
9
9
  * @param {number} layoutSize - 一行显示的表单项数量
10
10
  * @param {number} [gap=24] - 表单项间距
11
11
  * @returns {string} - 计算后的CSS样式
12
- */
12
+ */
13
13
  export function getFormWidth(child, layoutSize, gap = 24) {
14
- if (this.layout === 'vertical') {
15
- const width = (100 / layoutSize) * (child.size ?? 1)
16
- return `flex: 0 1 calc(${width}% - ${gap}px); margin-right: ${gap}px;`
17
- }
18
- return `flex: 0 1 ${(100 / layoutSize) * (child.size ?? 1)}%;`
14
+ const width = (100 / layoutSize) * (child.size ?? 1)
15
+ if (this.layout === 'vertical') return `flex: 0 1 calc(${width}% - ${gap}px); margin-right: ${gap}px;`
16
+ return `flex: 0 1 ${width}%;`
19
17
  }
20
18
 
21
19
  /**
@@ -80,9 +78,7 @@ export function setFormItem(h, vModel, child, childAttrs) {
80
78
 
81
79
  // 渲染表单项内容
82
80
  const renderContent = () => {
83
- if ($scopedSlots[child.model]) {
84
- return $scopedSlots[child.model](child)
85
- }
81
+ if ($scopedSlots[child.model]) return $scopedSlots[child.model](child)
86
82
  return (
87
83
  <RenderComp
88
84
  v-model={vModel[child.model]}
@@ -103,7 +99,9 @@ export function setFormItem(h, vModel, child, childAttrs) {
103
99
  <template slot="label">
104
100
  {renderLabel(child.customLabel)}
105
101
  </template>
102
+ {/* 当前表单项可slot进行单独开发配置 (目前仅支持非动态增减情况) */}
106
103
  {renderContent()}
104
+ {/* 如若再某个model后需添加按钮等场景(例如地图的选址按钮 */}
107
105
  {$scopedSlots[`${slotsName}Handle`] && $scopedSlots[`${slotsName}Handle`](formItemAttrs)}
108
106
  </FormModelItem>
109
107
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-component-gallery",
3
- "version": "2.0.22",
3
+ "version": "2.0.23",
4
4
  "description": "基础vue、antdvue、less实现的私有组件库",
5
5
  "main": "dist/index.umd.js",
6
6
  "files": [