solid-ctrl-flow 1.0.44 → 1.0.45
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/index.d.ts +106 -81
- package/dist/index.mjs +62 -26
- package/dist/index.umd.js +61 -25
- package/package.json +2 -1
- package/readMe.md +69 -29
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { Accessor } from 'solid-js';
|
|
2
|
+
import { ComponentProps } from 'solid-js';
|
|
2
3
|
import { Context } from 'solid-js';
|
|
3
4
|
import { EffectFunction } from 'solid-js';
|
|
4
5
|
import { JSX } from 'solid-js';
|
|
5
6
|
import { MemoOptions } from 'solid-js';
|
|
7
|
+
import { Owner } from 'solid-js';
|
|
8
|
+
import { ParentProps } from 'solid-js';
|
|
6
9
|
import { Resource } from 'solid-js';
|
|
7
10
|
import { Setter } from 'solid-js';
|
|
11
|
+
import { Show } from 'solid-js';
|
|
8
12
|
import { Signal } from 'solid-js';
|
|
13
|
+
import { Without } from 'logic-types';
|
|
9
14
|
|
|
10
15
|
/** Type hack for making {@link ReactiveContext} implement {@link Accessor} */
|
|
11
16
|
declare const BASE_CTOR: new <T>() => Accessor<T>;
|
|
@@ -38,40 +43,26 @@ export declare function bindTwoWay<S>(source: Signal<S>, dest: Signal<S>): () =>
|
|
|
38
43
|
|
|
39
44
|
export declare function bindTwoWay<S, D>(source: Signal<S>, dest: Signal<D>, to: Convert<S, D>, from: Convert<D, S>): () => void;
|
|
40
45
|
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
* {x => <>THRUTHY KEYED VALUE {x}</>}
|
|
62
|
-
* </When>
|
|
63
|
-
* </Case>
|
|
64
|
-
* <Match when>
|
|
65
|
-
* DEFAULT
|
|
66
|
-
* </Match>
|
|
67
|
-
* </Switch>
|
|
68
|
-
* <>
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
export declare const Case: (props: {
|
|
72
|
-
value: any;
|
|
73
|
-
children: JSX.Element;
|
|
74
|
-
}) => JSX.Element;
|
|
46
|
+
/** Like a {@link Match} but gets the value from the closest {@link On} ancestor */
|
|
47
|
+
export declare function Case(props: ParentProps<{
|
|
48
|
+
value: unknown;
|
|
49
|
+
}>): JSX.Element;
|
|
50
|
+
|
|
51
|
+
export declare function Case(props: ParentProps<{
|
|
52
|
+
pred: (x: any) => unknown;
|
|
53
|
+
}>): JSX.Element;
|
|
54
|
+
|
|
55
|
+
export declare function Case<T>(props: {
|
|
56
|
+
pred: (x: any) => T;
|
|
57
|
+
keyed?: false;
|
|
58
|
+
children: (x: Accessor<NonNullable<T>>) => JSX.Element;
|
|
59
|
+
}): JSX.Element;
|
|
60
|
+
|
|
61
|
+
export declare function Case<T>(props: {
|
|
62
|
+
pred: (x: any) => T;
|
|
63
|
+
keyed: true;
|
|
64
|
+
children: (x: NonNullable<T>) => JSX.Element;
|
|
65
|
+
}): JSX.Element;
|
|
75
66
|
|
|
76
67
|
/**
|
|
77
68
|
* Returns a {@link Signal} that applies the `??=` operator to the input one.
|
|
@@ -110,10 +101,16 @@ export declare function createExtractor(name?: string): {
|
|
|
110
101
|
/** Shows the content of the closest {@link Source} child of the closest {@link Joint} ancestor of this component */
|
|
111
102
|
Dest: () => JSX.Element;
|
|
112
103
|
/** Puts its content inside of a {@link Dest} if it is available; There is no need to include this component on the DOM tree */
|
|
113
|
-
Source: (props:
|
|
104
|
+
Source: (props: Slot & {
|
|
105
|
+
order?: number | undefined;
|
|
106
|
+
} & {
|
|
107
|
+
children?: JSX.Element;
|
|
108
|
+
} & {
|
|
109
|
+
sameContext?: boolean | undefined;
|
|
110
|
+
}) => JSX.Element;
|
|
114
111
|
/** Allows {@link Dest} and {@link Source} childrens to communicate with each other */
|
|
115
112
|
Joint: (props: {
|
|
116
|
-
children
|
|
113
|
+
children?: JSX.Element;
|
|
117
114
|
}) => JSX.Element;
|
|
118
115
|
/**
|
|
119
116
|
* Gets an {@link Accessor} that tells if the context in which THIS getter has been executed has been extracted (Is inside of a {@link Dest})
|
|
@@ -145,16 +142,34 @@ export declare function Enfold<T>(props: Keyed<T>): JSX.Element;
|
|
|
145
142
|
|
|
146
143
|
export declare function Enfold<T>(props: Unkeyed<T>): JSX.Element;
|
|
147
144
|
|
|
145
|
+
/**
|
|
146
|
+
* It works like a `Show`, but for `Switch`es, you can wrap `Match`es with it in order to not trigger them in certain conditions.
|
|
147
|
+
* It doesn't support the `fallback` attribute.
|
|
148
|
+
* Example:
|
|
149
|
+
* ```tsx
|
|
150
|
+
* const value = false;
|
|
151
|
+
* return <>
|
|
152
|
+
* <Switch>
|
|
153
|
+
* <Match when={false}>
|
|
154
|
+
* FALSE
|
|
155
|
+
* </Match>
|
|
156
|
+
* <Ensure when={false}>
|
|
157
|
+
* <Match when>
|
|
158
|
+
* This won't render because "value" is {false}
|
|
159
|
+
* </Match>
|
|
160
|
+
* </Ensure>
|
|
161
|
+
* <Match when>
|
|
162
|
+
* (This will render)
|
|
163
|
+
* </Match>
|
|
164
|
+
* </Switch>
|
|
165
|
+
* <>
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
export declare function Ensure(props: Without<ComponentProps<typeof Show>, "fallback">): JSX.Element;
|
|
169
|
+
|
|
148
170
|
/** Handy type alias */
|
|
149
171
|
declare type Equals = MemoOptions<unknown>["equals"];
|
|
150
172
|
|
|
151
|
-
/** Informations about a {@link Source} component */
|
|
152
|
-
declare type Info = Slot & {
|
|
153
|
-
/** Order of the current source if there are many */
|
|
154
|
-
order?: number;
|
|
155
|
-
children: JSX.Element;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
173
|
/** Parameters to pass to a keyed {@link Enfold} */
|
|
159
174
|
declare type Keyed<T> = Standard<T> & {
|
|
160
175
|
keyed: true;
|
|
@@ -181,17 +196,50 @@ export declare function memoProps<T, K extends readonly (keyof T)[]>(obj: T, key
|
|
|
181
196
|
* ```tsx
|
|
182
197
|
* return <>
|
|
183
198
|
* <Nest each={[ 1, 2, 3 ]} template={(c, x) => <>({x}, {c()}, -{x})</>}>
|
|
184
|
-
*
|
|
199
|
+
* CONTENT
|
|
185
200
|
* </Nest>
|
|
186
201
|
* </>
|
|
187
202
|
* ```
|
|
188
|
-
* Will output "(1, (2, (3,
|
|
203
|
+
* Will output "(1, (2, (3, CONTENT, -3), -2), -1)"
|
|
189
204
|
*/
|
|
190
|
-
export declare function Nest<T>(props: {
|
|
205
|
+
export declare function Nest<T>(props: ParentProps<{
|
|
191
206
|
each: readonly T[] | undefined;
|
|
192
207
|
template: (c: Accessor<JSX.Element>, x: T, i: Accessor<number>) => JSX.Element;
|
|
193
|
-
|
|
194
|
-
|
|
208
|
+
}>): JSX.Element;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Allows the use of {@link Case}s inside of {@link Switch}es.
|
|
212
|
+
* Example:
|
|
213
|
+
* ```tsx
|
|
214
|
+
* return <>
|
|
215
|
+
* <Switch>
|
|
216
|
+
* <Match when={SOMETHING}>
|
|
217
|
+
* RANDOM_CONDITION
|
|
218
|
+
* </Match>
|
|
219
|
+
* <On value={1}>
|
|
220
|
+
* <Case value={1}>
|
|
221
|
+
* TRUE
|
|
222
|
+
* </Case>
|
|
223
|
+
* <Case pred={x => x === 2}>
|
|
224
|
+
* FALSE_WITH_PREDICATE
|
|
225
|
+
* </Case>
|
|
226
|
+
* <Case pred={x => x.innerValue}>
|
|
227
|
+
* {x => <>THRUTHY VALUE {x()}</>}
|
|
228
|
+
* </Case>
|
|
229
|
+
* <Case keyed pred={x => x.innerValue}>
|
|
230
|
+
* {x => <>THRUTHY KEYED VALUE {x}</>}
|
|
231
|
+
* </Case>
|
|
232
|
+
* </On>
|
|
233
|
+
* <Match when>
|
|
234
|
+
* DEFAULT
|
|
235
|
+
* </Match>
|
|
236
|
+
* </Switch>
|
|
237
|
+
* <>
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
export declare const On: (props: ParentProps<{
|
|
241
|
+
value: any;
|
|
242
|
+
}>) => JSX.Element;
|
|
195
243
|
|
|
196
244
|
/**
|
|
197
245
|
* Reactive {@link Context} with some additional built-in functionalities.
|
|
@@ -211,18 +259,16 @@ export declare abstract class ReactiveContext<T> extends BASE_CTOR<T> {
|
|
|
211
259
|
* The function is binded for the way solid compiles components inside other objects.
|
|
212
260
|
* (The value is memoized)
|
|
213
261
|
*/
|
|
214
|
-
get Provider(): (props: {
|
|
262
|
+
get Provider(): (props: ParentProps<{
|
|
215
263
|
value?: T;
|
|
216
|
-
|
|
217
|
-
}) => JSX.Element;
|
|
264
|
+
}>) => JSX.Element;
|
|
218
265
|
/**
|
|
219
266
|
* Executes {@link runWithContext} on the current context
|
|
220
267
|
* @param value The value for the context
|
|
221
268
|
* @param f The function to run
|
|
222
269
|
* @returns The same thing {@link f} returned
|
|
223
270
|
*/
|
|
224
|
-
runWith<V extends T, R>(value: V, f: (x: V) => R): R;
|
|
225
|
-
runWith<R>(value: T | undefined, f: (x: T) => R): R;
|
|
271
|
+
runWith<V extends T, R>(value: V | undefined, f: (x: V) => R): R;
|
|
226
272
|
/**
|
|
227
273
|
* Creates a {@link ReactiveContext}
|
|
228
274
|
* @param defaultValue Initial value for the context
|
|
@@ -241,9 +287,12 @@ export declare abstract class ReactiveContext<T> extends BASE_CTOR<T> {
|
|
|
241
287
|
* @param f The function to run
|
|
242
288
|
* @returns The same thing {@link f} returned
|
|
243
289
|
*/
|
|
244
|
-
export declare function runWithContext<T, V extends T, R>(ctx: Context<T>, value: V, f: (x: V) => R): R;
|
|
290
|
+
export declare function runWithContext<T, V extends T, R>(ctx: Context<T>, value: V | undefined, f: (x: V) => R): R;
|
|
245
291
|
|
|
246
|
-
|
|
292
|
+
/** Makes its children inherit the {@link Context}s of the provided {@link Owner} */
|
|
293
|
+
export declare function SameContext(props: ParentProps<{
|
|
294
|
+
owner: Owner;
|
|
295
|
+
}>): JSX.Element;
|
|
247
296
|
|
|
248
297
|
/** Element which tracks its index in its containing array */
|
|
249
298
|
export declare interface Slot {
|
|
@@ -284,11 +333,10 @@ export declare namespace Slot {
|
|
|
284
333
|
export declare function splitAndMemoProps<T extends Record<any, any>, K extends readonly (keyof T)[]>(obj: T, keys: K, equals?: Equals): [Pick<T, Extract<K, readonly (keyof T)[]>[number]>, Omit<T, K[number]>];
|
|
285
334
|
|
|
286
335
|
/** Standard parameters to pass to an {@link Enfold} */
|
|
287
|
-
declare type Standard<T> = {
|
|
336
|
+
declare type Standard<T> = ParentProps<{
|
|
288
337
|
when: T;
|
|
289
|
-
children: JSX.Element;
|
|
290
338
|
unrecycled?: boolean;
|
|
291
|
-
}
|
|
339
|
+
}>;
|
|
292
340
|
|
|
293
341
|
/** Type of an element wrapping call-back which accepts an additional parameter */
|
|
294
342
|
declare type Template<T> = (c: Accessor<JSX.Element>, x: T) => JSX.Element;
|
|
@@ -345,27 +393,4 @@ export declare const unwrap: <T extends object>(f: Accessor<T>) => T;
|
|
|
345
393
|
*/
|
|
346
394
|
export declare function unwrapSignal<T>(f: Accessor<Signal<T>>): Signal<T>;
|
|
347
395
|
|
|
348
|
-
/** Like a {@link Match} but gets the value from the closest {@link Case} ancestor */
|
|
349
|
-
export declare function When(props: {
|
|
350
|
-
value: unknown;
|
|
351
|
-
children: JSX.Element;
|
|
352
|
-
}): JSX.Element;
|
|
353
|
-
|
|
354
|
-
export declare function When(props: {
|
|
355
|
-
pred: (x: any) => unknown;
|
|
356
|
-
children: JSX.Element;
|
|
357
|
-
}): JSX.Element;
|
|
358
|
-
|
|
359
|
-
export declare function When<T>(props: {
|
|
360
|
-
pred: (x: any) => T;
|
|
361
|
-
keyed?: false;
|
|
362
|
-
children: (x: Accessor<NonNullable<T>>) => JSX.Element;
|
|
363
|
-
}): JSX.Element;
|
|
364
|
-
|
|
365
|
-
export declare function When<T>(props: {
|
|
366
|
-
pred: (x: any) => T;
|
|
367
|
-
keyed: true;
|
|
368
|
-
children: (x: NonNullable<T>) => JSX.Element;
|
|
369
|
-
}): JSX.Element;
|
|
370
|
-
|
|
371
396
|
export { }
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useContext, createMemo, createComponent, createContext, createRoot, getOwner,
|
|
1
|
+
import { useContext, createMemo, createComponent, createContext, createRoot, getOwner, splitProps, createEffect, on, createResource, untrack, Show, onCleanup, For, mapArray, createSignal, onMount, Match, mergeProps, createRenderEffect, getListener } from "solid-js";
|
|
2
2
|
import { createMutable } from "solid-js/store";
|
|
3
3
|
const BASE_CTOR = Function;
|
|
4
4
|
class ReactiveContext extends BASE_CTOR {
|
|
@@ -64,20 +64,6 @@ function runWithContext(ctx2, value, f) {
|
|
|
64
64
|
}
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
|
-
const ctx = ReactiveContext.create(void 0, "case-when");
|
|
68
|
-
const Case = ctx.Provider;
|
|
69
|
-
function When(props) {
|
|
70
|
-
const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
|
|
71
|
-
const {
|
|
72
|
-
read
|
|
73
|
-
} = ctx;
|
|
74
|
-
const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
|
|
75
|
-
return createComponent(Match, mergeProps({
|
|
76
|
-
get when() {
|
|
77
|
-
return pred(read());
|
|
78
|
-
}
|
|
79
|
-
}, other));
|
|
80
|
-
}
|
|
81
67
|
function memoProps(obj, keys = Object.keys(obj), equals = false) {
|
|
82
68
|
const out = {};
|
|
83
69
|
for (const elm of keys)
|
|
@@ -169,7 +155,7 @@ function Dest() {
|
|
|
169
155
|
onCleanup(() => obj.attached--);
|
|
170
156
|
return createComponent(For, {
|
|
171
157
|
get each() {
|
|
172
|
-
return obj.source.toSorted((a, b) => a.order - b.order);
|
|
158
|
+
return obj.source.toSorted((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
173
159
|
},
|
|
174
160
|
children: (x) => createMemo(() => x.children)
|
|
175
161
|
});
|
|
@@ -178,15 +164,7 @@ function Source(props) {
|
|
|
178
164
|
const obj = useContext(this);
|
|
179
165
|
if (!obj)
|
|
180
166
|
return createMemo(() => props.children);
|
|
181
|
-
const
|
|
182
|
-
const info = {
|
|
183
|
-
get order() {
|
|
184
|
-
return order();
|
|
185
|
-
},
|
|
186
|
-
get children() {
|
|
187
|
-
return props.children;
|
|
188
|
-
}
|
|
189
|
-
};
|
|
167
|
+
const info = createInfo(props);
|
|
190
168
|
Slot.push(obj.source, info);
|
|
191
169
|
onCleanup(() => Slot.remove(obj.source, info));
|
|
192
170
|
return createComponent(Show, {
|
|
@@ -213,6 +191,32 @@ function Joint(props) {
|
|
|
213
191
|
}
|
|
214
192
|
});
|
|
215
193
|
}
|
|
194
|
+
function createInfo(props) {
|
|
195
|
+
const owner = getOwner(), memo = createMemo(() => props.order);
|
|
196
|
+
return {
|
|
197
|
+
get order() {
|
|
198
|
+
return memo();
|
|
199
|
+
},
|
|
200
|
+
get children() {
|
|
201
|
+
return createComponent(Show, {
|
|
202
|
+
get when() {
|
|
203
|
+
return props.sameContext;
|
|
204
|
+
},
|
|
205
|
+
get fallback() {
|
|
206
|
+
return props.children;
|
|
207
|
+
},
|
|
208
|
+
get children() {
|
|
209
|
+
return createComponent(SameContext, {
|
|
210
|
+
owner,
|
|
211
|
+
get children() {
|
|
212
|
+
return props.children;
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
}
|
|
216
220
|
function Nest(props) {
|
|
217
221
|
const memo = memoProps(props, ["template", "children"]);
|
|
218
222
|
const content = mapArray(() => props.each, (x, i) => {
|
|
@@ -226,6 +230,30 @@ function Nest(props) {
|
|
|
226
230
|
});
|
|
227
231
|
return createMemo(() => content().reduceRight((a, b) => b(a), memo.children));
|
|
228
232
|
}
|
|
233
|
+
const ctx = ReactiveContext.create(void 0, "on-case");
|
|
234
|
+
const On = ctx.Provider;
|
|
235
|
+
function Case(props) {
|
|
236
|
+
const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
|
|
237
|
+
const {
|
|
238
|
+
read
|
|
239
|
+
} = ctx;
|
|
240
|
+
const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
|
|
241
|
+
return createComponent(Match, mergeProps({
|
|
242
|
+
get when() {
|
|
243
|
+
return pred(read());
|
|
244
|
+
}
|
|
245
|
+
}, other));
|
|
246
|
+
}
|
|
247
|
+
function Ensure(props) {
|
|
248
|
+
return createComponent(Show, mergeProps(props, {
|
|
249
|
+
get fallback() {
|
|
250
|
+
return createComponent(Match, {
|
|
251
|
+
when: false,
|
|
252
|
+
children: void 0
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}));
|
|
256
|
+
}
|
|
229
257
|
const IDENTITY = (x) => x;
|
|
230
258
|
function bind(source, dest, to = IDENTITY, skip = false) {
|
|
231
259
|
const d = createRoot((d2) => (createRenderEffect(on(source, (x) => skip ? skip = false : dest((prev) => to(x, prev)))), d2));
|
|
@@ -264,13 +292,21 @@ function coalesceSignal([get, set], f) {
|
|
|
264
292
|
}
|
|
265
293
|
}
|
|
266
294
|
const debug = ReactiveContext.create(false, "debug-scope");
|
|
295
|
+
function SameContext(props) {
|
|
296
|
+
return createMemo(() => {
|
|
297
|
+
getOwner().context = props.owner.context;
|
|
298
|
+
return props.children;
|
|
299
|
+
});
|
|
300
|
+
}
|
|
267
301
|
export {
|
|
268
302
|
Case,
|
|
269
303
|
Enfold,
|
|
304
|
+
Ensure,
|
|
270
305
|
Nest,
|
|
306
|
+
On,
|
|
271
307
|
ReactiveContext,
|
|
308
|
+
SameContext,
|
|
272
309
|
Slot,
|
|
273
|
-
When,
|
|
274
310
|
bind,
|
|
275
311
|
bindTwoWay,
|
|
276
312
|
coalesceSignal,
|
package/dist/index.umd.js
CHANGED
|
@@ -66,20 +66,6 @@
|
|
|
66
66
|
}
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
-
const ctx = ReactiveContext.create(void 0, "case-when");
|
|
70
|
-
const Case = ctx.Provider;
|
|
71
|
-
function When(props) {
|
|
72
|
-
const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
|
|
73
|
-
const {
|
|
74
|
-
read
|
|
75
|
-
} = ctx;
|
|
76
|
-
const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
|
|
77
|
-
return solidJs.createComponent(solidJs.Match, solidJs.mergeProps({
|
|
78
|
-
get when() {
|
|
79
|
-
return pred(read());
|
|
80
|
-
}
|
|
81
|
-
}, other));
|
|
82
|
-
}
|
|
83
69
|
function memoProps(obj, keys = Object.keys(obj), equals = false) {
|
|
84
70
|
const out = {};
|
|
85
71
|
for (const elm of keys)
|
|
@@ -171,7 +157,7 @@
|
|
|
171
157
|
solidJs.onCleanup(() => obj.attached--);
|
|
172
158
|
return solidJs.createComponent(solidJs.For, {
|
|
173
159
|
get each() {
|
|
174
|
-
return obj.source.toSorted((a, b) => a.order - b.order);
|
|
160
|
+
return obj.source.toSorted((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
175
161
|
},
|
|
176
162
|
children: (x) => solidJs.createMemo(() => x.children)
|
|
177
163
|
});
|
|
@@ -180,15 +166,7 @@
|
|
|
180
166
|
const obj = solidJs.useContext(this);
|
|
181
167
|
if (!obj)
|
|
182
168
|
return solidJs.createMemo(() => props.children);
|
|
183
|
-
const
|
|
184
|
-
const info = {
|
|
185
|
-
get order() {
|
|
186
|
-
return order();
|
|
187
|
-
},
|
|
188
|
-
get children() {
|
|
189
|
-
return props.children;
|
|
190
|
-
}
|
|
191
|
-
};
|
|
169
|
+
const info = createInfo(props);
|
|
192
170
|
exports2.Slot.push(obj.source, info);
|
|
193
171
|
solidJs.onCleanup(() => exports2.Slot.remove(obj.source, info));
|
|
194
172
|
return solidJs.createComponent(solidJs.Show, {
|
|
@@ -215,6 +193,32 @@
|
|
|
215
193
|
}
|
|
216
194
|
});
|
|
217
195
|
}
|
|
196
|
+
function createInfo(props) {
|
|
197
|
+
const owner = solidJs.getOwner(), memo = solidJs.createMemo(() => props.order);
|
|
198
|
+
return {
|
|
199
|
+
get order() {
|
|
200
|
+
return memo();
|
|
201
|
+
},
|
|
202
|
+
get children() {
|
|
203
|
+
return solidJs.createComponent(solidJs.Show, {
|
|
204
|
+
get when() {
|
|
205
|
+
return props.sameContext;
|
|
206
|
+
},
|
|
207
|
+
get fallback() {
|
|
208
|
+
return props.children;
|
|
209
|
+
},
|
|
210
|
+
get children() {
|
|
211
|
+
return solidJs.createComponent(SameContext, {
|
|
212
|
+
owner,
|
|
213
|
+
get children() {
|
|
214
|
+
return props.children;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
218
222
|
function Nest(props) {
|
|
219
223
|
const memo = memoProps(props, ["template", "children"]);
|
|
220
224
|
const content = solidJs.mapArray(() => props.each, (x, i) => {
|
|
@@ -228,6 +232,30 @@
|
|
|
228
232
|
});
|
|
229
233
|
return solidJs.createMemo(() => content().reduceRight((a, b) => b(a), memo.children));
|
|
230
234
|
}
|
|
235
|
+
const ctx = ReactiveContext.create(void 0, "on-case");
|
|
236
|
+
const On = ctx.Provider;
|
|
237
|
+
function Case(props) {
|
|
238
|
+
const [mine, other] = splitAndMemoProps(props, ["value", "pred"]);
|
|
239
|
+
const {
|
|
240
|
+
read
|
|
241
|
+
} = ctx;
|
|
242
|
+
const pred = (x) => mine.pred ? mine.pred(x) : x === mine.value;
|
|
243
|
+
return solidJs.createComponent(solidJs.Match, solidJs.mergeProps({
|
|
244
|
+
get when() {
|
|
245
|
+
return pred(read());
|
|
246
|
+
}
|
|
247
|
+
}, other));
|
|
248
|
+
}
|
|
249
|
+
function Ensure(props) {
|
|
250
|
+
return solidJs.createComponent(solidJs.Show, solidJs.mergeProps(props, {
|
|
251
|
+
get fallback() {
|
|
252
|
+
return solidJs.createComponent(solidJs.Match, {
|
|
253
|
+
when: false,
|
|
254
|
+
children: void 0
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}));
|
|
258
|
+
}
|
|
231
259
|
const IDENTITY = (x) => x;
|
|
232
260
|
function bind(source, dest, to = IDENTITY, skip = false) {
|
|
233
261
|
const d = solidJs.createRoot((d2) => (solidJs.createRenderEffect(solidJs.on(source, (x) => skip ? skip = false : dest((prev) => to(x, prev)))), d2));
|
|
@@ -266,11 +294,19 @@
|
|
|
266
294
|
}
|
|
267
295
|
}
|
|
268
296
|
const debug = ReactiveContext.create(false, "debug-scope");
|
|
297
|
+
function SameContext(props) {
|
|
298
|
+
return solidJs.createMemo(() => {
|
|
299
|
+
solidJs.getOwner().context = props.owner.context;
|
|
300
|
+
return props.children;
|
|
301
|
+
});
|
|
302
|
+
}
|
|
269
303
|
exports2.Case = Case;
|
|
270
304
|
exports2.Enfold = Enfold;
|
|
305
|
+
exports2.Ensure = Ensure;
|
|
271
306
|
exports2.Nest = Nest;
|
|
307
|
+
exports2.On = On;
|
|
272
308
|
exports2.ReactiveContext = ReactiveContext;
|
|
273
|
-
exports2.
|
|
309
|
+
exports2.SameContext = SameContext;
|
|
274
310
|
exports2.bind = bind;
|
|
275
311
|
exports2.bindTwoWay = bindTwoWay;
|
|
276
312
|
exports2.coalesceSignal = coalesceSignal;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solid-ctrl-flow",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.45",
|
|
4
4
|
"description": "Control flow components for solid-js",
|
|
5
5
|
"author": "AFatNiBBa",
|
|
6
6
|
"license": "ISC",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@types/node": "^20.6.0",
|
|
19
|
+
"logic-types": "^1.3.2",
|
|
19
20
|
"vite": "^4.4.9",
|
|
20
21
|
"vite-plugin-dts": "^3.5.3",
|
|
21
22
|
"vite-plugin-solid": "^2.7.0"
|
package/readMe.md
CHANGED
|
@@ -5,32 +5,14 @@ It's always under hard development
|
|
|
5
5
|
|
|
6
6
|
## Components
|
|
7
7
|
|
|
8
|
-
### `
|
|
9
|
-
|
|
10
|
-
The following code
|
|
8
|
+
### `SameContext`
|
|
9
|
+
Makes its children inherit the `Context`s of the provided `Owner`
|
|
11
10
|
```tsx
|
|
12
|
-
const
|
|
11
|
+
const owner = ...;
|
|
13
12
|
return <>
|
|
14
|
-
<
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
<When value={2}>Two</When>
|
|
18
|
-
<When pred={(x) => `${x}`.length === 3}>Long</When>
|
|
19
|
-
</Case>
|
|
20
|
-
<Match when>Default</Match>
|
|
21
|
-
</Switch>
|
|
22
|
-
</>
|
|
23
|
-
```
|
|
24
|
-
Is equivalent to this
|
|
25
|
-
```tsx
|
|
26
|
-
const value = 1;
|
|
27
|
-
return <>
|
|
28
|
-
<Switch>
|
|
29
|
-
<Match when={value === 1}>One</Match>
|
|
30
|
-
<Match when={value === 2}>Two</Match>
|
|
31
|
-
<Match when={`${value}`.length === 3}>Long</Match>
|
|
32
|
-
<Match when>Default</Match>
|
|
33
|
-
</Switch>
|
|
13
|
+
<SameContext owner={owner}>
|
|
14
|
+
...
|
|
15
|
+
</SameContext>
|
|
34
16
|
</>
|
|
35
17
|
```
|
|
36
18
|
|
|
@@ -41,7 +23,7 @@ The following code
|
|
|
41
23
|
const value = true;
|
|
42
24
|
return <>
|
|
43
25
|
<Enfold when={value} template={c => <div style={{ background: "red" }}>{c()}</div>}>
|
|
44
|
-
|
|
26
|
+
CONTENT
|
|
45
27
|
</Enfold>
|
|
46
28
|
</>
|
|
47
29
|
```
|
|
@@ -49,13 +31,13 @@ Is equivalent to this
|
|
|
49
31
|
```tsx
|
|
50
32
|
return <>
|
|
51
33
|
<div style={{ background: "red" }}>
|
|
52
|
-
|
|
34
|
+
CONTENT
|
|
53
35
|
</div>
|
|
54
36
|
</>
|
|
55
37
|
```
|
|
56
38
|
And if `value` was falsish, it would have been equivalent to this
|
|
57
39
|
```tsx
|
|
58
|
-
return <>
|
|
40
|
+
return <>CONTENT</>
|
|
59
41
|
```
|
|
60
42
|
If you are wrapping the content with a context provider you need to set the `unrecycled` attribute to `true` to run again the whole content, otherwise the `Owner` of the content won't be child of the template one
|
|
61
43
|
```tsx
|
|
@@ -72,7 +54,7 @@ The following code
|
|
|
72
54
|
```tsx
|
|
73
55
|
return <>
|
|
74
56
|
<Nest each={[ "red", "green", "blue" ]} template={(c, x) => <div style={{ background: x, padding: "1ch" }}>{c()}</div>}>
|
|
75
|
-
|
|
57
|
+
CONTENT
|
|
76
58
|
</Nest>
|
|
77
59
|
</>
|
|
78
60
|
```
|
|
@@ -82,13 +64,63 @@ return <>
|
|
|
82
64
|
<div style={{ background: "red", padding: "1ch" }}>
|
|
83
65
|
<div style={{ background: "green", padding: "1ch" }}>
|
|
84
66
|
<div style={{ background: "blue", padding: "1ch" }}>
|
|
85
|
-
|
|
67
|
+
CONTENT
|
|
86
68
|
</div>
|
|
87
69
|
</div>
|
|
88
70
|
</div>
|
|
89
71
|
</>
|
|
90
72
|
```
|
|
91
73
|
|
|
74
|
+
### `On` - `Case`
|
|
75
|
+
Allows you to not repeat the same condition across many `Match` components.
|
|
76
|
+
The following code
|
|
77
|
+
```tsx
|
|
78
|
+
const value = 1;
|
|
79
|
+
return <>
|
|
80
|
+
<Switch>
|
|
81
|
+
<On value={value}>
|
|
82
|
+
<Case value={1}>One</Case>
|
|
83
|
+
<Case value={2}>Two</Case>
|
|
84
|
+
<Case pred={x => `${x}`.length === 3}>Long</Case>
|
|
85
|
+
</On>
|
|
86
|
+
<Match when>Default</Match>
|
|
87
|
+
</Switch>
|
|
88
|
+
</>
|
|
89
|
+
```
|
|
90
|
+
Is equivalent to this
|
|
91
|
+
```tsx
|
|
92
|
+
const value = 1;
|
|
93
|
+
return <>
|
|
94
|
+
<Switch>
|
|
95
|
+
<Match when={value === 1}>One</Match>
|
|
96
|
+
<Match when={value === 2}>Two</Match>
|
|
97
|
+
<Match when={`${value}`.length === 3}>Long</Match>
|
|
98
|
+
<Match when>Default</Match>
|
|
99
|
+
</Switch>
|
|
100
|
+
</>
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### `Ensure`
|
|
104
|
+
It works like a `Show`, but for `Switch`es, you can wrap `Match`es with it in order to not trigger them in certain conditions
|
|
105
|
+
```tsx
|
|
106
|
+
const value = false;
|
|
107
|
+
return <>
|
|
108
|
+
<Switch>
|
|
109
|
+
<Match when={false}>
|
|
110
|
+
FALSE
|
|
111
|
+
</Match>
|
|
112
|
+
<Ensure when={false}>
|
|
113
|
+
<Match when>
|
|
114
|
+
This won't render because "value" is {false}
|
|
115
|
+
</Match>
|
|
116
|
+
</Ensure>
|
|
117
|
+
<Match when>
|
|
118
|
+
(This will render)
|
|
119
|
+
</Match>
|
|
120
|
+
</Switch>
|
|
121
|
+
<>
|
|
122
|
+
```
|
|
123
|
+
|
|
92
124
|
### `createExtractor()`
|
|
93
125
|
Creates a context for components that may need to be out of their parent in certain conditions.
|
|
94
126
|
If you have this component
|
|
@@ -162,6 +194,14 @@ Notice that you can use a `Source` component without adding it to the DOM:
|
|
|
162
194
|
</myCustomExtractor.Source>
|
|
163
195
|
return <>Something else</>
|
|
164
196
|
```
|
|
197
|
+
When something gets moved from a `Source` to a `Dest`, it will inherit contexts from its destination, UNLESS you use the `sameContext` attribute
|
|
198
|
+
```tsx
|
|
199
|
+
return <>
|
|
200
|
+
<myCustomExtractor.Source sameContext>
|
|
201
|
+
...
|
|
202
|
+
</myCustomExtractor.Source>
|
|
203
|
+
</>
|
|
204
|
+
```
|
|
165
205
|
|
|
166
206
|
## Utility
|
|
167
207
|
|