@picked/svelte 0.0.1-dev.1768358359956 → 0.0.1-dev.1768970669177

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,10 +1,18 @@
1
1
  import { FieldValue, ValidationError } from '@picked/client';
2
2
  import { EventCallable, Store } from 'effector';
3
- type ExtractFieldIds<TFieldIds extends readonly string[]> = TFieldIds[number];
4
3
  interface GetFormOptions<TFieldIds extends readonly string[]> {
5
4
  id: string;
6
5
  title: string;
7
6
  fieldIds: TFieldIds;
7
+ onSuccess?: never;
8
+ onError?: never;
9
+ }
10
+ interface GetFormOptionsLegacy {
11
+ id: string;
12
+ title: string;
13
+ fieldIds?: never;
14
+ onSuccess?: () => void | Promise<void>;
15
+ onError?: (errors: ValidationError[]) => void | Promise<void>;
8
16
  }
9
17
  type SuccessCallback<TFieldIds extends string = string> = (options: {
10
18
  data: Record<TFieldIds, FieldValue>;
@@ -15,7 +23,7 @@ type SubmitCallback<TFieldIds extends string = string> = (data: Record<TFieldIds
15
23
  type SubmitEventWithCurrentTarget = SubmitEvent & {
16
24
  currentTarget: EventTarget & HTMLFormElement;
17
25
  };
18
- export declare function getForm<const TFieldIds extends readonly string[]>({ id: formId, title, fieldIds, }: GetFormOptions<TFieldIds>): {
26
+ export declare function getForm<const TFieldIds extends readonly string[]>(options: GetFormOptions<TFieldIds> | GetFormOptionsLegacy): {
19
27
  form: {
20
28
  onsubmit: EventCallable<SubmitEventWithCurrentTarget>;
21
29
  };
@@ -170,8 +178,8 @@ export declare function getForm<const TFieldIds extends readonly string[]>({ id:
170
178
  doNotStoreValue?: boolean;
171
179
  };
172
180
  })[]>;
173
- onFormSubmit: (callback: SubmitCallback<ExtractFieldIds<TFieldIds>>) => void;
174
- onFormSuccess: (callback: SuccessCallback<ExtractFieldIds<TFieldIds>>) => void;
181
+ onFormSubmit: (callback: SubmitCallback<string>) => void;
182
+ onFormSuccess: (callback: SuccessCallback<string>) => void;
175
183
  onFormError: (callback: ErrorCallback) => void;
176
184
  isFormSubmitting: import('effector').StoreWritable<boolean>;
177
185
  };
package/dist/form/form.js CHANGED
@@ -1 +1,21 @@
1
- import*as W from"svelte/internal/client";import{getStepFields as $}from"@picked/client";import{createStore as y,combine as j,attach as _,createEffect as d,sample as n,createEvent as x,split as z}from"effector";import{getPicked as G,getExperience as H}from"../hooks/client.js";import"svelte";import"svelte/store";import"@picked/analytics";function A(g){let u=[];return{executeFx:d(async(...a)=>await g(u,...a)),register:a=>{W.user_effect(()=>(u.push(a),()=>{u=u.filter(p=>p!==a)}))}}}function U({id:g,title:u,fieldIds:w}){const l=G();let a=null,p,b=null;try{a=H(),p=a.currentStepStore,b=a.goToNextStep}catch{p=y(null)}const h=y([]),V=j({fields:h,responses:l.responses.listStore},({fields:e,responses:t})=>e.map(o=>{var c;const f=(c=t.find(r=>r.fieldId===o.id))==null?void 0:c.value;return{...o,value:f}})),N=_({source:l.fields.listStore,effect:d(async e=>{const t=e.filter(o=>w.some(f=>f===o.id));return await $(l,t)})});n({clock:N.doneData,target:h}),N();const v=x(),P=x(),B=x(),I=x(),F=x(),S=y(!1),O=y(!1);O.on(I,()=>!0).on(v,()=>!1);const k=A(async(e,t)=>{await Promise.all(e.map(o=>Promise.resolve(o(t))))}),E=A(async(e,t)=>{await Promise.all(e.map(o=>Promise.resolve(o(t))))}),D=A(async(e,t)=>{let o=t;for(const f of e){const c=await Promise.resolve(f(o));c!==void 0&&(o=c)}return o}),q=d(async e=>{const t=await l.responses.commit(e);return{data:e,errors:t.errors}}),T=_({source:{step:p},mapParams:(e,t)=>({step:t.step,data:e.data}),effect:d(async({step:e,data:t})=>(a&&l.eventBus({type:"form_submit_success",form:{id:g,title:u},experience:a,step:e??void 0}),t))}),C=_({source:{step:p},mapParams:(e,t)=>({step:t.step,errors:e.errors}),effect:d(async({step:e,errors:t})=>(a&&(l.eventBus({type:"form_submit_error",form:{id:g,title:u},exception:{reason:t},experience:a,step:e??void 0}),t.forEach(o=>{l.eventBus({type:"field_validation_error",exception:{reason:[o]},experience:a,step:e??void 0})})),t))});return n({clock:v,fn:()=>!0,target:S}),n({clock:v,source:{step:p},fn:({step:e})=>({formId:g,title:u,step:e}),target:d(({formId:e,title:t,step:o})=>{a&&l.eventBus({type:"form_submit",form:{id:e,title:t},experience:a,step:o??void 0})})}),n({clock:v,source:{stepFields:h},fn:({stepFields:e},t)=>{t.preventDefault();const o=new FormData(t.currentTarget),f=Array.from(o.entries()),c={data:{},push(r){const i=r.questionId,s=this.data[i];s?Array.isArray(s)&&Array.isArray(r.value)?Array.isArray(s)&&Array.isArray(r.value)&&(typeof s[0]=="string"?this.data[i]=[...s,...r.value].map(String):typeof s[0]=="number"?this.data[i]=[...s,...r.value].map(Number):typeof s[0]=="boolean"?this.data[i]=[...s,...r.value].map(m=>!!m):this.data[i]=r.value):this.data[i]=r.value:this.data[i]=r.value}};return e.forEach(r=>{let i=f.filter(([s])=>s===r.id);i.length===0&&(i=[[r.id,""]]),i.forEach(([,s])=>{const m=r.id;if(s===""){c.push({questionId:m,value:null});return}switch(r.type){case"text":c.push({questionId:m,value:r.multiple?[String(s)]:String(s)});break;case"number":c.push({questionId:m,value:r.multiple?[Number(s)]:Number(s)});break;case"boolean":c.push({questionId:m,value:r.multiple?[s==="true"]:s==="true"});break}})}),c.data},target:D.executeFx}),n({clock:D.executeFx.doneData,target:q}),z({source:q.doneData,match:{hasErrors:({errors:e})=>e.length>0},cases:{hasErrors:C,__:T}}),n({clock:T.doneData,target:P}),n({clock:P,fn:e=>({data:e,stopNavigation:I}),target:k.executeFx}),n({clock:k.executeFx.done,target:F}),b&&n({clock:F,source:O,filter:e=>!e,target:b}),n({clock:C.doneData,target:B}),n({clock:B,target:E.executeFx}),n({clock:[F,E.executeFx.done],fn:()=>!1,target:S}),{form:{onsubmit:v},fields:V,onFormSubmit:D.register,onFormSuccess:k.register,onFormError:E.register,isFormSubmitting:S}}export{U as getForm};
1
+ import*as X from"svelte/internal/client";import{getStepFields as Y}from"@picked/client";import{createStore as b,combine as $,attach as N,createEffect as g,sample as n,createEvent as h,split as j}from"effector";import{getContext as z}from"svelte";import{getPicked as G,getExperience as H}from"../hooks/client.js";import{STEP_CONTEXT_KEY as J}from"../context.js";import"svelte/store";import"@picked/analytics";import{warnDeprecation as Q}from"../utils/deprecation.js";function B(l){let u=[];return{executeFx:g(async(...f)=>await l(u,...f)),register:f=>{X.user_effect(()=>(u.push(f),()=>{u=u.filter(v=>v!==f)}))}}}function ne(l){const{id:u,title:F}=l,k=!("fieldIds"in l)||l.fieldIds===void 0;let f,v,I;if(k){v=l.onSuccess,I=l.onError;try{const e=z(J);if(!e)throw new Error("Step context not found");f=e.step.getState().fieldIds}catch{throw new Error("getForm() called without fieldIds parameter requires being inside a <Step> component. Either wrap this component in <Step>, or use the new API: getForm({ id, title, fieldIds: [...] })")}Q("getForm({ onSuccess, onError })",`This API pattern is deprecated. Please migrate to the new observable pattern:
2
+
3
+ Before (deprecated):
4
+ const { form } = getForm({
5
+ id: "my-form",
6
+ title: "My Form",
7
+ onSuccess: () => {...},
8
+ onError: (errors) => {...}
9
+ })
10
+
11
+ After (recommended):
12
+ const { form, onFormSuccess, onFormError } = getForm({
13
+ id: "my-form",
14
+ title: "My Form",
15
+ fieldIds: ["field1", "field2"] as const
16
+ })
17
+
18
+ onFormSuccess(({ data, stopNavigation }) => {...})
19
+ onFormError((errors) => {...})
20
+
21
+ Benefits: Access to form data, type safety, multiple callbacks, navigation control`)}else f=l.fieldIds;const m=G();let i=null,x,w=null;try{i=H(),x=i.currentStepStore,w=i.goToNextStep}catch{x=b(null)}const A=b([]),W=$({fields:A,responses:m.responses.listStore},({fields:e,responses:t})=>e.map(s=>{var a;const p=(a=t.find(r=>r.fieldId===s.id))==null?void 0:a.value;return{...s,value:p}})),O=N({source:m.fields.listStore,effect:g(async e=>{const t=e.filter(s=>f.some(p=>p===s.id));return await Y(m,t)})});n({clock:O.doneData,target:A}),O();const y=h(),T=h(),C=h(),q=h(),P=h(),_=b(!1),M=b(!1);M.on(q,()=>!0).on(y,()=>!1);const E=B(async(e,t)=>{await Promise.all(e.map(s=>Promise.resolve(s(t))))}),S=B(async(e,t)=>{await Promise.all(e.map(s=>Promise.resolve(s(t))))}),D=B(async(e,t)=>{let s=t;for(const p of e){const a=await Promise.resolve(p(s));a!==void 0&&(s=a)}return s}),K=g(async e=>{const t=await m.responses.commit(e);return{data:e,errors:t.errors}}),L=N({source:{step:x},mapParams:(e,t)=>({step:t.step,data:e.data}),effect:g(async({step:e,data:t})=>(i&&m.eventBus({type:"form_submit_success",form:{id:u,title:F},experience:i,step:e??void 0}),t))}),V=N({source:{step:x},mapParams:(e,t)=>({step:t.step,errors:e.errors}),effect:g(async({step:e,errors:t})=>(i&&(m.eventBus({type:"form_submit_error",form:{id:u,title:F},exception:{reason:t},experience:i,step:e??void 0}),t.forEach(s=>{m.eventBus({type:"field_validation_error",exception:{reason:[s]},experience:i,step:e??void 0})})),t))});return n({clock:y,fn:()=>!0,target:_}),n({clock:y,source:{step:x},fn:({step:e})=>({formId:u,title:F,step:e}),target:g(({formId:e,title:t,step:s})=>{i&&m.eventBus({type:"form_submit",form:{id:e,title:t},experience:i,step:s??void 0})})}),n({clock:y,source:{stepFields:A},fn:({stepFields:e},t)=>{t.preventDefault();const s=new FormData(t.currentTarget),p=Array.from(s.entries()),a={data:{},push(r){const c=r.questionId,o=this.data[c];o?Array.isArray(o)&&Array.isArray(r.value)?Array.isArray(o)&&Array.isArray(r.value)&&(typeof o[0]=="string"?this.data[c]=[...o,...r.value].map(String):typeof o[0]=="number"?this.data[c]=[...o,...r.value].map(Number):typeof o[0]=="boolean"?this.data[c]=[...o,...r.value].map(d=>!!d):this.data[c]=r.value):this.data[c]=r.value:this.data[c]=r.value}};return e.forEach(r=>{let c=p.filter(([o])=>o===r.id);c.length===0&&(c=[[r.id,""]]),c.forEach(([,o])=>{const d=r.id;if(o===""){a.push({questionId:d,value:null});return}switch(r.type){case"text":a.push({questionId:d,value:r.multiple?[String(o)]:String(o)});break;case"number":a.push({questionId:d,value:r.multiple?[Number(o)]:Number(o)});break;case"boolean":a.push({questionId:d,value:r.multiple?[o==="true"]:o==="true"});break}})}),a.data},target:D.executeFx}),n({clock:D.executeFx.doneData,target:K}),j({source:K.doneData,match:{hasErrors:({errors:e})=>e.length>0},cases:{hasErrors:V,__:L}}),n({clock:L.doneData,target:T}),n({clock:T,fn:e=>({data:e,stopNavigation:q}),target:E.executeFx}),n({clock:E.executeFx.done,target:P}),w&&n({clock:P,source:M,filter:e=>!e,target:w}),n({clock:V.doneData,target:C}),n({clock:C,target:S.executeFx}),n({clock:[P,S.executeFx.done],fn:()=>!1,target:_}),k&&(v&&E.register(()=>v()),I&&S.register(e=>I(e))),{form:{onsubmit:y},fields:W,onFormSubmit:D.register,onFormSuccess:E.register,onFormError:S.register,isFormSubmitting:_}}export{ne as getForm};
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Deprecation warning system
3
+ * Ensures warnings are shown only once per feature per session
4
+ */
5
+ export declare function warnDeprecation(feature: string, message: string): void;
6
+ /**
7
+ * Clear all deprecation warnings (useful for testing)
8
+ */
9
+ export declare function clearDeprecationWarnings(): void;
@@ -0,0 +1,4 @@
1
+ const o=new Set;function c(n,r){o.has(n)||(o.add(n),console.warn(`%c[DEPRECATED] ${n}`,"color: orange; font-weight: bold;",`
2
+
3
+ ${r}
4
+ `))}function a(){o.clear()}export{a as clearDeprecationWarnings,c as warnDeprecation};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@picked/svelte",
3
- "version": "0.0.1-dev.1768358359956",
3
+ "version": "0.0.1-dev.1768970669177",
4
4
  "dependencies": {
5
5
  "d3": "^7.9.0"
6
6
  },
@@ -75,7 +75,7 @@
75
75
  ],
76
76
  "module": "./dist/index.js",
77
77
  "peerDependencies": {
78
- "@picked/client": "0.0.1-dev.1768358359956",
78
+ "@picked/client": "0.0.1-dev.1768970669177",
79
79
  "effector": "^23.3.0",
80
80
  "svelte": "^5.34.3"
81
81
  },