@yqg/simple 1.0.1 → 1.0.2

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.
package/README.md CHANGED
@@ -63,8 +63,35 @@ Vue.component('yqg-simple-form', YqgSimpleForm)
63
63
 
64
64
  ## 混入 (Mixins)
65
65
 
66
+ ### 引入方式
67
+
68
+ ```javascript
69
+ import { table, modal, watermark } from '@yqg/simple/mixin'
70
+
71
+ export default {
72
+ mixins: [table, modal, watermark],
73
+ // ...
74
+ }
75
+ ```
76
+
77
+ ### 可用的混入
78
+
79
+ - `table` - 表格混入,提供表格相关的通用方法
80
+ - `tableSelection` - 表格选择混入
81
+ - `modal` - 模态框混入,提供模态框相关的通用方法
82
+ - `watermark` - 水印混入,提供水印功能
83
+ - `commonTable` - 通用表格混入
84
+ - `clientTable` - 客户端表格混入
85
+ - `enumType` - 枚举类型混入
86
+ - `cardList` - 卡片列表混入
87
+
88
+ ### 按需引入
89
+
66
90
  ```javascript
67
- import { table, modal, watermark } from '@yqg/simple'
91
+ // 引入单个混入
92
+ import table from '@yqg/simple/src/mixin/table'
93
+ import modal from '@yqg/simple/src/mixin/modal'
94
+ import watermark from '@yqg/simple/src/mixin/watermark'
68
95
 
69
96
  export default {
70
97
  mixins: [table, modal, watermark],
@@ -87,9 +114,397 @@ export default {
87
114
 
88
115
  ## 工具函数
89
116
 
117
+ ### 完整引入(从主入口)
118
+
90
119
  ```javascript
91
- import { formatMap, object } from '@yqg/simple'
120
+ import { util, formatMap, object, constant } from '@yqg/simple'
121
+
122
+ // 使用字符串工具
123
+ const snakeCase = util.camelCaseToUnderscore('userName') // 'user_name'
124
+
125
+ // 使用对象工具
126
+ const value = object.pickValue(data, 'user.name')
127
+ const merged = object.merge(obj1, obj2)
92
128
 
93
- // 使用工具函数
129
+ // 使用格式化工具
94
130
  const formatted = formatMap.formatValue(value, options)
95
- const merged = object.deepMerge(obj1, obj2)
131
+ ```
132
+
133
+ ### 按需引入(推荐)
134
+
135
+ 支持通过子路径按需引入工具函数,减少打包体积:
136
+
137
+ ```javascript
138
+ // 字符串工具
139
+ import { camelCaseToUnderscore } from '@yqg/simple/util/tool'
140
+
141
+ const snakeCase = camelCaseToUnderscore('userName') // 'user_name'
142
+
143
+ // 对象操作工具
144
+ import { pickValue, setValue, merge } from '@yqg/simple/util/object'
145
+
146
+ const data = { user: { name: 'John', age: 30 } }
147
+ const name = pickValue(data, 'user.name') // 'John'
148
+ const updated = setValue(data, 'user.email', 'john@example.com')
149
+ const merged = merge({ a: 1 }, { b: 2 }) // { a: 1, b: 2 }
150
+
151
+ // 格式化工具
152
+ import getFormatter from '@yqg/simple/util/format-map'
153
+
154
+ const listFormatter = getFormatter('list')
155
+ const formatted = listFormatter.format(['a', 'b', 'c']) // 'a\nb\nc'
156
+
157
+ // 枚举工具
158
+ import { boolOf, valueOf } from '@yqg/simple/util/enum'
159
+
160
+ const bool = boolOf('TRUE') // true
161
+ const str = valueOf(true) // 'TRUE'
162
+
163
+ // JSON 工具
164
+ import { pureJSONString, reformatJson } from '@yqg/simple/util/json'
165
+
166
+ const jsonStr = pureJSONString({ name: 'John' })
167
+
168
+ // 本地存储工具
169
+ import { setStorage, getStorage, removeItem } from '@yqg/simple/util/local-storage'
170
+
171
+ setStorage('user', JSON.stringify({ name: 'John' }))
172
+ const user = getStorage('user')
173
+
174
+ // 键码映射
175
+ import keyCodeMap from '@yqg/simple/util/keyCodeMap'
176
+
177
+ if (event.keyCode === keyCodeMap.enter) {
178
+ // 处理回车键
179
+ }
180
+
181
+ // Storage 类
182
+ import Storage from '@yqg/simple/util/storage'
183
+
184
+ const myStorage = new Storage('my-key', true) // 使用 sessionStorage
185
+ myStorage.set({ data: 'value' })
186
+ const data = myStorage.get()
187
+ ```
188
+
189
+ ### TypeScript 支持
190
+
191
+ 所有工具函数都提供完整的 TypeScript 类型声明,开发时会有智能提示:
192
+
193
+ ```typescript
194
+ import { camelCaseToUnderscore, pickValue, merge } from '@yqg/simple/util'
195
+
196
+ // ✅ 编辑器自动提示:camelCaseToUnderscore(camelStr: string): string
197
+ const snakeCase = camelCaseToUnderscore('userName')
198
+
199
+ // ✅ 编辑器自动提示:pickValue<T = any>(obj: any, keyString: string): T | undefined
200
+ const value = pickValue<number>({ a: { b: 1 } }, 'a.b')
201
+
202
+ // ✅ 编辑器自动提示:merge<T, S>(target?: T, source?: S): T & S
203
+ const merged = merge({ a: 1 }, { b: 2 })
204
+ ```
205
+
206
+ ### 工具函数 API
207
+
208
+ #### 字符串工具 (`util/tool`)
209
+
210
+ - `camelCaseToUnderscore(camelStr: string): string` - 驼峰转下划线
211
+
212
+ #### 对象工具 (`util/object`)
213
+
214
+ - `evalProp<T, V>(value: T | ((values: V) => T), values?: V): T` - 评估属性(函数或值)
215
+ - `pickValue<T>(obj: any, keyString: string): T | undefined` - 提取嵌套值(支持 'a.b.c' 或 'a[0].b')
216
+ - `setValue<T>(obj: T, keyString: string, value: any): T` - 设置嵌套值
217
+ - `spreadObjectKeys<T>(origin: T): T` - 展开对象键
218
+ - `numbersToStr<T>(val: T | T[]): ...` - 数字转字符串
219
+ - `spreadProps(props): ...` - 展开 Vue 组件属性
220
+ - `appendClass(className, appendix): ...` - 追加类名
221
+ - `merge<T, S>(target?: T, source?: S): T & S` - 深度合并对象
222
+ - `isMobile: boolean` - 是否为移动设备
223
+
224
+ #### 格式化工具 (`util/format-map`)
225
+
226
+ - `getFormatter(format: string | Formatter): Formatter` - 获取格式化器
227
+ - 内置格式化器:`boolean`、`bool`、`list`、`comma`、`listJson`、`json`、`listComma`、`commaToList`、`date`、`dateTime`、`percent`
228
+
229
+ ```javascript
230
+ const listFormatter = getFormatter('list')
231
+ const formatted = listFormatter.format(['a', 'b', 'c']) // 'a\nb\nc'
232
+ const unformatted = listFormatter.unformat('a\nb\nc') // ['a', 'b', 'c']
233
+ ```
234
+
235
+ #### 枚举工具 (`util/enum`)
236
+
237
+ - `boolOf(str: string): boolean` - 字符串转布尔值('TRUE' -> true)
238
+ - `valueOf(bool: boolean): 'TRUE' | 'FALSE' | undefined` - 布尔值转字符串(true -> 'TRUE')
239
+
240
+ #### JSON 工具 (`util/json`)
241
+
242
+ - `pureJSONString(value: any): string` - 转换为格式化的 JSON 字符串
243
+ - `reformatJson(value: string, context?: any): string` - 重新格式化 JSON 字符串
244
+
245
+ #### 本地存储工具 (`util/local-storage`)
246
+
247
+ - `setStorage(itemName: string, item: string): void` - 设置 localStorage
248
+ - `getStorage(itemName: string): string | null` - 获取 localStorage
249
+ - `removeItem(itemName: string): void` - 删除 localStorage 项
250
+
251
+ #### 键码映射 (`util/keyCodeMap`)
252
+
253
+ ```javascript
254
+ const keyCodeMap = {
255
+ esc: 27,
256
+ tab: 9,
257
+ enter: 13,
258
+ up: 38,
259
+ down: 40,
260
+ backSpace: 8
261
+ }
262
+ ```
263
+
264
+ #### Storage 类 (`util/storage`)
265
+
266
+ 封装了 localStorage 或 sessionStorage 的存储类:
267
+
268
+ ```typescript
269
+ class Storage {
270
+ constructor(key: string, useSessionStorage?: boolean)
271
+ set(cache: any): void // 设置存储
272
+ get<T = any>(): T | null // 获取存储
273
+ remove(): void // 删除存储
274
+ on(fn: (event: StorageEvent) => void): void // 监听变化
275
+ off(needsRemove?: boolean): void // 取消监听
276
+ }
277
+ ```
278
+
279
+ ## 常量
280
+
281
+ 组件库提供了丰富的常量定义,包括表单字段定义、表格配置等。
282
+
283
+ ### 引入方式
284
+
285
+ #### 从主入口引入
286
+
287
+ ```javascript
288
+ import { constant } from '@yqg/simple'
289
+
290
+ // 使用
291
+ const dateDef = constant.dateTimeDef
292
+ const requiredField = constant.required({ field: 'name', label: '姓名' })
293
+ ```
294
+
295
+ #### 按需引入(推荐)
296
+
297
+ ```javascript
298
+ // 引入 fields.ts(包含 common-fields 的所有导出)
299
+ import {
300
+ extendsDefValue,
301
+ dateTimeDayStartDef,
302
+ dateTimeDayEndDef,
303
+ // 以下来自 common-fields
304
+ dateTimeDef,
305
+ required,
306
+ merge,
307
+ id,
308
+ timeCreate,
309
+ timeUpdate,
310
+ remark
311
+ } from '@yqg/simple/constant/fields'
312
+
313
+ // 引入 common-fields
314
+ import {
315
+ dateTimeDef,
316
+ required,
317
+ merge,
318
+ primaryStaticProps,
319
+ successStaticProps,
320
+ warningStaticProps,
321
+ fixedLeft,
322
+ fixedRight,
323
+ staticComp,
324
+ blockItem,
325
+ hiddenItem,
326
+ id,
327
+ time,
328
+ timeCreate,
329
+ timeUpdate,
330
+ remark
331
+ } from '@yqg/simple/constant/common-fields'
332
+
333
+ // 引入表格常量
334
+ import { defaultPagination } from '@yqg/simple/constant/table'
335
+ ```
336
+
337
+ ### 常用常量 API
338
+
339
+ #### fields 相关 (`constant/fields`)
340
+
341
+ **字段定义辅助函数:**
342
+
343
+ - `extendsDefValue(options)` - 扩展 DefValue 组件
344
+ ```javascript
345
+ const customDef = extendsDefValue({
346
+ props: { showTime: true },
347
+ filter: 'dateTime'
348
+ })
349
+ ```
350
+
351
+ - `dateTimeDayStartDef` - 日期时间定义(默认 00:00:00)
352
+ ```javascript
353
+ // 用于搜索开始时间
354
+ const startTimeDef = {
355
+ ...dateTimeDayStartDef,
356
+ field: 'startTime',
357
+ label: '开始时间'
358
+ }
359
+ ```
360
+
361
+ - `dateTimeDayEndDef` - 日期时间定义(默认 23:59:59)
362
+ ```javascript
363
+ // 用于搜索结束时间
364
+ const endTimeDef = {
365
+ ...dateTimeDayEndDef,
366
+ field: 'endTime',
367
+ label: '结束时间'
368
+ }
369
+ ```
370
+
371
+ #### common-fields 相关 (`constant/common-fields`)
372
+
373
+ **辅助函数:**
374
+
375
+ - `merge<T>(...args)` - 合并字段定义
376
+ ```javascript
377
+ const customField = merge(dateTimeDef, { required: true, label: '创建时间' })
378
+ ```
379
+
380
+ - `required(def)` - 标记字段为必填
381
+ ```javascript
382
+ const nameField = required({ field: 'name', label: '姓名' })
383
+ ```
384
+
385
+ - `fixedLeft(def, width?)` - 固定列到左侧
386
+ ```javascript
387
+ const idColumn = fixedLeft(id, 100)
388
+ ```
389
+
390
+ - `fixedRight(def, width?)` - 固定列到右侧
391
+ ```javascript
392
+ const opColumn = fixedRight(op, 150)
393
+ ```
394
+
395
+ - `staticComp(def)` - 将字段转为静态组件(只读)
396
+ ```javascript
397
+ const readOnlyName = staticComp({ field: 'name', label: '姓名' })
398
+ ```
399
+
400
+ **样式定义:**
401
+
402
+ - `primaryStaticProps` - 主色文本样式
403
+ - `successStaticProps` - 成功色文本样式
404
+ - `warningStaticProps` - 警告色文本样式
405
+ - `sizeLongProps` - 长宽度输入框样式
406
+
407
+ **布局定义:**
408
+
409
+ - `blockItem` - 块级表单项
410
+ - `hiddenItem` - 隐藏表单项
411
+ - `rangeItem` - 范围表单项
412
+
413
+ **常用字段定义:**
414
+
415
+ - `dateTimeDef` - 日期时间字段
416
+ ```javascript
417
+ { type: 'date', props: { showTime: true }, filter: 'dateTime' }
418
+ ```
419
+
420
+ - `id` - ID 字段
421
+ ```javascript
422
+ { field: 'id', label: 'ID' }
423
+ ```
424
+
425
+ - `time` - 时间字段
426
+ - `timeCreate` - 创建时间字段
427
+ - `timeUpdate` - 更新时间字段
428
+ - `timeCreated` - 创建时间字段(别名)
429
+ - `timeUpdated` - 更新时间字段(别名)
430
+ - `remark` - 备注字段(文本域)
431
+ - `op` - 操作列字段
432
+
433
+ **七牛上传字段:**
434
+
435
+ - `qiniuFileDef` - 七牛文件上传字段
436
+ - `qiniuImageDef` - 七牛图片上传字段
437
+
438
+ ### 使用示例
439
+
440
+ ```javascript
441
+ import {
442
+ merge,
443
+ required,
444
+ dateTimeDayStartDef,
445
+ dateTimeDayEndDef,
446
+ id,
447
+ timeCreate,
448
+ timeUpdate,
449
+ remark,
450
+ fixedLeft,
451
+ fixedRight
452
+ } from '@yqg/simple/constant/fields'
453
+
454
+ export default {
455
+ data() {
456
+ return {
457
+ // 表单字段定义
458
+ formFields: [
459
+ required(merge(id, { label: '用户ID' })),
460
+ required({ field: 'name', label: '姓名' }),
461
+ {
462
+ ...dateTimeDayStartDef,
463
+ field: 'startTime',
464
+ label: '开始时间'
465
+ },
466
+ {
467
+ ...dateTimeDayEndDef,
468
+ field: 'endTime',
469
+ label: '结束时间'
470
+ },
471
+ remark
472
+ ],
473
+
474
+ // 表格列定义
475
+ tableColumns: [
476
+ fixedLeft(id, 80),
477
+ { field: 'name', label: '姓名' },
478
+ timeCreate,
479
+ timeUpdate,
480
+ fixedRight({ field: 'op', label: '操作' }, 120)
481
+ ]
482
+ }
483
+ }
484
+ }
485
+ ```
486
+
487
+ ## 文档
488
+
489
+ - [详细使用示例](./USAGE_EXAMPLE.md)
490
+ - [更新日志](./CHANGELOG.md)
491
+
492
+ ## 开发
493
+
494
+ ```bash
495
+ # 安装依赖
496
+ npm install
497
+
498
+ # 开发模式
499
+ npm run dev
500
+
501
+ # 构建
502
+ npm run build
503
+
504
+ # 代码检查
505
+ npm run lint
506
+ ```
507
+
508
+ ## License
509
+
510
+ MIT