@picked/svelte 0.0.1-dev.1765777335219 → 0.0.1-dev.1765824208742
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 +2 -20
- package/dist/form/index.js +1 -1
- package/dist/hooks/step.d.ts +2 -38
- package/dist/hooks/step.js +1 -1
- package/dist/render/browser.js +1 -1
- package/dist/utils/ErrorBoundary.svelte +25 -0
- package/package.json +2 -2
package/dist/form/index.d.ts
CHANGED
|
@@ -1,32 +1,14 @@
|
|
|
1
1
|
import { ValidationError } from '@picked/client';
|
|
2
|
-
|
|
3
|
-
export interface GetFormOptions {
|
|
2
|
+
interface GetFormOptions {
|
|
4
3
|
id: string;
|
|
5
4
|
title: string;
|
|
6
|
-
fieldIds?: string[];
|
|
7
5
|
onSuccess?: () => void | Promise<void>;
|
|
8
6
|
onError?: (errors: ValidationError[]) => void | Promise<void>;
|
|
9
7
|
}
|
|
10
8
|
type SubmitEventWithCurrentTarget = SubmitEvent & {
|
|
11
9
|
currentTarget: EventTarget & HTMLFormElement;
|
|
12
10
|
};
|
|
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): {
|
|
11
|
+
export declare function getForm({ id: formId, title, onError, onSuccess }: GetFormOptions): {
|
|
30
12
|
form: {
|
|
31
13
|
onsubmit: import('effector').EventCallable<SubmitEventWithCurrentTarget>;
|
|
32
14
|
};
|
package/dist/form/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{createEvent as k,createEffect as m,attach as b,sample as g,split as D}from"effector";import{getPicked as F,getExperience as S}from"../hooks/client.js";import{getStep as B}from"../hooks/step.js";import"svelte";import"svelte/store";import"@picked/client";import"@picked/analytics";function z({id:f,title:l,onError:d,onSuccess:h}){const o=F(),x=B(),n=S(),{currentStepStore:u}=n,v=k(),y=m(r=>o.responses.commit(r)),A=b({source:{step:u},effect:m(({step:r})=>{o.eventBus({type:"form_submit_success",form:{id:f,title:l},experience:n,step:r??void 0}),h==null||h()})}),_=b({source:{step:u},mapParams:(r,a)=>({step:a.step,errors:r.errors}),effect:m(({step:r,errors:a})=>{o.eventBus({type:"form_submit_error",form:{id:f,title:l},exception:{reason:a},experience:n,step:r??void 0}),a.forEach(p=>{o.eventBus({type:"field_validation_error",exception:{reason:[p]},experience:n,step:r??void 0})}),d==null||d(a)})});return g({clock:v,source:{step:u},fn:({step:r})=>({formId:f,title:l,step:r}),target:m(({formId:r,title:a,step:p})=>{o.eventBus({type:"form_submit",form:{id:r,title:a},experience:n,step:p??void 0})})}),g({clock:v,source:{fields:o.fields.listStore,stepFields:x.fields,step:u},fn:({stepFields:r},a)=>{a.preventDefault();const p=new FormData(a.currentTarget),E=Array.from(p.entries()),c={data:{},push(t){const s=t.questionId,e=this.data[s];e?Array.isArray(e)&&Array.isArray(t.value)?Array.isArray(e)&&Array.isArray(t.value)&&(typeof e[0]=="string"?this.data[s]=[...e,...t.value].map(String):typeof e[0]=="number"?this.data[s]=[...e,...t.value].map(Number):typeof e[0]=="boolean"?this.data[s]=[...e,...t.value].map(i=>!!i):this.data[s]=t.value):this.data[s]=t.value:this.data[s]=t.value}};return r.forEach(t=>{let s=E.filter(([e])=>e===t.id);s.length===0&&(s=[[t.id,""]]),s.forEach(([,e])=>{const i=t.id;if(e===""){c.push({questionId:i,value:null});return}switch(t.type){case"text":c.push({questionId:i,value:t.multiple?[String(e)]:String(e)});break;case"number":c.push({questionId:i,value:t.multiple?[Number(e)]:Number(e)});break;case"boolean":c.push({questionId:i,value:t.multiple?[e==="true"]:e==="true"});break}})}),c.data},target:y}),D({source:y.doneData,match:{hasErrors:({errors:r})=>r.length>0},cases:{hasErrors:_,__:A}}),{form:{onsubmit:v}}}export{z as getForm};
|
package/dist/hooks/step.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Store } from 'effector';
|
|
2
|
-
import { ExperienceStep, Field, Response
|
|
2
|
+
import { ExperienceStep, Field, Response } from '@picked/client';
|
|
3
3
|
type FieldWithValue = Field & {
|
|
4
4
|
value: Response | undefined;
|
|
5
5
|
};
|
|
@@ -8,42 +8,6 @@ export interface StepContext {
|
|
|
8
8
|
fields: Store<FieldWithValue[]>;
|
|
9
9
|
isActiveStep: Store<boolean>;
|
|
10
10
|
}
|
|
11
|
-
|
|
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
|
-
};
|
|
11
|
+
export declare function getStep(): StepContext;
|
|
48
12
|
export declare function setStepContext(stepId: string): StepContext;
|
|
49
13
|
export {};
|
package/dist/hooks/step.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getContext as
|
|
1
|
+
import{getContext as v,setContext as E}from"svelte";import{createStore as h,combine as a,attach as w,createEffect as C,sample as l}from"effector";import{getStepFields as A}from"@picked/client";import{STEP_CONTEXT_KEY as d}from"../context.js";import{getStepRouter as F}from"../router/context.js";import{getExperience as T,getPicked as P}from"./client.js";function N(){return v(d)}function O(o){const u=T(),i=P(),{activeStep:m}=F(),n=m.map(e=>(e==null?void 0:e.id)===o),s=u.treeStore.map(e=>{const t=e.steps.find(r=>r.id===o);if(!t)throw new Error(`Step with id "${o}" not found in the experience tree.`);return t}),c=h([]),S=a({fields:c,responses:i.responses.listStore},({fields:e,responses:t})=>e.map(r=>{var f;const g=(f=t.find(x=>x.fieldId===r.id))==null?void 0:f.value;return{...r,value:g}})),p=w({source:a({step:s,fields:i.fields.listStore}),effect:C(async({step:{fieldIds:e},fields:t})=>await A(i,t.filter(r=>e.includes(r.id))))});return l({source:{isActiveStep:n},filter:({isActiveStep:e})=>e,target:p}),l({source:p.doneData,target:c}),E(d,{step:s,fields:S,isActiveStep:n})}export{N as getStep,O as setStepContext};
|
package/dist/render/browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{mount as
|
|
1
|
+
import{mount as d}from"svelte";import{isValidAdaptedComponent as E}from"../component.js";import{CLIENT_CONTEXT_KEY as f,EXPERIENCE_CONTEXT_KEY as h}from"../context.js";import{mergeArrays as u}from"../utils/array.js";import y from"../utils/ErrorBoundary.svelte";function C(n,e){return new Map([[f,n],[h,e]])}const k=async(n,e,s,o,i)=>{var p,a;if(!E(e))throw new Error("The component is invalid.");const c=u((p=e.style)==null?void 0:p.sheets,i.stylesheets),m=u((a=e.style)==null?void 0:a.css,i.css);c.length&&c.forEach(r=>{const t=document.createElement("link");t.setAttribute("rel","stylesheet"),t.setAttribute("href",r),t.setAttribute("type","text/css"),t.setAttribute("picked-injected","true"),n.appendChild(t)}),m.length&&m.forEach(r=>{const t=document.createElement("style");t.textContent=r,t.setAttribute("type","text/css"),t.setAttribute("picked-injected","true"),n.appendChild(t)});const l=r=>{d(y,{target:n,props:{children:r,componentProps:{client:s,experience:o}},context:C(s,o)})};if(e.component instanceof Promise){const{default:r}=await e.component;l(r);return}l(e.component)},N=n=>{};export{C as createContext,k as renderSvelteComponent,N as unRenderSvelteComponent};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { type Snippet } from 'svelte'
|
|
3
|
+
import { getPicked } from '../hooks'
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
children?: Snippet<[{ componentProps?: Record<string, any> }]>
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
componentProps?: Record<string, any>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const { children, componentProps }: Props = $props()
|
|
13
|
+
|
|
14
|
+
function handleError(error: unknown) {
|
|
15
|
+
const client = getPicked()
|
|
16
|
+
|
|
17
|
+
client.eventBus({
|
|
18
|
+
type: 'exception',
|
|
19
|
+
level: 'error',
|
|
20
|
+
data: error,
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<svelte:boundary onerror={handleError}>{@render children?.({ ...componentProps })}</svelte:boundary>
|
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.1765824208742",
|
|
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.1765824208742",
|
|
79
79
|
"effector": "^23.3.0",
|
|
80
80
|
"svelte": "^5.34.3"
|
|
81
81
|
},
|