@kne/form-creator 0.1.3 → 0.1.4
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 +125 -96
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -1
- package/dist/index.modern.js +58 -10
- package/dist/index.modern.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -366,99 +366,89 @@ render(<SchemaRenderExample />);
|
|
|
366
366
|
```
|
|
367
367
|
|
|
368
368
|
- 扩展组件与校验规则
|
|
369
|
-
- 通过
|
|
369
|
+
- 通过 preset({ rules, fields }) 一次扩展运行时校验、规则勾选面板与自定义填写项
|
|
370
370
|
- _FormCreator(@kne/current-lib_form-creator)[import * as _FormCreator from "@kne/form-creator"],(@kne/current-lib_form-creator/dist/index.css)[import "@kne/form-creator/dist/index.css"],_ReactFormAntd(@kne/react-form-antd)[import * as _ReactFormAntd from "@kne/react-form-antd"],_JsonView(@kne/json-view)[import * as _JsonView from "@kne/json-view"],(@kne/json-view/dist/index.css)[import "@kne/json-view/dist/index.css"],antd(antd)[import antd from "antd"]
|
|
371
371
|
|
|
372
372
|
```jsx
|
|
373
|
-
const {
|
|
374
|
-
|
|
375
|
-
registerField,
|
|
376
|
-
registerRulePreset,
|
|
377
|
-
defaultSchema,
|
|
378
|
-
createBlock,
|
|
379
|
-
createField
|
|
380
|
-
} = _FormCreator;
|
|
381
|
-
const { Rate, Slider, preset, RULES } = _ReactFormAntd;
|
|
373
|
+
const { default: FormCreator, preset, defaultSchema, createBlock, createField } = _FormCreator;
|
|
374
|
+
const { Rate, Slider } = _ReactFormAntd;
|
|
382
375
|
const { default: JsonView } = _JsonView;
|
|
383
376
|
const { useState } = React;
|
|
384
377
|
const { Alert, Typography, Space } = antd;
|
|
385
378
|
const { Text, Paragraph } = Typography;
|
|
386
379
|
|
|
387
|
-
//
|
|
380
|
+
// 一次 preset:运行时校验 + 编辑器规则面板 + 扩展填写项
|
|
388
381
|
preset({
|
|
389
|
-
rules:
|
|
390
|
-
// 身份证号(简化示例)
|
|
382
|
+
rules: {
|
|
391
383
|
ID_CARD: {
|
|
384
|
+
label: '身份证格式',
|
|
392
385
|
reg: /^\d{17}[\dXx]$/,
|
|
393
386
|
message: '%s格式不正确'
|
|
394
387
|
},
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
388
|
+
RATE_MIN: {
|
|
389
|
+
label: '评分至少3星',
|
|
390
|
+
validator: value => {
|
|
391
|
+
const score = Number(value) || 0;
|
|
392
|
+
return {
|
|
393
|
+
result: score >= 3,
|
|
394
|
+
errMsg: score >= 3 ? '' : '评分至少 3 星'
|
|
395
|
+
};
|
|
396
|
+
}
|
|
402
397
|
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
{
|
|
432
|
-
name: 'allowClear',
|
|
433
|
-
label: '允许清除',
|
|
434
|
-
type: 'boolean',
|
|
435
|
-
defaultValue: true
|
|
436
|
-
}
|
|
437
|
-
]
|
|
438
|
-
});
|
|
439
|
-
|
|
440
|
-
registerField('Slider', {
|
|
441
|
-
label: '滑块',
|
|
442
|
-
component: Slider,
|
|
443
|
-
defaultProps: { min: 0, max: 100, step: 1 },
|
|
444
|
-
propsSchema: [
|
|
445
|
-
{ name: 'min', label: '最小值', type: 'number', defaultValue: 0 },
|
|
446
|
-
{ name: 'max', label: '最大值', type: 'number', defaultValue: 100 },
|
|
447
|
-
{ name: 'step', label: '步长', type: 'number', min: 0, defaultValue: 1 },
|
|
448
|
-
{
|
|
449
|
-
name: 'tooltipPlacement',
|
|
450
|
-
label: '提示位置',
|
|
451
|
-
type: 'select',
|
|
452
|
-
defaultValue: 'top',
|
|
453
|
-
options: [
|
|
454
|
-
{ label: '上', value: 'top' },
|
|
455
|
-
{ label: '下', value: 'bottom' },
|
|
456
|
-
{ label: '左', value: 'left' },
|
|
457
|
-
{ label: '右', value: 'right' }
|
|
398
|
+
},
|
|
399
|
+
fields: {
|
|
400
|
+
Rate: {
|
|
401
|
+
label: '评分',
|
|
402
|
+
groupName: '评价组件',
|
|
403
|
+
component: Rate,
|
|
404
|
+
defaultProps: { count: 5, allowHalf: false, allowClear: true },
|
|
405
|
+
propsSchema: [
|
|
406
|
+
{
|
|
407
|
+
name: 'count',
|
|
408
|
+
label: '星星总数',
|
|
409
|
+
type: 'number',
|
|
410
|
+
min: 1,
|
|
411
|
+
max: 10,
|
|
412
|
+
defaultValue: 5
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
name: 'allowHalf',
|
|
416
|
+
label: '允许半星',
|
|
417
|
+
type: 'boolean',
|
|
418
|
+
defaultValue: false
|
|
419
|
+
},
|
|
420
|
+
{
|
|
421
|
+
name: 'allowClear',
|
|
422
|
+
label: '允许清除',
|
|
423
|
+
type: 'boolean',
|
|
424
|
+
defaultValue: true
|
|
425
|
+
}
|
|
458
426
|
]
|
|
459
427
|
},
|
|
460
|
-
|
|
461
|
-
|
|
428
|
+
Slider: {
|
|
429
|
+
label: '滑块',
|
|
430
|
+
component: Slider,
|
|
431
|
+
defaultProps: { min: 0, max: 100, step: 1 },
|
|
432
|
+
propsSchema: [
|
|
433
|
+
{ name: 'min', label: '最小值', type: 'number', defaultValue: 0 },
|
|
434
|
+
{ name: 'max', label: '最大值', type: 'number', defaultValue: 100 },
|
|
435
|
+
{ name: 'step', label: '步长', type: 'number', min: 0, defaultValue: 1 },
|
|
436
|
+
{
|
|
437
|
+
name: 'tooltipPlacement',
|
|
438
|
+
label: '提示位置',
|
|
439
|
+
type: 'select',
|
|
440
|
+
defaultValue: 'top',
|
|
441
|
+
options: [
|
|
442
|
+
{ label: '上', value: 'top' },
|
|
443
|
+
{ label: '下', value: 'bottom' },
|
|
444
|
+
{ label: '左', value: 'left' },
|
|
445
|
+
{ label: '右', value: 'right' }
|
|
446
|
+
]
|
|
447
|
+
},
|
|
448
|
+
{ name: 'dots', label: '显示刻度点', type: 'boolean', defaultValue: false }
|
|
449
|
+
]
|
|
450
|
+
}
|
|
451
|
+
}
|
|
462
452
|
});
|
|
463
453
|
|
|
464
454
|
const PresetExtendExample = () => {
|
|
@@ -467,7 +457,7 @@ const PresetExtendExample = () => {
|
|
|
467
457
|
blocks: [
|
|
468
458
|
createBlock('formInfo', {
|
|
469
459
|
title: '扩展字段与校验',
|
|
470
|
-
subtitle: '演示
|
|
460
|
+
subtitle: '演示 preset({ rules, fields }) 一次注册',
|
|
471
461
|
column: 2,
|
|
472
462
|
list: [
|
|
473
463
|
createField({
|
|
@@ -507,13 +497,10 @@ const PresetExtendExample = () => {
|
|
|
507
497
|
description={
|
|
508
498
|
<div>
|
|
509
499
|
<Paragraph style={{ marginBottom: 4 }}>
|
|
510
|
-
|
|
511
|
-
</Paragraph>
|
|
512
|
-
<Paragraph style={{ marginBottom: 4 }}>
|
|
513
|
-
2. <Text code>registerRulePreset</Text>:把规则挂到「填写规则」勾选面板
|
|
500
|
+
调用 <Text code>{'preset({ rules, fields })'}</Text> 即可同时注册:运行时校验(@kne/react-form-antd)、编辑器「填写规则」勾选面板、扩展填写项类型。
|
|
514
501
|
</Paragraph>
|
|
515
502
|
<Paragraph style={{ marginBottom: 0 }}>
|
|
516
|
-
|
|
503
|
+
规则项在 <Text code>rules</Text> 中写 <Text code>label</Text> + 校验定义(<Text code>reg/message</Text> 或 <Text code>validator</Text>);字段在 <Text code>fields</Text> 中按 type 配置,支持 <Text code>groupName</Text> 与 <Text code>propsSchema</Text>。
|
|
517
504
|
</Paragraph>
|
|
518
505
|
</div>
|
|
519
506
|
}
|
|
@@ -661,27 +648,65 @@ import { Form, SubmitButton } from '@kne/form-info';
|
|
|
661
648
|
#### 工具方法
|
|
662
649
|
|
|
663
650
|
- `createBlock(kind)` / `createStep()` / `normalizeSchema(schema)`
|
|
664
|
-
- `
|
|
665
|
-
- `registerRulePreset({ value, label })` 扩展「填写规则」勾选面板中的规则项
|
|
651
|
+
- `preset({ rules, fields })` 一次注册扩展规则与填写项(推荐)
|
|
666
652
|
- `parseRuleString(rule)` / `buildRuleString(config)` 校验规则字符串解析/组装
|
|
667
653
|
|
|
668
|
-
####
|
|
654
|
+
#### preset(推荐)
|
|
669
655
|
|
|
670
|
-
|
|
656
|
+
应用入口调用一次,同时完成:
|
|
671
657
|
|
|
672
|
-
|
|
658
|
+
1. **运行时校验**:合并进 `@kne/react-form-antd` 的 `RULES`
|
|
659
|
+
2. **编辑器规则面板**:写入「填写规则」勾选列表
|
|
660
|
+
3. **扩展填写项**:注册到字段类型 registry
|
|
661
|
+
|
|
662
|
+
内置规则 `REQ` / `TEL` / `EMAIL` 已内置,无需重复配置;传入同名 key 可覆盖运行时规则并更新编辑器 label。
|
|
673
663
|
|
|
674
664
|
```js
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
665
|
+
import { preset } from '@kne/form-creator';
|
|
666
|
+
import { Rate, Slider } from '@kne/react-form-antd';
|
|
667
|
+
|
|
668
|
+
preset({
|
|
669
|
+
rules: {
|
|
670
|
+
ID_CARD: {
|
|
671
|
+
label: '身份证格式',
|
|
672
|
+
reg: /^\d{17}[\dXx]$/,
|
|
673
|
+
message: '%s格式不正确'
|
|
674
|
+
},
|
|
675
|
+
RATE_MIN: {
|
|
676
|
+
label: '评分至少3星',
|
|
677
|
+
validator: value => ({
|
|
678
|
+
result: Number(value) >= 3,
|
|
679
|
+
errMsg: Number(value) >= 3 ? '' : '评分至少 3 星'
|
|
680
|
+
})
|
|
681
|
+
}
|
|
682
|
+
},
|
|
683
|
+
fields: {
|
|
684
|
+
Rate: {
|
|
685
|
+
label: '评分',
|
|
686
|
+
groupName: '评价组件',
|
|
687
|
+
component: Rate,
|
|
688
|
+
defaultProps: { count: 5 },
|
|
689
|
+
propsSchema: [
|
|
690
|
+
{ name: 'count', label: '星星总数', type: 'number', min: 1, max: 10, defaultValue: 5 }
|
|
691
|
+
]
|
|
692
|
+
}
|
|
693
|
+
}
|
|
683
694
|
});
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
| 配置 | 类型 | 说明 |
|
|
698
|
+
|------|------|------|
|
|
699
|
+
| `rules` | `Record<string, RuleDef>` | key 为规则 token;`RuleDef` 含 `label` + `reg/message` 或 `validator` |
|
|
700
|
+
| `fields` | `Record<string, FieldDef>` | key 为字段 type,形状同 `registerField` 第二参数 |
|
|
684
701
|
|
|
702
|
+
#### 高级用法(一般请用 preset)
|
|
703
|
+
|
|
704
|
+
- `registerField(type, definition)` 单独扩展字段类型
|
|
705
|
+
- `registerRulePreset({ value, label })` 单独扩展规则勾选项
|
|
706
|
+
|
|
707
|
+
扩展填写项时,用 `propsSchema` 声明可编辑的额外参数(写入字段 `props`),编辑器会按声明自动生成「填写项设置」表单:
|
|
708
|
+
|
|
709
|
+
```js
|
|
685
710
|
registerField('Slider', {
|
|
686
711
|
label: '滑块',
|
|
687
712
|
component: Slider,
|
|
@@ -705,6 +730,10 @@ registerField('Slider', {
|
|
|
705
730
|
});
|
|
706
731
|
```
|
|
707
732
|
|
|
733
|
+
#### 扩展组件与校验(示例见「扩展组件与校验规则」)
|
|
734
|
+
|
|
735
|
+
使用 `preset({ rules, fields })` 即可,无需再单独调用 `registerField` / `registerRulePreset` / react-form-antd 的 `preset`。
|
|
736
|
+
|
|
708
737
|
`propsSchema` 项约定:
|
|
709
738
|
|
|
710
739
|
| 字段 | 说明 |
|
package/dist/index.js
CHANGED
|
@@ -4369,6 +4369,57 @@ const FormCreator = withLocale$1(_ref => {
|
|
|
4369
4369
|
});
|
|
4370
4370
|
var FormCreator$1 = FormCreator;
|
|
4371
4371
|
|
|
4372
|
+
/**
|
|
4373
|
+
* 统一注册扩展规则与填写项:运行时校验 + 编辑器规则面板 + 字段类型 registry。
|
|
4374
|
+
* 应用入口调用一次即可,FormCreator / SchemaRenderer 共用。
|
|
4375
|
+
*/
|
|
4376
|
+
const preset = function (_temp) {
|
|
4377
|
+
let {
|
|
4378
|
+
rules = {},
|
|
4379
|
+
fields = {}
|
|
4380
|
+
} = _temp === void 0 ? {} : _temp;
|
|
4381
|
+
const runtimeRules = {
|
|
4382
|
+
...reactFormAntd.RULES
|
|
4383
|
+
};
|
|
4384
|
+
Object.entries(rules).forEach(_ref => {
|
|
4385
|
+
let [value, def] = _ref;
|
|
4386
|
+
if (def == null) {
|
|
4387
|
+
return;
|
|
4388
|
+
}
|
|
4389
|
+
if (typeof def === 'function') {
|
|
4390
|
+
registerRulePreset({
|
|
4391
|
+
value,
|
|
4392
|
+
label: value
|
|
4393
|
+
});
|
|
4394
|
+
runtimeRules[value] = def;
|
|
4395
|
+
return;
|
|
4396
|
+
}
|
|
4397
|
+
const {
|
|
4398
|
+
label,
|
|
4399
|
+
validator,
|
|
4400
|
+
...ruleBody
|
|
4401
|
+
} = def;
|
|
4402
|
+
if (label) {
|
|
4403
|
+
registerRulePreset({
|
|
4404
|
+
value,
|
|
4405
|
+
label
|
|
4406
|
+
});
|
|
4407
|
+
}
|
|
4408
|
+
if (typeof validator === 'function') {
|
|
4409
|
+
runtimeRules[value] = validator;
|
|
4410
|
+
} else if (Object.keys(ruleBody).length) {
|
|
4411
|
+
runtimeRules[value] = ruleBody;
|
|
4412
|
+
}
|
|
4413
|
+
});
|
|
4414
|
+
reactFormAntd.preset({
|
|
4415
|
+
rules: runtimeRules
|
|
4416
|
+
});
|
|
4417
|
+
Object.entries(fields).forEach(_ref2 => {
|
|
4418
|
+
let [type, definition] = _ref2;
|
|
4419
|
+
registerField(type, definition);
|
|
4420
|
+
});
|
|
4421
|
+
};
|
|
4422
|
+
|
|
4372
4423
|
exports.FormCreator = FormCreator$1;
|
|
4373
4424
|
exports.RULE_LEN_PRESET = RULE_LEN_PRESET;
|
|
4374
4425
|
exports.RULE_PRESET_ITEMS = RULE_PRESET_ITEMS;
|
|
@@ -4406,6 +4457,7 @@ exports.normalizeSchema = normalizeSchema;
|
|
|
4406
4457
|
exports.parseRuleString = parseRuleString;
|
|
4407
4458
|
exports.pickFieldProps = pickFieldProps;
|
|
4408
4459
|
exports.pickFromPropsSchema = pickFromPropsSchema;
|
|
4460
|
+
exports.preset = preset;
|
|
4409
4461
|
exports.registerField = registerField;
|
|
4410
4462
|
exports.registerRulePreset = registerRulePreset;
|
|
4411
4463
|
exports.updateBlocks = updateBlocks;
|