@picked/svelte 0.0.1-dev.1765423029766 → 0.0.1-dev.1765777335219
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/form/index.d.ts +20 -2
- package/dist/form/index.js +1 -1
- package/dist/hooks/step.d.ts +38 -2
- package/dist/hooks/step.js +1 -1
- package/package.json +2 -2
package/dist/form/index.d.ts
CHANGED
|
@@ -1,14 +1,32 @@
|
|
|
1
1
|
import { ValidationError } from '@picked/client';
|
|
2
|
-
|
|
2
|
+
import { StepContext } from '../hooks/step';
|
|
3
|
+
export interface GetFormOptions {
|
|
3
4
|
id: string;
|
|
4
5
|
title: string;
|
|
6
|
+
fieldIds?: string[];
|
|
5
7
|
onSuccess?: () => void | Promise<void>;
|
|
6
8
|
onError?: (errors: ValidationError[]) => void | Promise<void>;
|
|
7
9
|
}
|
|
8
10
|
type SubmitEventWithCurrentTarget = SubmitEvent & {
|
|
9
11
|
currentTarget: EventTarget & HTMLFormElement;
|
|
10
12
|
};
|
|
11
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Internal implementation that creates a form with optional step context.
|
|
15
|
+
* Used by both getForm (independent) and getStep (step-scoped).
|
|
16
|
+
*/
|
|
17
|
+
export declare function createForm({ id: formId, title, fieldIds: fieldFilter, onError, onSuccess, stepContext, }: GetFormOptions & {
|
|
18
|
+
stepContext?: StepContext;
|
|
19
|
+
}): {
|
|
20
|
+
form: {
|
|
21
|
+
onsubmit: import('effector').EventCallable<SubmitEventWithCurrentTarget>;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Creates a form that works independently without requiring a step context.
|
|
26
|
+
* If called within a step context, it will automatically use the step's fields.
|
|
27
|
+
* Otherwise, it will use all available fields.
|
|
28
|
+
*/
|
|
29
|
+
export declare function getForm(options: GetFormOptions): {
|
|
12
30
|
form: {
|
|
13
31
|
onsubmit: import('effector').EventCallable<SubmitEventWithCurrentTarget>;
|
|
14
32
|
};
|
package/dist/form/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createEvent as
|
|
1
|
+
import{getContext as F}from"svelte";import{createEvent as B,createEffect as l,attach as E,sample as _,split as D}from"effector";import{getPicked as N,getExperience as I}from"../hooks/client.js";import"@picked/client";import{STEP_CONTEXT_KEY as T}from"../context.js";import"svelte/store";import"@picked/analytics";function q(){try{return F(T)}catch{return}}function w({id:n,title:c,fieldIds:d,onError:h,onSuccess:v,stepContext:y}){const o=N(),p=I(),{currentStepStore:f}=p,g=B(),x=l(e=>o.responses.commit(e));let b=(y==null?void 0:y.fields)??o.fields.listStore.map(e=>e.map(s=>({...s,value:void 0})));d&&d.length>0&&(b=b.map(e=>e.filter(s=>d.includes(s.id))));const A=E({source:{step:f},effect:l(({step:e})=>{o.eventBus({type:"form_submit_success",form:{id:n,title:c},experience:p,step:e??void 0}),v==null||v()})}),S=E({source:{step:f},mapParams:(e,s)=>({step:s.step,errors:e.errors}),effect:l(({step:e,errors:s})=>{o.eventBus({type:"form_submit_error",form:{id:n,title:c},exception:{reason:s},experience:p,step:e??void 0}),s.forEach(u=>{o.eventBus({type:"field_validation_error",exception:{reason:[u]},experience:p,step:e??void 0})}),h==null||h(s)})});return _({clock:g,source:{step:f},fn:({step:e})=>({formId:n,title:c,step:e}),target:l(({formId:e,title:s,step:u})=>{o.eventBus({type:"form_submit",form:{id:e,title:s},experience:p,step:u??void 0})})}),_({clock:g,source:{fields:o.fields.listStore,stepFields:b,step:f},fn:({stepFields:e},s)=>{s.preventDefault();const u=new FormData(s.currentTarget),k=Array.from(u.entries()),m={data:{},push(r){const a=r.questionId,t=this.data[a];t?Array.isArray(t)&&Array.isArray(r.value)?Array.isArray(t)&&Array.isArray(r.value)&&(typeof t[0]=="string"?this.data[a]=[...t,...r.value].map(String):typeof t[0]=="number"?this.data[a]=[...t,...r.value].map(Number):typeof t[0]=="boolean"?this.data[a]=[...t,...r.value].map(i=>!!i):this.data[a]=r.value):this.data[a]=r.value:this.data[a]=r.value}};return e.forEach(r=>{let a=k.filter(([t])=>t===r.id);a.length===0&&(a=[[r.id,""]]),a.forEach(([,t])=>{const i=r.id;if(t===""){m.push({questionId:i,value:null});return}switch(r.type){case"text":m.push({questionId:i,value:r.multiple?[String(t)]:String(t)});break;case"number":m.push({questionId:i,value:r.multiple?[Number(t)]:Number(t)});break;case"boolean":m.push({questionId:i,value:r.multiple?[t==="true"]:t==="true"});break}})}),m.data},target:x}),D({source:x.doneData,match:{hasErrors:({errors:e})=>e.length>0},cases:{hasErrors:S,__:A}}),{form:{onsubmit:g}}}function G(n){const c=q();return w({...n,stepContext:c})}export{w as createForm,G as getForm};
|
package/dist/hooks/step.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Store } from 'effector';
|
|
2
|
-
import { ExperienceStep, Field, Response } from '@picked/client';
|
|
2
|
+
import { ExperienceStep, Field, Response, ValidationError } from '@picked/client';
|
|
3
3
|
type FieldWithValue = Field & {
|
|
4
4
|
value: Response | undefined;
|
|
5
5
|
};
|
|
@@ -8,6 +8,42 @@ export interface StepContext {
|
|
|
8
8
|
fields: Store<FieldWithValue[]>;
|
|
9
9
|
isActiveStep: Store<boolean>;
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
interface GetStepOptions {
|
|
12
|
+
fieldIds?: string[];
|
|
13
|
+
onSuccess?: () => void | Promise<void>;
|
|
14
|
+
onError?: (errors: ValidationError[]) => void | Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Gets the current step context. Optionally accepts callbacks to create a form
|
|
18
|
+
* automatically configured with the step's id and title.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Without form
|
|
22
|
+
* const { step, fields } = getStep()
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // With form - submits all step fields
|
|
26
|
+
* const { form } = getStep({
|
|
27
|
+
* onSuccess: () => console.log('Form submitted!'),
|
|
28
|
+
* onError: (errors) => console.error('Validation errors:', errors)
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // With form - only submits specific fields (e.g., email for newsletter)
|
|
33
|
+
* const { form } = getStep({
|
|
34
|
+
* fieldIds: ['email'],
|
|
35
|
+
* onSuccess: () => console.log('Newsletter signup!'),
|
|
36
|
+
* })
|
|
37
|
+
*/
|
|
38
|
+
export declare function getStep(options?: GetStepOptions): StepContext | {
|
|
39
|
+
form: {
|
|
40
|
+
onsubmit: import('effector').EventCallable<SubmitEvent & {
|
|
41
|
+
currentTarget: EventTarget & HTMLFormElement;
|
|
42
|
+
}>;
|
|
43
|
+
};
|
|
44
|
+
step: Store<ExperienceStep>;
|
|
45
|
+
fields: Store<FieldWithValue[]>;
|
|
46
|
+
isActiveStep: Store<boolean>;
|
|
47
|
+
};
|
|
12
48
|
export declare function setStepContext(stepId: string): StepContext;
|
|
13
49
|
export {};
|
package/dist/hooks/step.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getContext as
|
|
1
|
+
import{getContext as E,setContext as v}from"svelte";import{createStore as h,combine as d,attach as C,createEffect as w,sample as l}from"effector";import{getStepFields as F}from"@picked/client";import{STEP_CONTEXT_KEY as m}from"../context.js";import{getStepRouter as A}from"../router/context.js";import{getExperience as T,getPicked as b}from"./client.js";import{createForm as I}from"../form/index.js";function N(t){const o=E(m);if(t){const r=o.step.getState();return{...o,form:I({id:`${r.id}-form`,title:r.label||r.id,fieldIds:t.fieldIds,onSuccess:t.onSuccess,onError:t.onError,stepContext:o}).form}}return o}function O(t){const o=T(),r=b(),{activeStep:u}=A(),n=u.map(e=>(e==null?void 0:e.id)===t),c=o.treeStore.map(e=>{const i=e.steps.find(s=>s.id===t);if(!i)throw new Error(`Step with id "${t}" not found in the experience tree.`);return i}),f=h([]),S=d({fields:f,responses:r.responses.listStore},({fields:e,responses:i})=>e.map(s=>{var a;const x=(a=i.find(g=>g.fieldId===s.id))==null?void 0:a.value;return{...s,value:x}})),p=C({source:d({step:c,fields:r.fields.listStore}),effect:w(async({step:{fieldIds:e},fields:i})=>await F(r,i.filter(s=>e.includes(s.id))))});return l({source:{isActiveStep:n},filter:({isActiveStep:e})=>e,target:p}),l({source:p.doneData,target:f}),v(m,{step:c,fields:S,isActiveStep:n})}export{N as getStep,O as setStepContext};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@picked/svelte",
|
|
3
|
-
"version": "0.0.1-dev.
|
|
3
|
+
"version": "0.0.1-dev.1765777335219",
|
|
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.
|
|
78
|
+
"@picked/client": "0.0.1-dev.1765777335219",
|
|
79
79
|
"effector": "^23.3.0",
|
|
80
80
|
"svelte": "^5.34.3"
|
|
81
81
|
},
|