@plasmicpkgs/antd5 0.0.84 → 0.0.85

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.
@@ -1,11 +1,12 @@
1
1
  import { CodeComponentMode, ComponentHelpers } from "@plasmicapp/host";
2
- import { CanvasComponentProps } from "@plasmicapp/host/registerComponent";
3
2
  import { Form } from "antd";
4
3
  import type { FormInstance, FormProps } from "antd/es/form";
5
4
  import type { FormItemProps } from "antd/es/form/FormItem";
6
5
  import type { FormListOperation, FormListProps } from "antd/es/form/FormList";
7
6
  import React from "react";
8
7
  import { Registerable } from "./utils";
8
+ import { CanvasComponentProps } from "@plasmicapp/host/registerComponent";
9
+ import { TableSchema, DataOp } from "@plasmicapp/data-sources";
9
10
  interface InternalFormItemProps extends Omit<FormItemProps, "rules"> {
10
11
  rules?: PlasmicRule[];
11
12
  noLabel?: boolean;
@@ -35,6 +36,18 @@ export interface SimplifiedFormItemsProp extends InternalFormItemProps {
35
36
  }
36
37
  interface FormWrapperControlContextData {
37
38
  formInstance: FormInstance<any>;
39
+ schema?: TableSchema;
40
+ minimalFullLengthFields: Partial<SimplifiedFormItemsProp>[];
41
+ mergedFields: SimplifiedFormItemsProp[];
42
+ }
43
+ export declare enum FormType {
44
+ NewEntry = 0,
45
+ UpdateEntry = 1
46
+ }
47
+ interface DBConnectionProp {
48
+ formType: FormType;
49
+ dataSchema: DataOp;
50
+ dataEntry: DataOp;
38
51
  }
39
52
  interface FormWrapperProps extends FormProps, CanvasComponentProps<FormWrapperControlContextData> {
40
53
  /**
@@ -47,7 +60,9 @@ interface FormWrapperProps extends FormProps, CanvasComponentProps<FormWrapperCo
47
60
  extendedOnValuesChange?: (values: Parameters<NonNullable<FormProps["onValuesChange"]>>[1]) => void;
48
61
  formItems: SimplifiedFormItemsProp[];
49
62
  mode?: CodeComponentMode;
63
+ formType: "new-entry" | "update-entry";
50
64
  submitSlot?: boolean;
65
+ data?: DBConnectionProp;
51
66
  }
52
67
  interface InternalFormInstanceContextData {
53
68
  fireOnValuesChange: () => void;
@@ -65,9 +80,12 @@ interface FormRefActions extends Pick<FormInstance<any>, "setFieldsValue" | "res
65
80
  export declare const FormWrapper: React.ForwardRefExoticComponent<FormWrapperProps & React.RefAttributes<FormRefActions>>;
66
81
  export declare const formHelpers: ComponentHelpers<FormWrapperProps>;
67
82
  export declare const formComponentName = "plasmic-antd5-form";
83
+ export declare const formTypeDescription = "\n You can create form with two different behaviors:\n \n\n\n 1. Create a new entry: The form will be created empty and it will create a new row when submitted.\n 2. Update an entry: The form will be pre-filled with the row values and it will update the table entry when submitted.\n \n\n\n For both options, you can customize later.\n";
68
84
  export declare function registerForm(loader?: Registerable): void;
69
85
  interface FormControlContextData {
70
86
  internalFormCtx?: InternalFormInstanceContextData;
87
+ formInstance?: FormInstance<any>;
88
+ parentFormItemPath: (string | number)[];
71
89
  }
72
90
  interface CuratedFieldData {
73
91
  status: string | undefined;
package/dist/utils.d.ts CHANGED
@@ -22,6 +22,7 @@ type ReactElt = {
22
22
  export declare function traverseReactEltTree(children: React.ReactNode, callback: (elt: ReactElt) => void): void;
23
23
  export declare function asArray<T>(x: T[] | T | undefined | null): T[];
24
24
  export declare function omit<T extends object>(obj: T, ...keys: (keyof T)[]): Partial<T>;
25
+ export declare function has<T extends object>(obj: T, keys: (keyof T)[]): boolean;
25
26
  export declare function usePrevious<T>(value: T | undefined): T | undefined;
26
27
  export declare function capitalize(value: string): string;
27
28
  export declare function ensureArray<T>(x: T | T[]): T[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plasmicpkgs/antd5",
3
- "version": "0.0.84",
3
+ "version": "0.0.85",
4
4
  "description": "Plasmic registration calls for antd components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -55,7 +55,7 @@
55
55
  "antd": "^5.1.6"
56
56
  },
57
57
  "devDependencies": {
58
- "@plasmicapp/host": "1.0.150",
58
+ "@plasmicapp/host": "1.0.151",
59
59
  "@plasmicapp/query": "0.1.67",
60
60
  "@types/node": "^14.0.26",
61
61
  "@types/react": "^18.0.27",
@@ -76,5 +76,5 @@
76
76
  "publishConfig": {
77
77
  "access": "public"
78
78
  },
79
- "gitHead": "d933fd1793c7b2d01af2400582593724ec364085"
79
+ "gitHead": "35d4595c6d82b47128fdb0c536fab54d7bbc609f"
80
80
  }
@@ -11,6 +11,7 @@ var registerInput = require('./registerInput.cjs.js');
11
11
  var registerRadio = require('./registerRadio.cjs.js');
12
12
  var registerSwitch = require('./registerSwitch.cjs.js');
13
13
  var utils = require('./utils-838eeae8.cjs.js');
14
+ var dataSources = require('@plasmicapp/data-sources');
14
15
  require('classnames');
15
16
  require('@plasmicapp/host/registerComponent');
16
17
  require('@plasmicapp/host/registerGlobalContext');
@@ -67,18 +68,23 @@ var InputType = /* @__PURE__ */ ((InputType2) => {
67
68
  InputType2["DatePicker"] = "DatePicker";
68
69
  return InputType2;
69
70
  })(InputType || {});
71
+ var FormType = /* @__PURE__ */ ((FormType2) => {
72
+ FormType2[FormType2["NewEntry"] = 0] = "NewEntry";
73
+ FormType2[FormType2["UpdateEntry"] = 1] = "UpdateEntry";
74
+ return FormType2;
75
+ })(FormType || {});
70
76
  const PathContext = React__default.default.createContext({ relativePath: [], fullPath: [] });
71
77
  const InternalFormInstanceContext = React__default.default.createContext(void 0);
72
78
  const FormLayoutContext = React__default.default.createContext(void 0);
73
79
  const Internal = React__default.default.forwardRef(
74
80
  (props, ref) => {
75
- var _b, _c, _d, _e, _f;
81
+ var _b, _c, _d, _e;
76
82
  const [form] = antd.Form.useForm();
77
83
  const values = form.getFieldsValue(true);
78
84
  const lastValue = React__default.default.useRef(values);
79
85
  const _a = props, { extendedOnValuesChange, setRemountKey } = _a, rest = __objRest(_a, ["extendedOnValuesChange", "setRemountKey"]);
80
86
  let childrenNode;
81
- if (props.mode !== "simplified") {
87
+ if (props.mode === "advanced") {
82
88
  childrenNode = typeof props.children === "function" ? props.children(values, form) : props.children;
83
89
  } else {
84
90
  childrenNode = /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, ((_b = props.formItems) != null ? _b : []).map((formItem) => /* @__PURE__ */ React__default.default.createElement(
@@ -117,7 +123,6 @@ const Internal = React__default.default.forwardRef(
117
123
  },
118
124
  [props.layout, (_c = props.labelCol) == null ? void 0 : _c.span]
119
125
  );
120
- (_d = props.setControlContextData) == null ? void 0 : _d.call(props, { formInstance: form });
121
126
  React__default.default.useImperativeHandle(ref, () => ({
122
127
  formInstance: form,
123
128
  setFieldsValue: (newValues) => {
@@ -158,8 +163,8 @@ const Internal = React__default.default.forwardRef(
158
163
  extendedOnValuesChange == null ? void 0 : extendedOnValuesChange(args[1]);
159
164
  },
160
165
  form,
161
- labelCol: ((_e = props.labelCol) == null ? void 0 : _e.horizontalOnly) && props.layout !== "horizontal" ? void 0 : props.labelCol,
162
- wrapperCol: ((_f = props.wrapperCol) == null ? void 0 : _f.horizontalOnly) && props.layout !== "horizontal" ? void 0 : props.wrapperCol
166
+ labelCol: ((_d = props.labelCol) == null ? void 0 : _d.horizontalOnly) && props.layout !== "horizontal" ? void 0 : props.labelCol,
167
+ wrapperCol: ((_e = props.wrapperCol) == null ? void 0 : _e.horizontalOnly) && props.layout !== "horizontal" ? void 0 : props.wrapperCol
163
168
  }),
164
169
  /* @__PURE__ */ React__default.default.createElement("style", null, `
165
170
  .ant-form-item-explain + div, .ant-form-item-margin-offset {
@@ -171,6 +176,51 @@ const Internal = React__default.default.forwardRef(
171
176
  );
172
177
  }
173
178
  );
179
+ function useFormItemDefinitions(rawData, props, formRef) {
180
+ const { mode, formItems, setControlContextData } = props;
181
+ return React__default.default.useMemo(() => {
182
+ if (mode !== "simplified" || !formRef || !rawData || rawData.error) {
183
+ return void 0;
184
+ }
185
+ const data = dataSources.normalizeData(rawData);
186
+ const schema = data == null ? void 0 : data.schema;
187
+ if (!data || !schema || !data.data) {
188
+ return void 0;
189
+ }
190
+ const row = data.data.length > 0 ? data.data[0] : void 0;
191
+ const { mergedFields, minimalFullLengthFields } = dataSources.deriveFieldConfigs(
192
+ formItems != null ? formItems : [],
193
+ schema,
194
+ (field) => __spreadValues({
195
+ inputType: "Text" /* Text */
196
+ }, field && {
197
+ key: field.id,
198
+ fieldId: field.id,
199
+ label: field.label,
200
+ name: field.id,
201
+ inputType: field.type === "string" ? "Text" /* Text */ : field.type === "number" ? "Number" /* Number */ : field.type === "boolean" ? "Checkbox" /* Checkbox */ : "Text" /* Text */,
202
+ //missing date and date-time
203
+ initialValue: row ? row[field.id] : void 0,
204
+ hidden: field.primaryKey
205
+ })
206
+ );
207
+ setControlContextData == null ? void 0 : setControlContextData({
208
+ schema: data.schema,
209
+ minimalFullLengthFields,
210
+ mergedFields,
211
+ formInstance: formRef.formInstance
212
+ });
213
+ return mergedFields;
214
+ }, [mode, setControlContextData, formItems, rawData, formRef]);
215
+ }
216
+ const useRawData = (connection) => {
217
+ const rawDataSchema = dataSources.usePlasmicDataOp(connection == null ? void 0 : connection.dataSchema);
218
+ const rawDataEntry = dataSources.usePlasmicDataOp(connection == null ? void 0 : connection.dataEntry);
219
+ if (!connection) {
220
+ return void 0;
221
+ }
222
+ return 0 /* NewEntry */ === connection.formType ? rawDataSchema : rawDataEntry;
223
+ };
174
224
  const FormWrapper = React__default.default.forwardRef(
175
225
  (props, ref) => {
176
226
  const [remountKey, setRemountKey] = React__default.default.useState(0);
@@ -185,12 +235,24 @@ const FormWrapper = React__default.default.forwardRef(
185
235
  ref,
186
236
  () => wrapperRef.current ? __spreadValues({}, wrapperRef.current) : {}
187
237
  );
238
+ const rawData = useRawData(props.data);
239
+ const formItemDefinitions = useFormItemDefinitions(
240
+ rawData,
241
+ props,
242
+ wrapperRef.current
243
+ );
244
+ const _a = props, { formItems } = _a, rest = __objRest(_a, ["formItems"]);
245
+ const actualFormItems = props.mode === "simplified" && formItemDefinitions ? formItemDefinitions : formItems;
246
+ if (props.mode === "simplified" && rawData && "error" in rawData) {
247
+ return /* @__PURE__ */ React__default.default.createElement("div", null, "Error when fetching data: ", rawData.error.message);
248
+ }
188
249
  return /* @__PURE__ */ React__default.default.createElement(
189
250
  Internal,
190
251
  __spreadProps(__spreadValues({
191
252
  key: remountKey
192
- }, props), {
253
+ }, rest), {
193
254
  setRemountKey,
255
+ formItems: actualFormItems,
194
256
  ref: wrapperRef
195
257
  })
196
258
  );
@@ -209,7 +271,7 @@ const COMMON_ACTIONS = [
209
271
  "children"
210
272
  );
211
273
  },
212
- hidden: (props) => props.mode === "simplified"
274
+ hidden: (props) => props.mode !== "advanced"
213
275
  }
214
276
  // {
215
277
  // type: "button-action" as const,
@@ -277,6 +339,60 @@ const formHelpers = {
277
339
  }
278
340
  };
279
341
  const formComponentName = "plasmic-antd5-form";
342
+ const formTypeDescription = `
343
+ You can create form with two different behaviors:
344
+
345
+
346
+
347
+ 1. Create a new entry: The form will be created empty and it will create a new row when submitted.
348
+ 2. Update an entry: The form will be pre-filled with the row values and it will update the table entry when submitted.
349
+
350
+
351
+
352
+ For both options, you can customize later.
353
+ `;
354
+ const getConnectionDBPropRegistration = () => ({
355
+ formType: {
356
+ type: "choice",
357
+ options: [
358
+ { label: "New entry", value: 0 /* NewEntry */ },
359
+ { label: "Update entry", value: 1 /* UpdateEntry */ }
360
+ ],
361
+ displayName: "Form Type",
362
+ disableDynamicValue: true,
363
+ defaultValueHint: "Select the form type...",
364
+ description: `
365
+ You can create form with two different behaviors:
366
+
367
+
368
+
369
+ 1. Create a new entry: The form will be created empty and it will create a new row when submitted.
370
+ 2. Update an entry: The form will be pre-filled with the row values and it will update the table entry when submitted.
371
+
372
+
373
+
374
+ For both options, you can customize later.
375
+ `
376
+ },
377
+ dataSchema: {
378
+ type: "dataSourceOp",
379
+ description: "The data to generate the form",
380
+ hidden: (_ps, _ctx, { item }) => item.formType !== 0 /* NewEntry */,
381
+ displayName: "",
382
+ disableDynamicValue: true,
383
+ allowedOps: () => ["getTableSchema"],
384
+ hideCacheKey: true
385
+ },
386
+ dataEntry: {
387
+ type: "dataSourceOp",
388
+ description: "The data to generate the form",
389
+ hidden: (_ps, _ctx, { item }) => item.formType !== 1 /* UpdateEntry */,
390
+ displayName: "",
391
+ disableDynamicValue: true,
392
+ allowedOps: () => ["getOne"],
393
+ hideCacheKey: true
394
+ }
395
+ });
280
396
  function registerForm(loader) {
281
397
  utils.registerComponentHelper(loader, FormWrapper, {
282
398
  name: formComponentName,
@@ -290,13 +406,22 @@ function registerForm(loader) {
290
406
  type: "controlMode",
291
407
  defaultValue: "simplified"
292
408
  },
409
+ data: {
410
+ type: "object",
411
+ fields: getConnectionDBPropRegistration(),
412
+ hidden: (ps) => ps.mode !== "simplified" || !ps.data,
413
+ nameFunc: () => "DB Connection"
414
+ },
293
415
  formItems: {
294
416
  displayName: "Fields",
295
417
  type: "array",
296
418
  itemType: {
297
419
  type: "object",
298
420
  fields: __spreadValues({
299
- label: "string",
421
+ label: {
422
+ type: "string",
423
+ defaultValueHint: getDefaultValueHint("label")
424
+ },
300
425
  inputType: {
301
426
  type: "choice",
302
427
  options: Object.values(InputType).filter(
@@ -306,7 +431,8 @@ function registerForm(loader) {
306
431
  "Radio" /* Radio */
307
432
  ].includes(inputType)
308
433
  ),
309
- defaultValue: "Text" /* Text */
434
+ defaultValue: "Text" /* Text */,
435
+ defaultValueHint: getDefaultValueHint("inputType")
310
436
  },
311
437
  options: {
312
438
  type: "array",
@@ -375,7 +501,10 @@ function registerForm(loader) {
375
501
  displayName: "Option Type"
376
502
  }
377
503
  }, commonFormItemProps),
378
- nameFunc: (item) => item.label
504
+ nameFunc: (item) => {
505
+ var _a, _b;
506
+ return (_b = (_a = item.fieldId) != null ? _a : item.label) != null ? _b : item.name;
507
+ }
379
508
  },
380
509
  defaultValue: [
381
510
  {
@@ -389,7 +518,31 @@ function registerForm(loader) {
389
518
  inputType: "Text Area" /* TextArea */
390
519
  }
391
520
  ],
392
- hidden: (props) => props.mode !== "simplified"
521
+ hidden: (ps) => {
522
+ if (ps.mode === "advanced") {
523
+ return true;
524
+ }
525
+ if (ps.mode === "simplified") {
526
+ return false;
527
+ }
528
+ return !ps.data || !ps.formType;
529
+ },
530
+ unstable__keyFunc: (x) => x.key,
531
+ unstable__minimalValue: (ps, contextData) => {
532
+ return ps.mode === "simplified" ? ps.formItems : contextData == null ? void 0 : contextData.minimalFullLengthFields;
533
+ },
534
+ unstable__canDelete: (item, ps, ctx) => {
535
+ if (ps.mode !== "database-schema-driven") {
536
+ return true;
537
+ }
538
+ if (!(ctx == null ? void 0 : ctx.schema)) {
539
+ return false;
540
+ }
541
+ if (item.fieldId && ctx.schema.fields.some((f) => f.id === item.fieldId)) {
542
+ return false;
543
+ }
544
+ return true;
545
+ }
393
546
  },
394
547
  submitSlot: __spreadValues({
395
548
  type: "slot",
@@ -412,7 +565,7 @@ function registerForm(loader) {
412
565
  }),
413
566
  children: {
414
567
  type: "slot",
415
- hidden: (props) => props.mode === "simplified"
568
+ hidden: (props) => props.mode !== "advanced"
416
569
  },
417
570
  initialValues: {
418
571
  displayName: "Initial field values",
@@ -525,7 +678,13 @@ function registerForm(loader) {
525
678
  advanced: true
526
679
  }
527
680
  },
528
- actions: COMMON_ACTIONS,
681
+ actions: [
682
+ ...COMMON_ACTIONS,
683
+ {
684
+ type: "form-schema",
685
+ hidden: (ps) => ps.mode !== "simplified" || !!ps.data
686
+ }
687
+ ],
529
688
  states: {
530
689
  value: {
531
690
  type: "readonly",
@@ -687,10 +846,13 @@ function FormItemWrapper(props) {
687
846
  initialValue: props.initialValue,
688
847
  name: props.name
689
848
  });
849
+ const fullPath = React__default.default.useContext(PathContext).fullPath;
690
850
  const internalFormCtx = React__default.default.useContext(InternalFormInstanceContext);
691
851
  const { fireOnValuesChange, forceRemount } = internalFormCtx != null ? internalFormCtx : {};
692
852
  (_b = props.setControlContextData) == null ? void 0 : _b.call(props, {
693
- internalFormCtx
853
+ internalFormCtx,
854
+ formInstance: form,
855
+ parentFormItemPath: fullPath
694
856
  });
695
857
  React__default.default.useEffect(() => {
696
858
  if (prevPropValues.current.name !== props.name) {
@@ -720,15 +882,16 @@ function FormItemWrapper(props) {
720
882
  );
721
883
  }
722
884
  function deriveValuePropName(props) {
885
+ var _a;
723
886
  if (props.valuePropName) {
724
887
  return props.valuePropName;
725
888
  }
726
- const valueProps = React__default.default.Children.map(props.children, (child) => {
727
- var _a;
889
+ const valueProps = ((_a = React__default.default.Children.map(props.children, (child) => {
890
+ var _a2;
728
891
  if (React__default.default.isValidElement(child)) {
729
892
  const childType = child.type;
730
893
  if (childType) {
731
- const x = (_a = childType.__plasmicFormFieldMeta) == null ? void 0 : _a.valueProp;
894
+ const x = (_a2 = childType.__plasmicFormFieldMeta) == null ? void 0 : _a2.valueProp;
732
895
  if (x) {
733
896
  return x;
734
897
  }
@@ -739,7 +902,7 @@ function deriveValuePropName(props) {
739
902
  }
740
903
  }
741
904
  return void 0;
742
- }).filter((x) => !!x);
905
+ })) != null ? _a : []).filter((x) => !!x);
743
906
  if (valueProps.length > 0) {
744
907
  return valueProps[0];
745
908
  }
@@ -771,12 +934,29 @@ function FormItemForwarder(_a) {
771
934
  return i === 0 && React.isValidElement(child) ? React.cloneElement(child, newProps) : child;
772
935
  });
773
936
  }
937
+ function getDefaultValueHint(field) {
938
+ return (_props, contextData, { item }) => {
939
+ if (!contextData || !("mergedFields" in contextData)) {
940
+ return void 0;
941
+ }
942
+ if (item == null ? void 0 : item.fieldId) {
943
+ const fieldSetting = contextData.mergedFields.find(
944
+ (f) => f.fieldId === item.fieldId
945
+ );
946
+ return fieldSetting == null ? void 0 : fieldSetting[field];
947
+ }
948
+ return void 0;
949
+ };
950
+ }
774
951
  const commonFormItemProps = {
775
952
  name: {
776
- type: "string"
953
+ type: "string",
954
+ required: true,
955
+ defaultValueHint: getDefaultValueHint("name")
777
956
  },
778
957
  initialValue: {
779
- type: "string"
958
+ type: "string",
959
+ defaultValueHint: getDefaultValueHint("initialValue")
780
960
  },
781
961
  rules: {
782
962
  displayName: "Validation rules",
@@ -819,7 +999,8 @@ const commonFormItemProps = {
819
999
  },
820
1000
  hidden: {
821
1001
  type: "boolean",
822
- advanced: true
1002
+ advanced: true,
1003
+ defaultValueHint: getDefaultValueHint("hidden")
823
1004
  },
824
1005
  validateTrigger: {
825
1006
  type: "choice",
@@ -1190,6 +1371,7 @@ function registerFormList(loader) {
1190
1371
  exports.FormGroup = FormGroup;
1191
1372
  exports.FormItemWrapper = FormItemWrapper;
1192
1373
  exports.FormListWrapper = FormListWrapper;
1374
+ exports.FormType = FormType;
1193
1375
  exports.FormWrapper = FormWrapper;
1194
1376
  exports.InputType = InputType;
1195
1377
  exports.formComponentName = formComponentName;
@@ -1197,6 +1379,7 @@ exports.formGroupComponentName = formGroupComponentName;
1197
1379
  exports.formHelpers = formHelpers;
1198
1380
  exports.formItemComponentName = formItemComponentName;
1199
1381
  exports.formListComponentName = formListComponentName;
1382
+ exports.formTypeDescription = formTypeDescription;
1200
1383
  exports.registerForm = registerForm;
1201
1384
  exports.registerFormGroup = registerFormGroup;
1202
1385
  exports.registerFormItem = registerFormItem;