leisure-core 0.6.61 → 0.6.62

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.
@@ -6,7 +6,6 @@
6
6
  :class="['customClass', $attrs.class]"
7
7
  :style="$attrs.style"
8
8
  :type="type"
9
- @clear="handleClear"
10
9
  ></el-date-picker>
11
10
  </template>
12
11
 
@@ -38,12 +37,6 @@ export default {
38
37
  },
39
38
  },
40
39
  methods: {
41
- handleClear() {
42
- this.internalValue = new Date();
43
- if (this.$listeners.clear) {
44
- this.$listeners.clear();
45
- }
46
- },
47
40
  isTimestampInSeconds(value) {
48
41
  if (typeof value !== "number") return false;
49
42
 
@@ -80,6 +80,11 @@ export default {
80
80
  type: Number,
81
81
  default: 2,
82
82
  },
83
+ // 新增:是否允许负数(仅对 number 和 amount 类型生效)
84
+ allowNegative: {
85
+ type: Boolean,
86
+ default: false,
87
+ },
83
88
  },
84
89
 
85
90
  data() {
@@ -111,6 +116,7 @@ export default {
111
116
  delete attrs.value;
112
117
  delete attrs.allowDecimal;
113
118
  delete attrs.decimalLimit;
119
+ delete attrs.allowNegative; // 新增
114
120
 
115
121
  // 合并值,确保我们的 value 逻辑优先
116
122
  return {
@@ -143,7 +149,12 @@ export default {
143
149
 
144
150
  displayValue() {
145
151
  // 金额类型在非聚焦状态下显示格式化后的值
146
- if (this.amount && !this.isFocused && this.internalValue) {
152
+ if (
153
+ this.amount &&
154
+ !this.isFocused &&
155
+ this.internalValue !== undefined &&
156
+ this.internalValue !== ""
157
+ ) {
147
158
  return this.formatAmount(this.internalValue);
148
159
  }
149
160
  return this.internalValue;
@@ -152,10 +163,6 @@ export default {
152
163
 
153
164
  watch: {
154
165
  value(newVal) {
155
- // let cleaned = newVal;
156
- // if (this.amount && !this.allowDecimal && cleaned) {
157
- // cleaned = String(cleaned).replace(/\./g, "");
158
- // }
159
166
  this.internalValue = newVal;
160
167
  },
161
168
 
@@ -228,14 +235,31 @@ export default {
228
235
  },
229
236
 
230
237
  formatNumber(value) {
231
- let filtered = value.replace(/[^\d.-]/g, "").replace(/(\..*)\./g, "$1");
238
+ // 根据 allowNegative 决定是否保留负号
239
+ let filtered = value;
240
+ if (this.allowNegative) {
241
+ // 允许负数:保留数字、小数点、负号,但负号只能出现在开头,多个负号只保留第一个
242
+ filtered = value.replace(/[^\d.-]/g, "");
243
+ // 处理多个负号的情况:只保留第一个负号,并确保负号在开头
244
+ const hasNegative = filtered.startsWith("-");
245
+ filtered = filtered.replace(/-/g, "");
246
+ if (hasNegative) {
247
+ filtered = "-" + filtered;
248
+ }
249
+ // 处理小数点后多余的小数点
250
+ filtered = filtered.replace(/(\..*)\./g, "$1");
251
+ } else {
252
+ // 不允许负数:直接过滤掉负号
253
+ filtered = value.replace(/[^\d.]/g, "");
254
+ filtered = filtered.replace(/(\..*)\./g, "$1");
255
+ }
232
256
 
233
- // 如果限制小数位数,且不允许小数,则移除小数点
257
+ // 如果不允许小数,移除小数点
234
258
  if (this.number && !this.allowDecimal) {
235
259
  filtered = filtered.replace(/\./g, "");
236
260
  }
237
261
 
238
- // 如果限制小数位数,且允许小数,则限制小数位数
262
+ // 如果允许小数,限制小数位数
239
263
  if (this.number && this.allowDecimal && this.decimalLimit > 0) {
240
264
  const parts = filtered.split(".");
241
265
  if (parts.length === 2 && parts[1].length > this.decimalLimit) {
@@ -244,8 +268,6 @@ export default {
244
268
  }
245
269
 
246
270
  return filtered;
247
- // 只允许数字、小数点、负号
248
- // return value.replace(/[^\d.-]/g, "").replace(/(\..*)\./g, "$1");
249
271
  },
250
272
 
251
273
  formatMobile(value) {
@@ -254,8 +276,21 @@ export default {
254
276
  },
255
277
 
256
278
  formatAmountInput(value) {
257
- // 金额输入过滤:只允许数字和小数点
258
- let filteredValue = value.replace(/[^\d.]/g, "");
279
+ // 金额输入过滤
280
+ let filteredValue = value;
281
+
282
+ if (this.allowNegative) {
283
+ // 允许负数:保留数字、小数点、负号,负号只能出现在开头
284
+ filteredValue = value.replace(/[^\d.-]/g, "");
285
+ const hasNegative = filteredValue.startsWith("-");
286
+ filteredValue = filteredValue.replace(/-/g, "");
287
+ if (hasNegative) {
288
+ filteredValue = "-" + filteredValue;
289
+ }
290
+ } else {
291
+ // 不允许负数:只保留数字和小数点
292
+ filteredValue = value.replace(/[^\d.]/g, "");
293
+ }
259
294
 
260
295
  // 如果禁止小数,则删除所有小数点
261
296
  if (!this.allowDecimal) {
@@ -274,17 +309,24 @@ export default {
274
309
  parts[0] + "." + parts[1].substring(0, this.decimalLimit);
275
310
  }
276
311
 
312
+ // 特殊情况:单独的负号或负号+小数点不允许,清除无效输入
313
+ if (filteredValue === "-" || filteredValue === "-.") {
314
+ filteredValue = "";
315
+ }
316
+
277
317
  return filteredValue;
278
318
  },
279
319
 
280
320
  formatAmount(value) {
281
- if (!value) return "";
321
+ if (value === undefined || value === null || value === "") return "";
282
322
 
283
- // 将值转换为字符串
323
+ // 将值转换为字符串,处理负数
284
324
  const strValue = String(value);
325
+ const isNegative = strValue.startsWith("-");
326
+ let absValue = isNegative ? strValue.slice(1) : strValue;
285
327
 
286
328
  // 分割整数和小数部分
287
- const parts = strValue.split(".");
329
+ const parts = absValue.split(".");
288
330
  let integerPart = parts[0];
289
331
  let decimalPart = parts.length > 1 ? parts[1] : "";
290
332
 
@@ -292,11 +334,19 @@ export default {
292
334
  integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
293
335
 
294
336
  // 组合整数和小数部分
337
+ let formatted = "";
295
338
  if (decimalPart && this.allowDecimal) {
296
- return `${integerPart}.${decimalPart}`;
339
+ formatted = `${integerPart}.${decimalPart}`;
297
340
  } else {
298
- return integerPart;
341
+ formatted = integerPart;
299
342
  }
343
+
344
+ // 添加负号
345
+ if (isNegative && formatted !== "") {
346
+ formatted = "-" + formatted;
347
+ }
348
+
349
+ return formatted;
300
350
  },
301
351
 
302
352
  validateInput(value, showError = false) {
@@ -309,7 +359,14 @@ export default {
309
359
 
310
360
  // 数值验证
311
361
  if (this.number && strValue) {
312
- if (!/^-?\d*\.?\d*$/.test(strValue)) {
362
+ let numberPattern;
363
+ if (this.allowNegative) {
364
+ // 允许负数:可选负号,整数或小数
365
+ numberPattern = new RegExp(`^-?\\d*\\.?\\d*$`);
366
+ } else {
367
+ numberPattern = /^\d*\.?\d*$/;
368
+ }
369
+ if (!numberPattern.test(strValue)) {
313
370
  isValid = false;
314
371
  message = "请输入有效的数值";
315
372
  }
@@ -333,16 +390,29 @@ export default {
333
390
 
334
391
  // 金额验证
335
392
  if (this.amount && strValue) {
336
- const amountRegex = this.allowDecimal
337
- ? new RegExp(`^\\d+(\\.\\d{0,${this.decimalLimit}})?$`)
338
- : /^\d+$/;
339
-
340
- if (!amountRegex.test(strValue.replace(/,/g, ""))) {
393
+ // 去除千分位分隔符后再验证
394
+ const cleanValue = strValue.replace(/,/g, "");
395
+ let amountPattern;
396
+ if (this.allowNegative) {
397
+ amountPattern = this.allowDecimal
398
+ ? new RegExp(`^-?\\d+(\\.\\d{0,${this.decimalLimit}})?$`)
399
+ : /^-?\d+$/;
400
+ } else {
401
+ amountPattern = this.allowDecimal
402
+ ? new RegExp(`^\\d+(\\.\\d{0,${this.decimalLimit}})?$`)
403
+ : /^\d+$/;
404
+ }
405
+ if (!amountPattern.test(cleanValue)) {
341
406
  isValid = false;
342
407
  message = this.allowDecimal
343
408
  ? `请输入有效的金额(最多${this.decimalLimit}位小数)`
344
409
  : "请输入有效的金额(整数)";
345
410
  }
411
+ // 额外检查负数是否允许
412
+ if (!this.allowNegative && cleanValue.startsWith("-")) {
413
+ isValid = false;
414
+ message = "不允许输入负数";
415
+ }
346
416
  }
347
417
 
348
418
  // 最大长度验证
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leisure-core",
3
- "version": "0.6.61",
3
+ "version": "0.6.62",
4
4
  "description": "leisure-core是京心数据基于vue2.x开发的一套后台管理系统桌面端组件库,封装了大量实用的UI控件模板,非常方便开发者快速搭建前端应用",
5
5
  "private": false,
6
6
  "author": "北方乐逍遥(zcx7878)",