@picked/svelte 0.0.7-next.2 → 0.0.7
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 +4 -5
- package/dist/form/index.js +1 -1
- package/dist/hooks/step.d.ts +10 -11
- package/dist/hooks/step.js +1 -1
- package/dist/router/StepRouter.svelte +14 -27
- package/dist/router/context.d.ts +2 -2
- package/package.json +2 -2
package/dist/form/index.d.ts
CHANGED
|
@@ -5,12 +5,11 @@ interface GetFormOptions {
|
|
|
5
5
|
onSuccess?: () => void | Promise<void>;
|
|
6
6
|
onError?: (errors: ValidationError[]) => void | Promise<void>;
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
currentTarget: EventTarget & HTMLFormElement;
|
|
10
|
-
};
|
|
11
|
-
export declare function getForm({ id, title, onError, onSuccess }: GetFormOptions): {
|
|
8
|
+
export declare function getForm(options: GetFormOptions): {
|
|
12
9
|
form: {
|
|
13
|
-
onsubmit:
|
|
10
|
+
onsubmit: (event: SubmitEvent & {
|
|
11
|
+
currentTarget: EventTarget & HTMLFormElement;
|
|
12
|
+
}) => Promise<void>;
|
|
14
13
|
};
|
|
15
14
|
};
|
|
16
15
|
export {};
|
package/dist/form/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{getPicked as v,getExperience as y}from"../hooks/client.js";import"svelte";import"effector";import"svelte/store";import"@picked/client";function B(a){const s=v();y();const{id:n,title:o}=a;async function d(c){var f,m;c.preventDefault(),s.eventBus({type:"form_submit",form:{id:n,title:o}});const p=new FormData(c.currentTarget),i={data:{},push(t){const r=t.questionId,e=this.data[r];e?Array.isArray(e)&&Array.isArray(t.value)?Array.isArray(e)&&Array.isArray(t.value)&&(typeof e[0]=="string"?this.data[r]=[...e,...t.value].map(String):typeof e[0]=="number"?this.data[r]=[...e,...t.value].map(Number):typeof e[0]=="boolean"?this.data[r]=[...e,...t.value].map(u=>!!u):this.data[r]=t.value):this.data[r]=t.value:this.data[r]=t.value}};for(const[t,r]of p.entries()){const e=s.fields.getById(t).getState();if(!e)continue;const u=e.id;switch(e.type){case"text":i.push({questionId:u,value:e.multiple?[String(r)]:String(r)});break;case"number":i.push({questionId:u,value:e.multiple?[Number(r)]:Number(r)});break;case"boolean":i.push({questionId:u,value:e.multiple?[r==="true"]:r==="true"});break}}const{errors:l}=await s.responses.commit(i.data);if(l.length>0){s.eventBus({type:"form_submit_error",form:{id:n,title:o},exception:"Form validation error"}),l.forEach(t=>{s.eventBus({type:"field_validation_error",exception:t})}),await((f=a.onError)==null?void 0:f.call(a,l));return}s.eventBus({type:"form_submit_success",form:{id:n,title:o}}),await((m=a.onSuccess)==null?void 0:m.call(a))}return{form:{onsubmit:d}}}export{B as getForm};
|
package/dist/hooks/step.d.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { Store } from 'effector';
|
|
2
|
+
import { Readable } from 'svelte/store';
|
|
2
3
|
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
|
-
};
|
|
10
4
|
export interface StepContext {
|
|
11
|
-
step: Store<
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
step: Store<{
|
|
6
|
+
id: string;
|
|
7
|
+
label: string;
|
|
8
|
+
}>;
|
|
9
|
+
fields: Store<{
|
|
10
|
+
field: Field | undefined;
|
|
11
|
+
value: Response | undefined;
|
|
12
|
+
}[]>;
|
|
13
|
+
isActiveStep?: Readable<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 g,setContext as C}from"svelte";import{combine as E}from"effector";import{derived as b}from"svelte/store";import{STEP_CONTEXT_KEY as f}from"../context.js";import{getStepRouter as h}from"../router/context.js";import{getExperience as w,getPicked as T}from"./client.js";function K(){return g(f)}function N(r){const l=w(),n=T(),{activeStep:s}=h(),m=s?b(s,e=>(e==null?void 0:e.id)===r):void 0,p=E({tree:l.treeStore,fields:n.fields.listStore,responses:n.responses.listStore},({tree:e,fields:c,responses:u})=>{const t=e.steps.find(o=>o.id===r);if(!t)throw new Error(`Step with id "${r}" not found in the experience tree.`);const x=t.fieldIds.map(o=>{var d;const a=c.find(i=>i.id===o),S=(d=u.find(i=>i.fieldId===o))==null?void 0:d.value;return{field:a,value:S}});return{step:{id:t.id,label:t.label},fields:x}});return C(f,{step:p.map(e=>e.step),fields:p.map(e=>e.fields),isActiveStep:m})}export{K as getStep,N as setStepContext};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import { writable } from 'svelte/store'
|
|
3
3
|
import { supportsWindow } from '@picked/client'
|
|
4
4
|
import { getExperience } from '../hooks'
|
|
5
5
|
import { setStepRouter, type Step } from './context'
|
|
@@ -8,22 +8,9 @@
|
|
|
8
8
|
|
|
9
9
|
const { currentStepStore } = experience
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const activeStep = writable<Step | null>(null)
|
|
12
12
|
let hasActiveStep = false
|
|
13
|
-
const
|
|
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
|
-
})
|
|
13
|
+
const steps = writable<Step[]>([])
|
|
27
14
|
|
|
28
15
|
const registerStep = (step: Step) => {
|
|
29
16
|
// todo: getIsSSR context
|
|
@@ -34,32 +21,32 @@
|
|
|
34
21
|
// we just skip them.
|
|
35
22
|
if (hasActiveStep) return
|
|
36
23
|
|
|
37
|
-
const isMatchingStep = step.id === currentStepStore
|
|
24
|
+
const isMatchingStep = step.id === $currentStepStore?.id
|
|
38
25
|
|
|
39
26
|
if (isMatchingStep) {
|
|
40
|
-
|
|
27
|
+
activeStep.set(step)
|
|
41
28
|
hasActiveStep = true
|
|
42
29
|
}
|
|
43
30
|
} else {
|
|
44
|
-
|
|
31
|
+
steps.update((steps) => [...steps, step])
|
|
45
32
|
}
|
|
46
33
|
}
|
|
47
34
|
|
|
48
35
|
const unregisterStep = (step: Step) => {
|
|
49
|
-
|
|
36
|
+
steps.update((st) => st.filter((s) => s !== step))
|
|
50
37
|
}
|
|
51
38
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
target: setActiveStepEvent,
|
|
56
|
-
})
|
|
39
|
+
$: {
|
|
40
|
+
activeStep.set($steps.find((s) => s.id === $currentStepStore?.id) || null)
|
|
41
|
+
}
|
|
57
42
|
|
|
58
43
|
setStepRouter({
|
|
59
44
|
registerStep,
|
|
60
45
|
unregisterStep,
|
|
61
|
-
activeStep
|
|
46
|
+
activeStep,
|
|
62
47
|
})
|
|
63
48
|
</script>
|
|
64
49
|
|
|
65
|
-
|
|
50
|
+
{#if $currentStepStore?.id}
|
|
51
|
+
<slot />
|
|
52
|
+
{/if}
|
package/dist/router/context.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Writable } from 'svelte/store';
|
|
2
2
|
export interface Step {
|
|
3
3
|
id: string;
|
|
4
4
|
}
|
|
5
5
|
interface ContextOptions {
|
|
6
|
-
activeStep
|
|
6
|
+
activeStep?: Writable<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.7
|
|
3
|
+
"version": "0.0.7",
|
|
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.7
|
|
73
|
+
"@picked/client": "0.0.7",
|
|
74
74
|
"effector": "^23.3.0",
|
|
75
75
|
"svelte": "^5.34.3"
|
|
76
76
|
},
|