@picked/svelte 0.0.7 → 0.0.8
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 +5 -4
- package/dist/form/index.js +1 -1
- package/dist/hooks/step.d.ts +11 -10
- package/dist/hooks/step.js +1 -1
- package/dist/router/StepRouter.svelte +27 -14
- package/dist/router/context.d.ts +2 -2
- package/package.json +2 -2
package/dist/form/index.d.ts
CHANGED
|
@@ -5,11 +5,12 @@ interface GetFormOptions {
|
|
|
5
5
|
onSuccess?: () => void | Promise<void>;
|
|
6
6
|
onError?: (errors: ValidationError[]) => void | Promise<void>;
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
type SubmitEventWithCurrentTarget = SubmitEvent & {
|
|
9
|
+
currentTarget: EventTarget & HTMLFormElement;
|
|
10
|
+
};
|
|
11
|
+
export declare function getForm({ id, title, onError, onSuccess }: GetFormOptions): {
|
|
9
12
|
form: {
|
|
10
|
-
onsubmit: (
|
|
11
|
-
currentTarget: EventTarget & HTMLFormElement;
|
|
12
|
-
}) => Promise<void>;
|
|
13
|
+
onsubmit: import('effector').EventCallable<SubmitEventWithCurrentTarget>;
|
|
13
14
|
};
|
|
14
15
|
};
|
|
15
16
|
export {};
|
package/dist/form/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{createEvent as g,createEffect as f,sample as A,split as _}from"effector";import{getPicked as x}from"../hooks/client.js";import"svelte";import"@picked/client";import"svelte/store";function E({id:c,title:m,onError:u,onSuccess:l}){const s=x(),p=g(),d=f(a=>s.responses.commit(a)),v=f(()=>{s.eventBus({type:"form_submit_success",form:{id:c,title:m}}),l==null||l()}),h=f(({errors:a})=>{s.eventBus({type:"form_submit_error",form:{id:c,title:m},exception:"Form validation error"}),a.forEach(i=>{s.eventBus({type:"field_validation_error",exception:i})}),u==null||u([])});return A({clock:p,source:{fields:s.fields.listStore},fn:({fields:a},i)=>{i.preventDefault();const y=new FormData(i.currentTarget),n={data:{},push(r){const t=r.questionId,e=this.data[t];e?Array.isArray(e)&&Array.isArray(r.value)?Array.isArray(e)&&Array.isArray(r.value)&&(typeof e[0]=="string"?this.data[t]=[...e,...r.value].map(String):typeof e[0]=="number"?this.data[t]=[...e,...r.value].map(Number):typeof e[0]=="boolean"?this.data[t]=[...e,...r.value].map(o=>!!o):this.data[t]=r.value):this.data[t]=r.value:this.data[t]=r.value}};for(const[r,t]of y.entries()){const e=a.find(b=>b.id===r);if(!e)continue;const o=e.id;switch(e.type){case"text":n.push({questionId:o,value:e.multiple?[String(t)]:String(t)});break;case"number":n.push({questionId:o,value:e.multiple?[Number(t)]:Number(t)});break;case"boolean":n.push({questionId:o,value:e.multiple?[t==="true"]:t==="true"});break}}return n.data},target:d}),_({source:d.doneData,match:{hasErrors:({errors:a})=>a.length>0},cases:{hasErrors:h,__:v}}),{form:{onsubmit:p}}}export{E as getForm};
|
package/dist/hooks/step.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { Store } from 'effector';
|
|
2
|
-
import { Readable } from 'svelte/store';
|
|
3
2
|
import { Field, Response } from '@picked/client';
|
|
3
|
+
import { Step } from '../router/context';
|
|
4
|
+
type FieldWithValue = Field & {
|
|
5
|
+
value: Response | undefined;
|
|
6
|
+
};
|
|
7
|
+
type StepWithLabel = Step & {
|
|
8
|
+
label?: string;
|
|
9
|
+
};
|
|
4
10
|
export interface StepContext {
|
|
5
|
-
step: Store<
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}>;
|
|
9
|
-
fields: Store<{
|
|
10
|
-
field: Field | undefined;
|
|
11
|
-
value: Response | undefined;
|
|
12
|
-
}[]>;
|
|
13
|
-
isActiveStep?: Readable<boolean>;
|
|
11
|
+
step: Store<StepWithLabel>;
|
|
12
|
+
fields: Store<FieldWithValue[]>;
|
|
13
|
+
isActiveStep: Store<boolean>;
|
|
14
14
|
}
|
|
15
15
|
export declare function getStep(): StepContext;
|
|
16
16
|
export declare function setStepContext(stepId: string): StepContext;
|
|
17
|
+
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};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import { createEvent, createStore, sample } from 'effector'
|
|
3
3
|
import { supportsWindow } from '@picked/client'
|
|
4
4
|
import { getExperience } from '../hooks'
|
|
5
5
|
import { setStepRouter, type Step } from './context'
|
|
@@ -8,9 +8,22 @@
|
|
|
8
8
|
|
|
9
9
|
const { currentStepStore } = experience
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const activeStepStore = createStore<Step | null>(null)
|
|
12
12
|
let hasActiveStep = false
|
|
13
|
-
const
|
|
13
|
+
const stepsStore = createStore<Step[]>([])
|
|
14
|
+
|
|
15
|
+
const registerStepEvent = createEvent<Step>()
|
|
16
|
+
const unregisterStepEvent = createEvent<Step>()
|
|
17
|
+
const setActiveStepEvent = createEvent<Step | null>()
|
|
18
|
+
|
|
19
|
+
stepsStore.on(registerStepEvent, (state, step) => [...state, step])
|
|
20
|
+
stepsStore.on(unregisterStepEvent, (state, step) => state.filter((s) => s !== step))
|
|
21
|
+
|
|
22
|
+
sample({
|
|
23
|
+
source: setActiveStepEvent,
|
|
24
|
+
filter: (step) => step !== null,
|
|
25
|
+
target: activeStepStore,
|
|
26
|
+
})
|
|
14
27
|
|
|
15
28
|
const registerStep = (step: Step) => {
|
|
16
29
|
// todo: getIsSSR context
|
|
@@ -21,32 +34,32 @@
|
|
|
21
34
|
// we just skip them.
|
|
22
35
|
if (hasActiveStep) return
|
|
23
36
|
|
|
24
|
-
const isMatchingStep = step.id ===
|
|
37
|
+
const isMatchingStep = step.id === currentStepStore.getState()?.id
|
|
25
38
|
|
|
26
39
|
if (isMatchingStep) {
|
|
27
|
-
|
|
40
|
+
setActiveStepEvent(step)
|
|
28
41
|
hasActiveStep = true
|
|
29
42
|
}
|
|
30
43
|
} else {
|
|
31
|
-
|
|
44
|
+
registerStepEvent(step)
|
|
32
45
|
}
|
|
33
46
|
}
|
|
34
47
|
|
|
35
48
|
const unregisterStep = (step: Step) => {
|
|
36
|
-
|
|
49
|
+
unregisterStepEvent(step)
|
|
37
50
|
}
|
|
38
51
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
52
|
+
sample({
|
|
53
|
+
source: { steps: stepsStore, currentStep: currentStepStore },
|
|
54
|
+
fn: ({ steps, currentStep }) => steps.find((s) => s.id === currentStep?.id) || null,
|
|
55
|
+
target: setActiveStepEvent,
|
|
56
|
+
})
|
|
42
57
|
|
|
43
58
|
setStepRouter({
|
|
44
59
|
registerStep,
|
|
45
60
|
unregisterStep,
|
|
46
|
-
activeStep,
|
|
61
|
+
activeStep: activeStepStore.map((val) => val),
|
|
47
62
|
})
|
|
48
63
|
</script>
|
|
49
64
|
|
|
50
|
-
|
|
51
|
-
<slot />
|
|
52
|
-
{/if}
|
|
65
|
+
<slot />
|
package/dist/router/context.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Store } from 'effector';
|
|
2
2
|
export interface Step {
|
|
3
3
|
id: string;
|
|
4
4
|
}
|
|
5
5
|
interface ContextOptions {
|
|
6
|
-
activeStep
|
|
6
|
+
activeStep: Store<Step | null>;
|
|
7
7
|
unregisterStep: (step: Step) => void;
|
|
8
8
|
registerStep: (step: Step) => void;
|
|
9
9
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@picked/svelte",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"d3": "^7.9.0"
|
|
6
6
|
},
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
],
|
|
71
71
|
"module": "./dist/index.js",
|
|
72
72
|
"peerDependencies": {
|
|
73
|
-
"@picked/client": "0.0.
|
|
73
|
+
"@picked/client": "0.0.8",
|
|
74
74
|
"effector": "^23.3.0",
|
|
75
75
|
"svelte": "^5.34.3"
|
|
76
76
|
},
|