@plasmicpkgs/antd5 0.0.84 → 0.0.86

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/dist/index.d.ts CHANGED
@@ -2,7 +2,8 @@ import { InputType } from "./registerForm";
2
2
  import { Registerable } from "./utils";
3
3
  export declare function registerAll(loader?: Registerable): void;
4
4
  export { buttonComponentName } from "./registerButton";
5
- export { formComponentName, formGroupComponentName, formItemComponentName, formListComponentName, InputType, } from "./registerForm";
5
+ export { FormType, formTypeDescription, formComponentName, formGroupComponentName, formItemComponentName, formListComponentName, InputType, } from "./registerForm";
6
+ export { registerForm, registerFormGroup, registerFormItem, } from "./registerForm";
6
7
  export type { SimplifiedFormItemsProp } from "./registerForm";
7
8
  export declare const componentNameToInputType: {
8
9
  "plasmic-antd5-input": InputType;
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ var query = require('@plasmicapp/query');
10
10
  var enUS = require('antd/lib/locale/en_US.js');
11
11
  var equal = require('fast-deep-equal');
12
12
  var classNames = require('classnames');
13
+ var dataSources = require('@plasmicapp/data-sources');
13
14
  var dayjs = require('dayjs');
14
15
 
15
16
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -2422,12 +2423,17 @@ var InputType = /* @__PURE__ */ ((InputType2) => {
2422
2423
  InputType2["DatePicker"] = "DatePicker";
2423
2424
  return InputType2;
2424
2425
  })(InputType || {});
2426
+ var FormType = /* @__PURE__ */ ((FormType2) => {
2427
+ FormType2[FormType2["NewEntry"] = 0] = "NewEntry";
2428
+ FormType2[FormType2["UpdateEntry"] = 1] = "UpdateEntry";
2429
+ return FormType2;
2430
+ })(FormType || {});
2425
2431
  const PathContext = React__default.default.createContext({ relativePath: [], fullPath: [] });
2426
2432
  const InternalFormInstanceContext = React__default.default.createContext(void 0);
2427
2433
  const FormLayoutContext = React__default.default.createContext(void 0);
2428
2434
  const Internal = React__default.default.forwardRef(
2429
2435
  (props, ref) => {
2430
- var _b, _c, _d, _e, _f;
2436
+ var _b, _c, _d, _e;
2431
2437
  const [form] = antd.Form.useForm();
2432
2438
  const values = form.getFieldsValue(true);
2433
2439
  const lastValue = React__default.default.useRef(values);
@@ -2472,7 +2478,6 @@ const Internal = React__default.default.forwardRef(
2472
2478
  },
2473
2479
  [props.layout, (_c = props.labelCol) == null ? void 0 : _c.span]
2474
2480
  );
2475
- (_d = props.setControlContextData) == null ? void 0 : _d.call(props, { formInstance: form });
2476
2481
  React__default.default.useImperativeHandle(ref, () => ({
2477
2482
  formInstance: form,
2478
2483
  setFieldsValue: (newValues) => {
@@ -2513,8 +2518,8 @@ const Internal = React__default.default.forwardRef(
2513
2518
  extendedOnValuesChange == null ? void 0 : extendedOnValuesChange(args[1]);
2514
2519
  },
2515
2520
  form,
2516
- labelCol: ((_e = props.labelCol) == null ? void 0 : _e.horizontalOnly) && props.layout !== "horizontal" ? void 0 : props.labelCol,
2517
- wrapperCol: ((_f = props.wrapperCol) == null ? void 0 : _f.horizontalOnly) && props.layout !== "horizontal" ? void 0 : props.wrapperCol
2521
+ labelCol: ((_d = props.labelCol) == null ? void 0 : _d.horizontalOnly) && props.layout !== "horizontal" ? void 0 : props.labelCol,
2522
+ wrapperCol: ((_e = props.wrapperCol) == null ? void 0 : _e.horizontalOnly) && props.layout !== "horizontal" ? void 0 : props.wrapperCol
2518
2523
  }),
2519
2524
  /* @__PURE__ */ React__default.default.createElement("style", null, `
2520
2525
  .ant-form-item-explain + div, .ant-form-item-margin-offset {
@@ -2526,6 +2531,51 @@ const Internal = React__default.default.forwardRef(
2526
2531
  );
2527
2532
  }
2528
2533
  );
2534
+ function useFormItemDefinitions(rawData, props, formRef) {
2535
+ const { mode, formItems, setControlContextData } = props;
2536
+ return React__default.default.useMemo(() => {
2537
+ if (mode !== "simplified" || !formRef || !rawData || rawData.error) {
2538
+ return void 0;
2539
+ }
2540
+ const data = dataSources.normalizeData(rawData);
2541
+ const schema = data == null ? void 0 : data.schema;
2542
+ if (!data || !schema || !data.data) {
2543
+ return void 0;
2544
+ }
2545
+ const row = data.data.length > 0 ? data.data[0] : void 0;
2546
+ const { mergedFields, minimalFullLengthFields } = dataSources.deriveFieldConfigs(
2547
+ formItems != null ? formItems : [],
2548
+ schema,
2549
+ (field) => __spreadValues$4({
2550
+ inputType: "Text" /* Text */
2551
+ }, field && {
2552
+ key: field.id,
2553
+ fieldId: field.id,
2554
+ label: field.label,
2555
+ name: field.id,
2556
+ inputType: field.type === "string" ? "Text" /* Text */ : field.type === "number" ? "Number" /* Number */ : field.type === "boolean" ? "Checkbox" /* Checkbox */ : "Text" /* Text */,
2557
+ //missing date and date-time
2558
+ initialValue: row ? row[field.id] : void 0,
2559
+ hidden: field.primaryKey
2560
+ })
2561
+ );
2562
+ setControlContextData == null ? void 0 : setControlContextData({
2563
+ schema: data.schema,
2564
+ minimalFullLengthFields,
2565
+ mergedFields,
2566
+ formInstance: formRef.formInstance
2567
+ });
2568
+ return mergedFields;
2569
+ }, [mode, setControlContextData, formItems, rawData, formRef]);
2570
+ }
2571
+ const useRawData = (connection) => {
2572
+ const rawDataSchema = dataSources.usePlasmicDataOp(connection == null ? void 0 : connection.dataSchema);
2573
+ const rawDataEntry = dataSources.usePlasmicDataOp(connection == null ? void 0 : connection.dataEntry);
2574
+ if (!connection) {
2575
+ return void 0;
2576
+ }
2577
+ return 0 /* NewEntry */ === connection.formType ? rawDataSchema : rawDataEntry;
2578
+ };
2529
2579
  const FormWrapper = React__default.default.forwardRef(
2530
2580
  (props, ref) => {
2531
2581
  const [remountKey, setRemountKey] = React__default.default.useState(0);
@@ -2540,12 +2590,24 @@ const FormWrapper = React__default.default.forwardRef(
2540
2590
  ref,
2541
2591
  () => wrapperRef.current ? __spreadValues$4({}, wrapperRef.current) : {}
2542
2592
  );
2593
+ const rawData = useRawData(props.data);
2594
+ const formItemDefinitions = useFormItemDefinitions(
2595
+ rawData,
2596
+ props,
2597
+ wrapperRef.current
2598
+ );
2599
+ const _a = props, { formItems } = _a, rest = __objRest$3(_a, ["formItems"]);
2600
+ const actualFormItems = props.mode === "simplified" && formItemDefinitions ? formItemDefinitions : formItems;
2601
+ if (props.mode === "simplified" && rawData && "error" in rawData) {
2602
+ return /* @__PURE__ */ React__default.default.createElement("div", null, "Error when fetching data: ", rawData.error.message);
2603
+ }
2543
2604
  return /* @__PURE__ */ React__default.default.createElement(
2544
2605
  Internal,
2545
2606
  __spreadProps$3(__spreadValues$4({
2546
2607
  key: remountKey
2547
- }, props), {
2608
+ }, rest), {
2548
2609
  setRemountKey,
2610
+ formItems: actualFormItems,
2549
2611
  ref: wrapperRef
2550
2612
  })
2551
2613
  );
@@ -2564,7 +2626,7 @@ const COMMON_ACTIONS = [
2564
2626
  "children"
2565
2627
  );
2566
2628
  },
2567
- hidden: (props) => props.mode === "simplified"
2629
+ hidden: (props) => props.mode !== "advanced"
2568
2630
  }
2569
2631
  // {
2570
2632
  // type: "button-action" as const,
@@ -2632,6 +2694,60 @@ const formHelpers = {
2632
2694
  }
2633
2695
  };
2634
2696
  const formComponentName = "plasmic-antd5-form";
2697
+ const formTypeDescription = `
2698
+ You can create form with two different behaviors:
2699
+
2700
+
2701
+
2702
+ 1. Create a new entry: The form will be created empty and it will create a new row when submitted.
2703
+ 2. Update an entry: The form will be pre-filled with the row values and it will update the table entry when submitted.
2704
+
2705
+
2706
+
2707
+ For both options, you can customize later.
2708
+ `;
2709
+ const getConnectionDBPropRegistration = () => ({
2710
+ formType: {
2711
+ type: "choice",
2712
+ options: [
2713
+ { label: "New entry", value: 0 /* NewEntry */ },
2714
+ { label: "Update entry", value: 1 /* UpdateEntry */ }
2715
+ ],
2716
+ displayName: "Form Type",
2717
+ disableDynamicValue: true,
2718
+ defaultValueHint: "Select the form type...",
2719
+ description: `
2720
+ You can create form with two different behaviors:
2721
+
2722
+
2723
+
2724
+ 1. Create a new entry: The form will be created empty and it will create a new row when submitted.
2725
+ 2. Update an entry: The form will be pre-filled with the row values and it will update the table entry when submitted.
2726
+
2727
+
2728
+
2729
+ For both options, you can customize later.
2730
+ `
2731
+ },
2732
+ dataSchema: {
2733
+ type: "dataSourceOp",
2734
+ description: "The data to generate the form",
2735
+ hidden: (_ps, _ctx, { item }) => item.formType !== 0 /* NewEntry */,
2736
+ displayName: "",
2737
+ disableDynamicValue: true,
2738
+ allowedOps: () => ["getTableSchema"],
2739
+ hideCacheKey: true
2740
+ },
2741
+ dataEntry: {
2742
+ type: "dataSourceOp",
2743
+ description: "The data to generate the form",
2744
+ hidden: (_ps, _ctx, { item }) => item.formType !== 1 /* UpdateEntry */,
2745
+ displayName: "",
2746
+ disableDynamicValue: true,
2747
+ allowedOps: () => ["getOne"],
2748
+ hideCacheKey: true
2749
+ }
2750
+ });
2635
2751
  function registerForm(loader) {
2636
2752
  registerComponentHelper(loader, FormWrapper, {
2637
2753
  name: formComponentName,
@@ -2645,13 +2761,22 @@ function registerForm(loader) {
2645
2761
  type: "controlMode",
2646
2762
  defaultValue: "simplified"
2647
2763
  },
2764
+ data: {
2765
+ type: "object",
2766
+ fields: getConnectionDBPropRegistration(),
2767
+ hidden: (ps) => ps.mode !== "simplified" || !ps.data,
2768
+ nameFunc: () => "DB Connection"
2769
+ },
2648
2770
  formItems: {
2649
2771
  displayName: "Fields",
2650
2772
  type: "array",
2651
2773
  itemType: {
2652
2774
  type: "object",
2653
2775
  fields: __spreadValues$4({
2654
- label: "string",
2776
+ label: {
2777
+ type: "string",
2778
+ defaultValueHint: getDefaultValueHint("label")
2779
+ },
2655
2780
  inputType: {
2656
2781
  type: "choice",
2657
2782
  options: Object.values(InputType).filter(
@@ -2661,7 +2786,8 @@ function registerForm(loader) {
2661
2786
  "Radio" /* Radio */
2662
2787
  ].includes(inputType)
2663
2788
  ),
2664
- defaultValue: "Text" /* Text */
2789
+ defaultValue: "Text" /* Text */,
2790
+ defaultValueHint: getDefaultValueHint("inputType")
2665
2791
  },
2666
2792
  options: {
2667
2793
  type: "array",
@@ -2730,7 +2856,10 @@ function registerForm(loader) {
2730
2856
  displayName: "Option Type"
2731
2857
  }
2732
2858
  }, commonFormItemProps),
2733
- nameFunc: (item) => item.label
2859
+ nameFunc: (item) => {
2860
+ var _a, _b;
2861
+ return (_b = (_a = item.fieldId) != null ? _a : item.label) != null ? _b : item.name;
2862
+ }
2734
2863
  },
2735
2864
  defaultValue: [
2736
2865
  {
@@ -2744,7 +2873,31 @@ function registerForm(loader) {
2744
2873
  inputType: "Text Area" /* TextArea */
2745
2874
  }
2746
2875
  ],
2747
- hidden: (props) => props.mode !== "simplified"
2876
+ hidden: (ps) => {
2877
+ if (ps.mode === "advanced") {
2878
+ return true;
2879
+ }
2880
+ if (ps.mode === "simplified") {
2881
+ return false;
2882
+ }
2883
+ return !ps.data || !ps.formType;
2884
+ },
2885
+ unstable__keyFunc: (x) => x.key,
2886
+ unstable__minimalValue: (ps, contextData) => {
2887
+ return ps.mode === "simplified" ? ps.formItems : contextData == null ? void 0 : contextData.minimalFullLengthFields;
2888
+ },
2889
+ unstable__canDelete: (item, ps, ctx) => {
2890
+ if (ps.mode !== "database-schema-driven") {
2891
+ return true;
2892
+ }
2893
+ if (!(ctx == null ? void 0 : ctx.schema)) {
2894
+ return false;
2895
+ }
2896
+ if (item.fieldId && ctx.schema.fields.some((f) => f.id === item.fieldId)) {
2897
+ return false;
2898
+ }
2899
+ return true;
2900
+ }
2748
2901
  },
2749
2902
  submitSlot: __spreadValues$4({
2750
2903
  type: "slot",
@@ -2767,7 +2920,7 @@ function registerForm(loader) {
2767
2920
  }),
2768
2921
  children: {
2769
2922
  type: "slot",
2770
- hidden: (props) => props.mode === "simplified"
2923
+ hidden: (props) => props.mode !== "advanced"
2771
2924
  },
2772
2925
  initialValues: {
2773
2926
  displayName: "Initial field values",
@@ -2880,7 +3033,13 @@ function registerForm(loader) {
2880
3033
  advanced: true
2881
3034
  }
2882
3035
  },
2883
- actions: COMMON_ACTIONS,
3036
+ actions: [
3037
+ ...COMMON_ACTIONS,
3038
+ {
3039
+ type: "form-schema",
3040
+ hidden: (ps) => ps.mode !== "simplified" || !!ps.data
3041
+ }
3042
+ ],
2884
3043
  states: {
2885
3044
  value: {
2886
3045
  type: "readonly",
@@ -3042,10 +3201,13 @@ function FormItemWrapper(props) {
3042
3201
  initialValue: props.initialValue,
3043
3202
  name: props.name
3044
3203
  });
3204
+ const fullPath = React__default.default.useContext(PathContext).fullPath;
3045
3205
  const internalFormCtx = React__default.default.useContext(InternalFormInstanceContext);
3046
3206
  const { fireOnValuesChange, forceRemount } = internalFormCtx != null ? internalFormCtx : {};
3047
3207
  (_b = props.setControlContextData) == null ? void 0 : _b.call(props, {
3048
- internalFormCtx
3208
+ internalFormCtx,
3209
+ formInstance: form,
3210
+ parentFormItemPath: fullPath
3049
3211
  });
3050
3212
  React__default.default.useEffect(() => {
3051
3213
  if (prevPropValues.current.name !== props.name) {
@@ -3075,15 +3237,16 @@ function FormItemWrapper(props) {
3075
3237
  );
3076
3238
  }
3077
3239
  function deriveValuePropName(props) {
3240
+ var _a;
3078
3241
  if (props.valuePropName) {
3079
3242
  return props.valuePropName;
3080
3243
  }
3081
- const valueProps = React__default.default.Children.map(props.children, (child) => {
3082
- var _a;
3244
+ const valueProps = ((_a = React__default.default.Children.map(props.children, (child) => {
3245
+ var _a2;
3083
3246
  if (React__default.default.isValidElement(child)) {
3084
3247
  const childType = child.type;
3085
3248
  if (childType) {
3086
- const x = (_a = childType.__plasmicFormFieldMeta) == null ? void 0 : _a.valueProp;
3249
+ const x = (_a2 = childType.__plasmicFormFieldMeta) == null ? void 0 : _a2.valueProp;
3087
3250
  if (x) {
3088
3251
  return x;
3089
3252
  }
@@ -3094,7 +3257,7 @@ function deriveValuePropName(props) {
3094
3257
  }
3095
3258
  }
3096
3259
  return void 0;
3097
- }).filter((x) => !!x);
3260
+ })) != null ? _a : []).filter((x) => !!x);
3098
3261
  if (valueProps.length > 0) {
3099
3262
  return valueProps[0];
3100
3263
  }
@@ -3126,12 +3289,29 @@ function FormItemForwarder(_a) {
3126
3289
  return i === 0 && React.isValidElement(child) ? React.cloneElement(child, newProps) : child;
3127
3290
  });
3128
3291
  }
3292
+ function getDefaultValueHint(field) {
3293
+ return (_props, contextData, { item }) => {
3294
+ if (!contextData || !("mergedFields" in contextData)) {
3295
+ return void 0;
3296
+ }
3297
+ if (item == null ? void 0 : item.fieldId) {
3298
+ const fieldSetting = contextData.mergedFields.find(
3299
+ (f) => f.fieldId === item.fieldId
3300
+ );
3301
+ return fieldSetting == null ? void 0 : fieldSetting[field];
3302
+ }
3303
+ return void 0;
3304
+ };
3305
+ }
3129
3306
  const commonFormItemProps = {
3130
3307
  name: {
3131
- type: "string"
3308
+ type: "string",
3309
+ required: true,
3310
+ defaultValueHint: getDefaultValueHint("name")
3132
3311
  },
3133
3312
  initialValue: {
3134
- type: "string"
3313
+ type: "string",
3314
+ defaultValueHint: getDefaultValueHint("initialValue")
3135
3315
  },
3136
3316
  rules: {
3137
3317
  displayName: "Validation rules",
@@ -3174,7 +3354,8 @@ const commonFormItemProps = {
3174
3354
  },
3175
3355
  hidden: {
3176
3356
  type: "boolean",
3177
- advanced: true
3357
+ advanced: true,
3358
+ defaultValueHint: getDefaultValueHint("hidden")
3178
3359
  },
3179
3360
  validateTrigger: {
3180
3361
  type: "choice",
@@ -4332,6 +4513,7 @@ const inputTypeToComponentName = Object.fromEntries(
4332
4513
  Object.entries(componentNameToInputType).map((kv) => kv.reverse())
4333
4514
  );
4334
4515
 
4516
+ exports.FormType = FormType;
4335
4517
  exports.InputType = InputType;
4336
4518
  exports.buttonComponentName = buttonComponentName;
4337
4519
  exports.componentNameToInputType = componentNameToInputType;
@@ -4339,7 +4521,11 @@ exports.formComponentName = formComponentName;
4339
4521
  exports.formGroupComponentName = formGroupComponentName;
4340
4522
  exports.formItemComponentName = formItemComponentName;
4341
4523
  exports.formListComponentName = formListComponentName;
4524
+ exports.formTypeDescription = formTypeDescription;
4342
4525
  exports.inputTypeToComponentName = inputTypeToComponentName;
4343
4526
  exports.registerAll = registerAll;
4527
+ exports.registerForm = registerForm;
4528
+ exports.registerFormGroup = registerFormGroup;
4529
+ exports.registerFormItem = registerFormItem;
4344
4530
  exports.useFormInstanceMaybe = useFormInstanceMaybe;
4345
4531
  //# sourceMappingURL=index.js.map