@kne/form-creator 0.1.27 → 0.1.29

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
@@ -33,19 +33,62 @@ npm i --save @kne/form-creator
33
33
  #### 示例代码
34
34
 
35
35
  - 配置式表单搭建(全屏)
36
- - 通过模块列表添加/编辑/排序,右侧实时预览;默认示例含列表嵌套与选项切换
37
- - _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"],_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"],_FormInfo(@kne/form-info)[import * as _FormInfo from "@kne/form-info"]
36
+ - 模块搭建与实时预览;工具栏可导入/导出 Schema JSON(弹窗内 JSONEditor)
37
+ - _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"],_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"],remoteLoader(@kne/remote-loader)[import * as remoteLoader from "@kne/remote-loader"]
38
38
 
39
39
  ```jsx
40
- const {default: FormCreator, defaultSchema, createBlock, createField, createStep, createChoiceOption} = _FormCreator;
40
+ const {
41
+ default: FormCreator,
42
+ defaultSchema,
43
+ createBlock,
44
+ createField,
45
+ createStep,
46
+ createChoiceOption,
47
+ normalizeSchema
48
+ } = _FormCreator;
41
49
  const {default: JsonView} = _JsonView;
42
- const {useState} = React;
43
- const {Typography} = antd;
50
+ const {createWithRemoteLoader, preset: remoteLoaderPreset} = remoteLoader;
51
+ const {useState, useRef} = React;
52
+ const {Typography, Button, Space, Alert, message, Modal} = antd;
44
53
 
45
54
  const {Text} = Typography;
46
55
 
47
- const BaseExample = () => {
48
- const [schema, setSchema] = useState(() => ({
56
+ /** 仅在用到 JSONEditor 时声明 components-thirdparty(merge 进全局 remotes,不覆盖 core) */
57
+ remoteLoaderPreset({
58
+ remotes: {
59
+ 'components-thirdparty': {
60
+ url: 'https://uc.fatalent.cn',
61
+ tpl: '{{url}}/packages/@kne-components/{{remote}}/{{version}}/build',
62
+ remote: 'components-thirdparty',
63
+ defaultVersion: '0.1.64'
64
+ }
65
+ }
66
+ });
67
+
68
+ const SchemaJsonEditor = ({JSONEditorField, initialText, textRef}) => {
69
+ const [text, setText] = useState(initialText);
70
+ return (
71
+ <JSONEditorField
72
+ height={420}
73
+ value={text}
74
+ onChange={next => {
75
+ setText(next);
76
+ textRef.current = next;
77
+ }}
78
+ />
79
+ );
80
+ };
81
+
82
+ const BaseExample = createWithRemoteLoader({
83
+ // JSONEditor.Field:纯 UI(value/onChange);默认 JSONEditor 是 FormInfo 字段
84
+ // Schema 弹窗用 antd Modal 受控打开(示例页未挂 ModalContextHolder,useModal 会静默无效果)
85
+ modules: ['components-thirdparty:JSONEditor.Field']
86
+ })(({remoteModules}) => {
87
+ const [JSONEditorField] = remoteModules;
88
+ const [schemaJsonOpen, setSchemaJsonOpen] = useState(false);
89
+ const schemaJsonTextRef = useRef('');
90
+ const [schemaJsonInitial, setSchemaJsonInitial] = useState('');
91
+ const [schema, setSchema] = useState(() => ({
49
92
  ...defaultSchema(),
50
93
  actions: {
51
94
  showSubmit: true,
@@ -547,14 +590,91 @@ const BaseExample = () => {
547
590
  ]
548
591
  }));
549
592
 
550
- return <div>
551
- <FormCreator value={schema} onChange={setSchema} />
552
- <div style={{marginTop: 16}}>
553
- <JsonView data={schema} theme="light" collapsedFrom={2} searchable={false} />
554
- <Text type="secondary">初始示例已包含表单分组、列表、表格列表、行内编辑列表、列表嵌套、选项切换、多字段、分步等模块。</Text>
555
- </div>
556
- </div>;
557
- };
593
+ const handleSchemaJson = () => {
594
+ const initialText = JSON.stringify(schema, null, 2);
595
+ schemaJsonTextRef.current = initialText;
596
+ setSchemaJsonInitial(initialText);
597
+ setSchemaJsonOpen(true);
598
+ };
599
+
600
+ const handleCopySchemaJson = () => {
601
+ const text = schemaJsonTextRef.current;
602
+ if (navigator.clipboard?.writeText) {
603
+ navigator.clipboard.writeText(text).then(
604
+ () => message.success('已复制 Schema JSON'),
605
+ () => message.error('复制失败')
606
+ );
607
+ } else {
608
+ message.info(text);
609
+ }
610
+ };
611
+
612
+ const handleApplySchemaJson = () => {
613
+ const text = schemaJsonTextRef.current;
614
+ if (!String(text || '').trim()) {
615
+ message.warning('请先编辑 Schema JSON');
616
+ return;
617
+ }
618
+ try {
619
+ setSchema(normalizeSchema(JSON.parse(text)));
620
+ message.success('已应用 Schema');
621
+ setSchemaJsonOpen(false);
622
+ } catch (e) {
623
+ message.error('JSON 无效,请检查后再试');
624
+ }
625
+ };
626
+
627
+ return (
628
+ <Space direction="vertical" size={16} style={{width: '100%'}}>
629
+ <Alert
630
+ type="info"
631
+ showIcon
632
+ message="Schema JSON"
633
+ description={
634
+ <span>
635
+ 工具栏「Schema JSON」打开弹窗(远程 <Text code>components-thirdparty:JSONEditor.Field</Text>
636
+ ):「复制」导出当前内容,「应用」把修改写回编辑器(经 <Text code>normalizeSchema</Text>)。
637
+ </span>
638
+ }
639
+ />
640
+ <FormCreator
641
+ value={schema}
642
+ onChange={setSchema}
643
+ extraToolbar={
644
+ <Button size="small" onClick={handleSchemaJson}>
645
+ Schema JSON
646
+ </Button>
647
+ }
648
+ />
649
+ <Modal
650
+ title="Schema JSON"
651
+ open={schemaJsonOpen}
652
+ onCancel={() => setSchemaJsonOpen(false)}
653
+ width={860}
654
+ destroyOnHidden
655
+ footer={
656
+ <Space>
657
+ <Button onClick={() => setSchemaJsonOpen(false)}>取消</Button>
658
+ <Button onClick={handleCopySchemaJson}>复制</Button>
659
+ <Button type="primary" onClick={handleApplySchemaJson}>
660
+ 应用
661
+ </Button>
662
+ </Space>
663
+ }
664
+ >
665
+ {schemaJsonOpen ? (
666
+ <SchemaJsonEditor key={schemaJsonInitial} JSONEditorField={JSONEditorField} initialText={schemaJsonInitial} textRef={schemaJsonTextRef} />
667
+ ) : null}
668
+ </Modal>
669
+ <div>
670
+ <JsonView data={schema} theme="light" collapsedFrom={2} searchable={false} />
671
+ <Text type="secondary">
672
+ 初始示例已包含表单分组、列表、表格列表、行内编辑列表、列表嵌套、选项切换、多字段、分步、矩阵量表等模块。
673
+ </Text>
674
+ </div>
675
+ </Space>
676
+ );
677
+ });
558
678
 
559
679
  render(<BaseExample />);
560
680
 
@@ -662,6 +782,118 @@ render(<SchemaRenderExample />);
662
782
 
663
783
  ```
664
784
 
785
+ - 步骤展示样式
786
+ - steps 模块的 stepsStyle:default 完整步骤条(默认),focus 突出当前步骤标题与分段进度
787
+ - _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"],antd(antd)[import antd from "antd"]
788
+
789
+ ```jsx
790
+ const { SchemaRenderer, createBlock, createField, createStep } = _FormCreator;
791
+ const { useMemo, useState } = React;
792
+ const { Alert, Flex, Segmented, Typography } = antd;
793
+ const { Text } = Typography;
794
+
795
+ const createOnboardingSteps = stepsStyle =>
796
+ createBlock('steps', {
797
+ title: '入职信息登记',
798
+ subtitle: '按步骤完成,每步校验通过后进入下一步',
799
+ stepsStyle,
800
+ items: [
801
+ createStep({
802
+ title: '个人信息',
803
+ column: 2,
804
+ list: [
805
+ createField({ type: 'Input', name: 'name', label: '姓名', rule: 'REQ', props: { placeholder: '请输入姓名' } }),
806
+ createField({ type: 'Input', name: 'mobile', label: '手机号', rule: 'REQ TEL', props: { placeholder: '请输入手机号' } }),
807
+ createField({ type: 'Input', name: 'email', label: '邮箱', rule: 'REQ EMAIL', props: { placeholder: 'name@company.com' } }),
808
+ createField({ type: 'DatePicker', name: 'birthday', label: '出生日期', props: { placeholder: '请选择日期' } })
809
+ ]
810
+ }),
811
+ createStep({
812
+ title: '岗位与入职安排(含所属部门、岗位名称与入职日期确认,请与 HR 沟通后填写)',
813
+ column: 2,
814
+ list: [
815
+ createField({
816
+ type: 'Select',
817
+ name: 'department',
818
+ label: '所属部门',
819
+ rule: 'REQ',
820
+ props: {
821
+ placeholder: '请选择部门',
822
+ options: [
823
+ { label: '产品研发中心', value: 'rd' },
824
+ { label: '市场营销部', value: 'marketing' },
825
+ { label: '人力资源部', value: 'hr' }
826
+ ]
827
+ }
828
+ }),
829
+ createField({ type: 'Input', name: 'position', label: '岗位名称', rule: 'REQ', props: { placeholder: '例如:高级前端工程师' } }),
830
+ createField({ type: 'DatePicker', name: 'joinDate', label: '入职日期', rule: 'REQ', props: { placeholder: '请选择日期' } })
831
+ ]
832
+ }),
833
+ createStep({
834
+ title: '紧急联系人',
835
+ column: 2,
836
+ list: [
837
+ createField({ type: 'Input', name: 'contactName', label: '联系人姓名', rule: 'REQ', props: { placeholder: '请输入姓名' } }),
838
+ createField({ type: 'Input', name: 'contactMobile', label: '联系人电话', rule: 'REQ TEL', props: { placeholder: '请输入手机号' } })
839
+ ]
840
+ }),
841
+ createStep({
842
+ title: '银行卡与薪资账户',
843
+ column: 2,
844
+ list: [
845
+ createField({ type: 'Input', name: 'bankName', label: '开户银行', rule: 'REQ', props: { placeholder: '例如:招商银行上海分行' } }),
846
+ createField({ type: 'Input', name: 'bankAccount', label: '银行卡号', rule: 'REQ', props: { placeholder: '请输入银行卡号' } })
847
+ ]
848
+ }),
849
+ createStep({
850
+ title: '其他说明',
851
+ column: 1,
852
+ list: [createField({ type: 'TextArea', name: 'remark', label: '备注', block: true, props: { rows: 3, placeholder: '如有特殊情况请说明' } })]
853
+ })
854
+ ]
855
+ });
856
+
857
+ const StepsStyleExample = () => {
858
+ const [stepsStyle, setStepsStyle] = useState('focus');
859
+ const schema = useMemo(
860
+ () => ({
861
+ actions: { showSubmit: true, showReset: true, submitText: '提交登记' },
862
+ blocks: [createOnboardingSteps(stepsStyle)]
863
+ }),
864
+ [stepsStyle]
865
+ );
866
+
867
+ return (
868
+ <Flex vertical gap={16}>
869
+ <Alert
870
+ type="info"
871
+ showIcon
872
+ message="步骤展示样式"
873
+ description={
874
+ <span>
875
+ steps 模块通过 <Text code>stepsStyle</Text> 切换导航样式:<Text code>default</Text>(默认)为完整步骤条;
876
+ <Text code>focus</Text> 突出当前步骤标题(长标题自动换行)与分段进度,已完成的进度段可点击回退,适合步骤多、标题长或窄屏场景。编辑器中可在「编辑模块」里配置。
877
+ </span>
878
+ }
879
+ />
880
+ <Segmented
881
+ value={stepsStyle}
882
+ onChange={setStepsStyle}
883
+ options={[
884
+ { label: '聚焦当前步骤(focus)', value: 'focus' },
885
+ { label: '完整步骤条(default)', value: 'default' }
886
+ ]}
887
+ />
888
+ <SchemaRenderer key={stepsStyle} schema={schema} />
889
+ </Flex>
890
+ );
891
+ };
892
+
893
+ render(<StepsStyleExample />);
894
+
895
+ ```
896
+
665
897
  - 列表嵌套与选项切换
666
898
  - 列表每一条里可再放分组或子列表;选项切换支持单选/多选,每个选项可添加填写项
667
899
  - _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"],_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"]
@@ -2143,6 +2375,7 @@ import { SchemaContent, SchemaContentInner } from '@kne/form-creator';
2143
2375
  name?, label?, addText?, addPosition?, maxLength?, minLength?, fieldType?, autoStep?,
2144
2376
  layout?, rowTitle?, rule?, rows?, columns?, // matrixScale
2145
2377
  mode?, selectorName?, selectorInData?, discriminator?,
2378
+ stepsStyle?: 'default' | 'focus', // steps 导航样式,默认 default
2146
2379
  list?: [{ id, type, name, label, tips?, description?, rule?, block?, hidden?, locked?, visibleWhen?, props? }],
2147
2380
  itemBlocks?: Block[], // list / editableList:列表项内子模块
2148
2381
  blocks?: Block[], // formInfo / object:子模块
@@ -2242,7 +2475,7 @@ FormCreator 不传 `languages` 时为单语言输入(与改造前一致,存
2242
2475
  | choice | 选项分支 | title, **mode**(`single`\|`multiple`), **minLength** / **maxLength**(仅多选:最少/最多选几项), selectorName, selectorInData, options[{ title, list, **blocks** }] |
2243
2476
  | multiField | 同类型多值 | name, label, fieldType, addText(不支持子模块) |
2244
2477
  | matrixScale | 矩阵量表(`@kne/form-info` MatrixScale):每行共用同一 `fieldType` | name, title, **rowTitle**, **layout**(`options` 选项矩阵 \| `columns` 单元格矩阵 \| `field` 每行一个字段), fieldType(默认 RadioGroup), rule(如 `REQ`), **rows**`[{ label, name }]`, **options**(layout=options), **columns**`[{ label, name }]`(layout=columns);不支持子模块 |
2245
- | steps | 步骤表单 | title, subtitle, bordered, autoStep, items[{ title, column, list, **blocks** }] |
2478
+ | steps | 步骤表单 | title, subtitle, bordered, autoStep, stepsStyle(`default` 完整步骤条 / `focus` 突出当前步骤标题与分段进度), items[{ title, column, list, **blocks** }] |
2246
2479
 
2247
2480
  `choice`:顶部同一条选项 UI;`single` 只挂载当前选项内容,`multiple` 平铺所有选中项。多选时可配置 `minLength` / `maxLength` 限制最少、最多选择几个(提交时校验;达到上限后未选项禁用)。JSON Schema 的 oneOf/anyOf 可分别映射为 `mode: 'single'/'multiple'`。`allOf` 无需独立 kind,展开为多个 sibling blocks 即可。
2248
2481