@hybridly/vue 0.1.0-alpha.3 → 0.1.0-alpha.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/dist/index.cjs CHANGED
@@ -512,15 +512,10 @@ function toReactive(objectRef) {
512
512
  function useProperties() {
513
513
  return vue.readonly(toReactive(vue.computed(() => state.context.value?.view.properties)));
514
514
  }
515
- function useTypedProperty(path, fallback) {
516
- return vue.computed(
517
- () => path.split(".").reduce((o, i) => o[i], state.context.value?.view.properties) ?? fallback
518
- );
519
- }
520
- function useProperty(path, fallback) {
521
- return vue.computed(
522
- () => path.split(".").reduce((o, i) => o[i], state.context.value?.view.properties) ?? fallback
523
- );
515
+ function useProperty(path) {
516
+ return vue.computed(() => {
517
+ return path.split(".").reduce((o, i) => o?.[i], state.context.value?.view.properties);
518
+ });
524
519
  }
525
520
 
526
521
  function useContext() {
@@ -562,6 +557,14 @@ function useForm(options) {
562
557
  Reflect.set(fields, key, safeClone(Reflect.get(initial, key)));
563
558
  });
564
559
  }
560
+ function clear(...keys) {
561
+ if (keys.length === 0) {
562
+ keys = Object.keys(fields);
563
+ }
564
+ keys.forEach((key) => {
565
+ delete fields[key];
566
+ });
567
+ }
565
568
  function submit(optionsOverrides) {
566
569
  const url = typeof options.url === "function" ? options.url() : options.url;
567
570
  const data = typeof options.transform === "function" ? options.transform?.(fields) : fields;
@@ -599,7 +602,9 @@ function useForm(options) {
599
602
  },
600
603
  success: (payload, context) => {
601
604
  clearErrors();
602
- setInitial(fields);
605
+ if (options?.updateInitials) {
606
+ setInitial(fields);
607
+ }
603
608
  if (options?.reset !== false) {
604
609
  reset();
605
610
  }
@@ -634,7 +639,10 @@ function useForm(options) {
634
639
  delete errors.value[key];
635
640
  }
636
641
  function setErrors(incoming) {
637
- errors.value = incoming;
642
+ clearErrors();
643
+ Object.entries(incoming).forEach(([key, value]) => {
644
+ errors.value[key] = value;
645
+ });
638
646
  }
639
647
  function abort() {
640
648
  core.router.abort();
@@ -650,6 +658,7 @@ function useForm(options) {
650
658
  }, { deep: true, immediate: true });
651
659
  return vue.reactive({
652
660
  reset,
661
+ clear,
653
662
  fields,
654
663
  abort,
655
664
  setErrors,
@@ -772,4 +781,3 @@ exports.useHistoryState = useHistoryState;
772
781
  exports.usePaginator = usePaginator;
773
782
  exports.useProperties = useProperties;
774
783
  exports.useProperty = useProperty;
775
- exports.useTypedProperty = useTypedProperty;
package/dist/index.d.ts CHANGED
@@ -181,16 +181,11 @@ declare const RouterLink: vue.DefineComponent<{
181
181
 
182
182
  /** Accesses all current properties. */
183
183
  declare function useProperties<T extends object, Global extends GlobalHybridlyProperties>(): vue.DeepReadonly<vue.UnwrapNestedRefs<T & Global>>;
184
- /**
185
- * Accesses a property with the given type.
186
- * @experimental Workaround for not being able to type `useProperty`, might be removed in the future.
187
- */
188
- declare function useTypedProperty<T>(path: string, fallback?: T): ComputedRef<T>;
189
184
  /** Accesses a property with a dot notation. */
190
- declare function useProperty<T = GlobalHybridlyProperties, P extends Path<T> = Path<T>, Fallback extends PathValue<T, P> = PathValue<T, P>>(path: [P] extends [never] ? string : P, fallback?: Fallback): ComputedRef<[PathValue<T, P>] extends [never] ? Fallback : PathValue<T, P>>;
191
- type PathImpl<T, K extends keyof T> = K extends string ? T[K] extends Record<string, any> ? T[K] extends ArrayLike<any> ? K | `${K}.${PathImpl<T[K], Exclude<keyof T[K], keyof any[]>>}` : K | `${K}.${PathImpl<T[K], keyof T[K]>}` : K : never;
185
+ declare function useProperty<F = never, T = GlobalHybridlyProperties, P extends Path<T> = Path<T>>(path: [F] extends [never] ? [P] extends [never] ? string : P : string): ComputedRef<[F] extends [never] ? [PathValue<T, P>] extends [never] ? never : PathValue<T, P> : F>;
186
+ type PathImpl<T, K extends keyof T> = K extends string ? T[K] extends undefined ? undefined : NonNullable<T[K]> extends Record<string, any> ? NonNullable<T[K]> extends ArrayLike<any> ? K | `${K}.${PathImpl<NonNullable<T[K]>, Exclude<keyof NonNullable<T[K]>, keyof any[]>>}` : K | `${K}.${PathImpl<NonNullable<T[K]>, keyof NonNullable<T[K]>>}` : K : never;
192
187
  type Path<T> = PathImpl<T, keyof T> | keyof T;
193
- type PathValue<T, P extends Path<T>> = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? Rest extends Path<T[K]> ? PathValue<T[K], Rest> : never : never : P extends keyof T ? T[P] : never;
188
+ type PathValue<T, P extends Path<T>> = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? T[K] extends undefined ? undefined : Rest extends Path<T[K]> ? PathValue<T[K], Rest> : Rest extends Path<NonNullable<T[K]>> ? PathValue<NonNullable<T[K]>, Rest> | undefined : never : never : P extends keyof T ? T[P] extends undefined ? undefined : T[P] : never;
194
189
 
195
190
  type MaybePromise<T> = T | Promise<T>;
196
191
 
@@ -485,12 +480,28 @@ interface FormOptions<T extends Fields> extends Omit<HybridRequestOptions$1, 'da
485
480
  fields: T;
486
481
  url?: UrlResolvable$1 | (() => UrlResolvable$1);
487
482
  key?: string | false;
483
+ /**
484
+ * Defines the delay after which the `recentlySuccessful` and `recentlyFailed` variables are reset to `false`.
485
+ */
488
486
  timeout?: number;
487
+ /**
488
+ * Resets the fields of the form to their initial value after a successful submission.
489
+ * @default true
490
+ */
489
491
  reset?: boolean;
492
+ /**
493
+ * Updates the initial values from the form after a successful submission.
494
+ * @default false
495
+ */
496
+ updateInitials?: boolean;
497
+ /**
498
+ * Callback executed before the form submission for transforming the fields.
499
+ */
490
500
  transform?: (fields: T) => Fields;
491
501
  }
492
502
  declare function useForm<T extends Fields = Fields>(options: FormOptions<T>): {
493
503
  reset: (...keys: (keyof T)[]) => void;
504
+ clear: (...keys: (keyof T)[]) => void;
494
505
  fields: vue.UnwrapRef<vue.UnwrapNestedRefs<T>>;
495
506
  abort: () => void;
496
507
  setErrors: (incoming: Record<string, string>) => void;
@@ -645,4 +656,4 @@ declare function useDialog(): {
645
656
  properties: vue.ComputedRef<Properties | undefined>;
646
657
  };
647
658
 
648
- export { Layout, RouterLink, defineLayout, defineLayoutProperties, initializeHybridly, registerHook, resolvePageComponent, useBackForward, useContext, useDialog, useForm, useHistoryState, usePaginator, useProperties, useProperty, useTypedProperty };
659
+ export { Layout, RouterLink, defineLayout, defineLayoutProperties, initializeHybridly, registerHook, resolvePageComponent, useBackForward, useContext, useDialog, useForm, useHistoryState, usePaginator, useProperties, useProperty };
package/dist/index.mjs CHANGED
@@ -504,15 +504,10 @@ function toReactive(objectRef) {
504
504
  function useProperties() {
505
505
  return readonly(toReactive(computed(() => state.context.value?.view.properties)));
506
506
  }
507
- function useTypedProperty(path, fallback) {
508
- return computed(
509
- () => path.split(".").reduce((o, i) => o[i], state.context.value?.view.properties) ?? fallback
510
- );
511
- }
512
- function useProperty(path, fallback) {
513
- return computed(
514
- () => path.split(".").reduce((o, i) => o[i], state.context.value?.view.properties) ?? fallback
515
- );
507
+ function useProperty(path) {
508
+ return computed(() => {
509
+ return path.split(".").reduce((o, i) => o?.[i], state.context.value?.view.properties);
510
+ });
516
511
  }
517
512
 
518
513
  function useContext() {
@@ -554,6 +549,14 @@ function useForm(options) {
554
549
  Reflect.set(fields, key, safeClone(Reflect.get(initial, key)));
555
550
  });
556
551
  }
552
+ function clear(...keys) {
553
+ if (keys.length === 0) {
554
+ keys = Object.keys(fields);
555
+ }
556
+ keys.forEach((key) => {
557
+ delete fields[key];
558
+ });
559
+ }
557
560
  function submit(optionsOverrides) {
558
561
  const url = typeof options.url === "function" ? options.url() : options.url;
559
562
  const data = typeof options.transform === "function" ? options.transform?.(fields) : fields;
@@ -591,7 +594,9 @@ function useForm(options) {
591
594
  },
592
595
  success: (payload, context) => {
593
596
  clearErrors();
594
- setInitial(fields);
597
+ if (options?.updateInitials) {
598
+ setInitial(fields);
599
+ }
595
600
  if (options?.reset !== false) {
596
601
  reset();
597
602
  }
@@ -626,7 +631,10 @@ function useForm(options) {
626
631
  delete errors.value[key];
627
632
  }
628
633
  function setErrors(incoming) {
629
- errors.value = incoming;
634
+ clearErrors();
635
+ Object.entries(incoming).forEach(([key, value]) => {
636
+ errors.value[key] = value;
637
+ });
630
638
  }
631
639
  function abort() {
632
640
  router.abort();
@@ -642,6 +650,7 @@ function useForm(options) {
642
650
  }, { deep: true, immediate: true });
643
651
  return reactive({
644
652
  reset,
653
+ clear,
645
654
  fields,
646
655
  abort,
647
656
  setErrors,
@@ -747,4 +756,4 @@ function useDialog() {
747
756
  };
748
757
  }
749
758
 
750
- export { RouterLink, defineLayout, defineLayoutProperties, initializeHybridly, registerHook, resolvePageComponent, useBackForward, useContext, useDialog, useForm, useHistoryState, usePaginator, useProperties, useProperty, useTypedProperty };
759
+ export { RouterLink, defineLayout, defineLayoutProperties, initializeHybridly, registerHook, resolvePageComponent, useBackForward, useContext, useDialog, useForm, useHistoryState, usePaginator, useProperties, useProperty };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hybridly/vue",
3
- "version": "0.1.0-alpha.3",
3
+ "version": "0.1.0-alpha.4",
4
4
  "description": "Vue adapter for Hybridly",
5
5
  "keywords": [
6
6
  "hybridly",
@@ -43,10 +43,10 @@
43
43
  "lodash.isequal": "^4.5.0",
44
44
  "nprogress": "^0.2.0",
45
45
  "qs": "^6.11.0",
46
- "@hybridly/config": "0.1.0-alpha.3",
47
- "@hybridly/core": "0.1.0-alpha.3",
48
- "@hybridly/progress-plugin": "0.1.0-alpha.3",
49
- "@hybridly/utils": "0.1.0-alpha.3"
46
+ "@hybridly/config": "0.1.0-alpha.4",
47
+ "@hybridly/core": "0.1.0-alpha.4",
48
+ "@hybridly/progress-plugin": "0.1.0-alpha.4",
49
+ "@hybridly/utils": "0.1.0-alpha.4"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/lodash": "^4.14.191",