@agnos-ui/core 0.0.1-alpha.0 → 0.0.1-alpha.2
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/README.md +4 -4
- package/{dist/lib/accordion.d.ts → accordion.d.ts} +17 -22
- package/{dist/lib/accordion.js → accordion.js} +66 -68
- package/{dist/lib/alert.d.ts → alert.d.ts} +2 -1
- package/{dist/lib/alert.js → alert.js} +10 -7
- package/commonProps.d.ts +6 -0
- package/commonProps.js +1 -0
- package/{dist/lib/config.d.ts → config.d.ts} +5 -0
- package/extendWidget.d.ts +3 -0
- package/extendWidget.js +28 -0
- package/{dist/lib/index.d.ts → index.d.ts} +3 -0
- package/{dist/lib/index.js → index.js} +3 -0
- package/{dist/lib/modal → modal}/modal.d.ts +3 -9
- package/{dist/lib/modal → modal}/modal.js +18 -14
- package/package.json +14 -28
- package/{dist/lib/pagination.d.ts → pagination.d.ts} +4 -12
- package/{dist/lib/pagination.js → pagination.js} +1 -1
- package/progressbar.d.ts +87 -0
- package/progressbar.js +78 -0
- package/{dist/lib/rating.d.ts → rating.d.ts} +5 -11
- package/{dist/lib/select.d.ts → select.d.ts} +2 -5
- package/{dist/lib/services → services}/checks.d.ts +5 -0
- package/{dist/lib/services → services}/checks.js +5 -0
- package/{dist/lib/services → services}/index.d.ts +2 -0
- package/{dist/lib/services → services}/index.js +2 -0
- package/services/intersection.d.ts +26 -0
- package/services/intersection.js +47 -0
- package/services/navManager.d.ts +5 -0
- package/services/navManager.js +52 -0
- package/services/sortUtils.d.ts +2 -0
- package/services/sortUtils.js +14 -0
- package/{dist/lib/services → services}/stores.d.ts +60 -23
- package/{dist/lib/services → services}/stores.js +92 -53
- package/{dist/lib/services → services}/writables.d.ts +2 -1
- package/{dist/lib/services → services}/writables.js +15 -1
- package/dist/lib/tsdoc-metadata.json +0 -11
- /package/{dist/lib/config.js → config.js} +0 -0
- /package/{dist/lib/modal → modal}/scrollbars.d.ts +0 -0
- /package/{dist/lib/modal → modal}/scrollbars.js +0 -0
- /package/{dist/lib/pagination.utils.d.ts → pagination.utils.d.ts} +0 -0
- /package/{dist/lib/pagination.utils.js → pagination.utils.js} +0 -0
- /package/{dist/lib/rating.js → rating.js} +0 -0
- /package/{dist/lib/select.js → select.js} +0 -0
- /package/{dist/lib/services → services}/directiveUtils.d.ts +0 -0
- /package/{dist/lib/services → services}/directiveUtils.js +0 -0
- /package/{dist/lib/services → services}/focustrack.d.ts +0 -0
- /package/{dist/lib/services → services}/focustrack.js +0 -0
- /package/{dist/lib/services → services}/portal.d.ts +0 -0
- /package/{dist/lib/services → services}/portal.js +0 -0
- /package/{dist/lib/services → services}/siblingsInert.d.ts +0 -0
- /package/{dist/lib/services → services}/siblingsInert.js +0 -0
- /package/{dist/lib/transitions → transitions}/baseTransitions.d.ts +0 -0
- /package/{dist/lib/transitions → transitions}/baseTransitions.js +0 -0
- /package/{dist/lib/transitions → transitions}/bootstrap/collapse.d.ts +0 -0
- /package/{dist/lib/transitions → transitions}/bootstrap/collapse.js +0 -0
- /package/{dist/lib/transitions → transitions}/bootstrap/fade.d.ts +0 -0
- /package/{dist/lib/transitions → transitions}/bootstrap/fade.js +0 -0
- /package/{dist/lib/transitions → transitions}/bootstrap/index.d.ts +0 -0
- /package/{dist/lib/transitions → transitions}/bootstrap/index.js +0 -0
- /package/{dist/lib/transitions → transitions}/collapse.d.ts +0 -0
- /package/{dist/lib/transitions → transitions}/collapse.js +0 -0
- /package/{dist/lib/transitions → transitions}/cssTransitions.d.ts +0 -0
- /package/{dist/lib/transitions → transitions}/cssTransitions.js +0 -0
- /package/{dist/lib/transitions → transitions}/index.d.ts +0 -0
- /package/{dist/lib/transitions → transitions}/index.js +0 -0
- /package/{dist/lib/transitions → transitions}/simpleClassTransition.d.ts +0 -0
- /package/{dist/lib/transitions → transitions}/simpleClassTransition.js +0 -0
- /package/{dist/lib/transitions → transitions}/utils.d.ts +0 -0
- /package/{dist/lib/transitions → transitions}/utils.js +0 -0
- /package/{dist/lib/types.d.ts → types.d.ts} +0 -0
- /package/{dist/lib/types.js → types.js} +0 -0
- /package/{dist/lib/utils.d.ts → utils.d.ts} +0 -0
- /package/{dist/lib/utils.js → utils.js} +0 -0
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import type { ReadableSignal,
|
|
2
|
-
export type ToWritableSignal<P
|
|
3
|
-
[K in keyof P
|
|
1
|
+
import type { ReadableSignal, StoreInput, StoreOptions, StoresInputValues, WritableSignal } from '@amadeus-it-group/tansu';
|
|
2
|
+
export type ToWritableSignal<P> = {
|
|
3
|
+
[K in keyof P as `${K & string}$`]-?: WritableSignal<P[K], P[K] | undefined>;
|
|
4
4
|
};
|
|
5
|
-
export type
|
|
6
|
-
[K in keyof T]?: ReadableSignal<T[K]
|
|
5
|
+
export type ReadableSignals<T extends object> = {
|
|
6
|
+
[K in keyof T]?: ReadableSignal<T[K] | undefined>;
|
|
7
|
+
};
|
|
8
|
+
export type ValuesOrReadableSignals<T extends object> = {
|
|
9
|
+
[K in keyof T]?: ReadableSignal<T[K] | undefined> | T[K];
|
|
10
|
+
};
|
|
11
|
+
export type ValuesOrWritableSignals<T extends object> = {
|
|
12
|
+
[K in keyof T]?: WritableSignal<T[K] | undefined> | T[K];
|
|
7
13
|
};
|
|
8
14
|
export type WithoutDollar<S extends `${string}$`> = S extends `${infer U}$` ? U : never;
|
|
9
15
|
export type ValueOfStore<S extends ReadableSignal<any>> = S extends ReadableSignal<infer U> ? U : never;
|
|
@@ -32,7 +38,7 @@ export type ToState<S extends {
|
|
|
32
38
|
* @param stores - object of stores
|
|
33
39
|
* @returns the patch function
|
|
34
40
|
*/
|
|
35
|
-
export declare function createPatch<T extends object
|
|
41
|
+
export declare function createPatch<T extends object>(stores: ToWritableSignal<T>): <U extends Partial<T>>(storesValues?: void | U | undefined) => void;
|
|
36
42
|
/**
|
|
37
43
|
* This utility function is designed to compare the first level of two objects.
|
|
38
44
|
*
|
|
@@ -46,12 +52,12 @@ export declare function createPatch<T extends object, V extends object = T>(stor
|
|
|
46
52
|
*/
|
|
47
53
|
export declare function findChangedProperties<T extends Record<string, any>>(obj1: Partial<T>, obj2: Partial<T>): Partial<T> | null;
|
|
48
54
|
export declare const INVALID_VALUE: unique symbol;
|
|
49
|
-
export type NormalizeValue<T
|
|
50
|
-
export interface WritableWithDefaultOptions<T
|
|
55
|
+
export type NormalizeValue<T> = (value: T) => T | typeof INVALID_VALUE;
|
|
56
|
+
export interface WritableWithDefaultOptions<T> {
|
|
51
57
|
/**
|
|
52
58
|
* the normalize value function. should return the invalidValue symbol when the provided value is invalid
|
|
53
59
|
*/
|
|
54
|
-
normalizeValue?: NormalizeValue<T
|
|
60
|
+
normalizeValue?: NormalizeValue<T>;
|
|
55
61
|
/**
|
|
56
62
|
* the equal function, allowing to compare two values. used to check if a previous and current values are equals.
|
|
57
63
|
*/
|
|
@@ -66,14 +72,35 @@ export interface WritableWithDefaultOptions<T, U = T> {
|
|
|
66
72
|
* `set` or `update` functions), or the `defValue` is used instead (if the invalid value comes from the `config$` store).
|
|
67
73
|
*
|
|
68
74
|
* @param defValue - Default value used when both the own value and the config$ value are undefined.
|
|
69
|
-
* @param config$ -
|
|
75
|
+
* @param config$ - Store containing the default value used when the own value is undefined
|
|
70
76
|
* @param options - Object which can contain the following optional functions: normalizeValue and equal
|
|
77
|
+
* @param own$ - Store containing the own value
|
|
71
78
|
* @returns a writable store with the extra default value and normalization logic described above
|
|
72
79
|
*/
|
|
73
|
-
export declare function writableWithDefault<T
|
|
74
|
-
export type ConfigValidator<T extends object
|
|
75
|
-
[K in keyof T
|
|
80
|
+
export declare function writableWithDefault<T>(defValue: T, config$?: ReadableSignal<T | undefined>, options?: WritableWithDefaultOptions<T>, own$?: WritableSignal<T | undefined>): WritableSignal<T, T | undefined>;
|
|
81
|
+
export type ConfigValidator<T extends object> = {
|
|
82
|
+
[K in keyof T]?: WritableWithDefaultOptions<T[K]>;
|
|
76
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* Returns true if the provided argument is a store (ReadableSignal).
|
|
86
|
+
* @param x - argument that is tested
|
|
87
|
+
* @returns true if the argument is a store (ReadableSignal)
|
|
88
|
+
*/
|
|
89
|
+
export declare const isStore: (x: any) => x is ReadableSignal<any>;
|
|
90
|
+
/**
|
|
91
|
+
* If the provided argument is already a store, it is returned as is, otherwise, a readable store is created with the provided argument as its initial value.
|
|
92
|
+
* @param x - either a store or a simple value
|
|
93
|
+
* @returns either x if x is already a store, or readable(x) otherwise
|
|
94
|
+
*/
|
|
95
|
+
export declare const toReadableStore: <T>(x: T | ReadableSignal<T>) => ReadableSignal<T>;
|
|
96
|
+
/**
|
|
97
|
+
* If the provided argument is already a store, it is returned as is, otherwise, a writable store is created with the provided argument as its initial value.
|
|
98
|
+
* @param x - either a writable store or a simple value
|
|
99
|
+
* @returns either x if x is already a store, or writable(x) otherwise
|
|
100
|
+
*/
|
|
101
|
+
export declare const toWritableStore: <T>(x: T | WritableSignal<T, T>) => WritableSignal<T, T>;
|
|
102
|
+
export declare const normalizeConfigStores: <T extends object>(keys: (keyof T)[], config?: ReadableSignal<Partial<T>> | ValuesOrReadableSignals<T> | undefined) => ReadableSignals<T>;
|
|
103
|
+
export declare const mergeConfigStores: <T extends object>(keys: (keyof T)[], config1?: ReadableSignals<T> | undefined, config2?: ReadableSignals<T> | undefined) => ReadableSignals<T>;
|
|
77
104
|
/**
|
|
78
105
|
* Returns an object containing, for each property of `defConfig`, a corresponding writable with the normalization and default value logic
|
|
79
106
|
* described in {@link writableWithDefault}. Keys in the returned object are the same as the ones present in `defConfig`,
|
|
@@ -81,8 +108,7 @@ export type ConfigValidator<T extends object, U extends object = T> = {
|
|
|
81
108
|
*
|
|
82
109
|
* @param defConfig - object containing, for each property, a default value to use in case `config$` does not provide the suitable default
|
|
83
110
|
* value for that property
|
|
84
|
-
* @param
|
|
85
|
-
* for each property of `defConfig` either a store containing the default value or the default value itself
|
|
111
|
+
* @param propsConfig - object defining the config and props
|
|
86
112
|
* @param options - object containing, for each property of `defConfig`, an optional object with the following optional functions: normalizeValue and equal
|
|
87
113
|
* @returns an object containing writables
|
|
88
114
|
*
|
|
@@ -90,8 +116,8 @@ export type ConfigValidator<T extends object, U extends object = T> = {
|
|
|
90
116
|
* ```ts
|
|
91
117
|
* const defConfig = {propA: 1};
|
|
92
118
|
* const validation = {propA: {normalizeValue: value => +value}};
|
|
93
|
-
* const config
|
|
94
|
-
* const {propA$} = writablesWithDefault(defConfig, config
|
|
119
|
+
* const config = writable({propA: 5});
|
|
120
|
+
* const {propA$} = writablesWithDefault(defConfig, {config}, validation);
|
|
95
121
|
* ```
|
|
96
122
|
*
|
|
97
123
|
* @example With an object containing a value and a store
|
|
@@ -99,15 +125,15 @@ export type ConfigValidator<T extends object, U extends object = T> = {
|
|
|
99
125
|
* const defConfig = {propA: 1, propB: 2};
|
|
100
126
|
* const validation = {propA: {normalizeValue: value => +value}};
|
|
101
127
|
* const config = {propA: 5, propB: writable(3)};
|
|
102
|
-
* const {propA$, propB$} = writablesWithDefault(defConfig, config, validation);
|
|
128
|
+
* const {propA$, propB$} = writablesWithDefault(defConfig, {config}, validation);
|
|
103
129
|
* ```
|
|
104
130
|
*/
|
|
105
|
-
export declare const writablesWithDefault: <T extends object
|
|
131
|
+
export declare const writablesWithDefault: <T extends object>(defConfig: T, propsConfig?: PropsConfig<T> | undefined, options?: ConfigValidator<T> | undefined) => ToWritableSignal<T>;
|
|
106
132
|
/**
|
|
107
133
|
* Shortcut for calling both {@link writablesWithDefault} and {@link createPatch} in one call.
|
|
108
134
|
* @param defConfig - object containing, for each property, a default value to use in case `config` does not provide the suitable default
|
|
109
135
|
* value for that property
|
|
110
|
-
* @param
|
|
136
|
+
* @param propsConfig - either a store of objects containing, for each property of `defConfig`, the default value or an object containing
|
|
111
137
|
* for each property of `defConfig` either a store containing the default value or the default value itself
|
|
112
138
|
* @param options - object containing, for each property of `defConfig`, an optional object with the following optional functions: normalizeValue and equal
|
|
113
139
|
* @returns an array with two items: the first one containing the writables (returned by {@link writablesWithDefault}),
|
|
@@ -129,12 +155,23 @@ export declare const writablesWithDefault: <T extends object, U extends object =
|
|
|
129
155
|
* const [{propA$, propB$}, patch] = writablesForProps(defConfig, config, validation);
|
|
130
156
|
* ```
|
|
131
157
|
*/
|
|
132
|
-
export declare const writablesForProps: <T extends object
|
|
133
|
-
export
|
|
158
|
+
export declare const writablesForProps: <T extends object>(defConfig: T, propsConfig?: PropsConfig<T> | undefined, options?: { [K in keyof T]?: WritableWithDefaultOptions<T[K]> | undefined; } | undefined) => [ToWritableSignal<T>, <U extends Partial<T>>(storesValues?: void | U | undefined) => void];
|
|
159
|
+
export interface PropsConfig<U extends object> {
|
|
160
|
+
/**
|
|
161
|
+
* Object containing, for each property, either its initial value, or a store that will contain the value at any time.
|
|
162
|
+
* When the value of a property is undefined or invalid, the value from the config is used.
|
|
163
|
+
*/
|
|
164
|
+
props?: ValuesOrWritableSignals<U>;
|
|
165
|
+
/**
|
|
166
|
+
* Either a store of objects containing, for each property, the default value,
|
|
167
|
+
* or an object containing, for each property, either a store containing the default value or the default value itself.
|
|
168
|
+
*/
|
|
169
|
+
config?: ReadableSignal<Partial<U>> | ValuesOrReadableSignals<Partial<U>>;
|
|
170
|
+
}
|
|
134
171
|
export declare const stateStores: <A extends {
|
|
135
172
|
[x: `${string}$`]: ReadableSignal<any>;
|
|
136
173
|
}>(inputStores: A) => {
|
|
137
174
|
state$: ReadableSignal<ToState<A>>;
|
|
138
175
|
stores: { [key in `${string}$` & keyof A]: ReadableSignal<ValueOfStore<A[key]>>; };
|
|
139
176
|
};
|
|
140
|
-
export declare const bindableDerived: <T, U extends [WritableSignal<T, T>, ...StoreInput<any>[]]>(onChange$: ReadableSignal<(value: T) => void>, stores: U, adjustValue: (arg: StoresInputValues<U>) => T) => ReadableSignal<T>;
|
|
177
|
+
export declare const bindableDerived: <T, U extends [WritableSignal<T, T>, ...StoreInput<any>[]]>(onChange$: ReadableSignal<(value: T) => void>, stores: U, adjustValue: (arg: StoresInputValues<U>) => T, equal?: (currentValue: T, newValue: T) => boolean) => ReadableSignal<T>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { batch, computed, derived, get, readable, writable
|
|
1
|
+
import { asReadable, batch, computed, derived, get, readable, writable } from '@amadeus-it-group/tansu';
|
|
2
2
|
import { identity } from '../utils';
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
@@ -60,7 +60,6 @@ const update = function (updater) {
|
|
|
60
60
|
this.set(updater(this()));
|
|
61
61
|
};
|
|
62
62
|
export const INVALID_VALUE = Symbol();
|
|
63
|
-
/* eslint-disable jsdoc/check-param-names, jsdoc/require-param */
|
|
64
63
|
/**
|
|
65
64
|
* Returns a writable store whose value is either its own value (when it is not undefined) or a default value
|
|
66
65
|
* that comes either from the `config$` store (when it is not undefined) or from `defValue`.
|
|
@@ -70,41 +69,81 @@ export const INVALID_VALUE = Symbol();
|
|
|
70
69
|
* `set` or `update` functions), or the `defValue` is used instead (if the invalid value comes from the `config$` store).
|
|
71
70
|
*
|
|
72
71
|
* @param defValue - Default value used when both the own value and the config$ value are undefined.
|
|
73
|
-
* @param config$ -
|
|
72
|
+
* @param config$ - Store containing the default value used when the own value is undefined
|
|
74
73
|
* @param options - Object which can contain the following optional functions: normalizeValue and equal
|
|
74
|
+
* @param own$ - Store containing the own value
|
|
75
75
|
* @returns a writable store with the extra default value and normalization logic described above
|
|
76
76
|
*/
|
|
77
|
-
export function writableWithDefault(defValue, config$ = readable(undefined),
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
77
|
+
export function writableWithDefault(defValue, config$ = readable(undefined), options = {}, own$ = writable(undefined)) {
|
|
78
|
+
const { normalizeValue = identity, equal = Object.is } = options;
|
|
79
|
+
const getDefValue = () => defValue;
|
|
80
|
+
const callNormalizeValue = (value, defValue = getDefValue) => {
|
|
81
81
|
const normalizedValue = value === undefined ? undefined : normalizeValue(value);
|
|
82
82
|
if (normalizedValue === INVALID_VALUE) {
|
|
83
|
-
console.error('Not using invalid value
|
|
84
|
-
return defValue;
|
|
83
|
+
console.error('Not using invalid value', value);
|
|
84
|
+
return defValue();
|
|
85
85
|
}
|
|
86
86
|
if (normalizedValue === undefined) {
|
|
87
|
-
return defValue;
|
|
87
|
+
return defValue();
|
|
88
88
|
}
|
|
89
89
|
return normalizedValue;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}, { equal }), {
|
|
90
|
+
};
|
|
91
|
+
const validatedDefConfig$ = computed(() => callNormalizeValue(config$()), { equal });
|
|
92
|
+
const validatedOwnValue$ = computed(() => callNormalizeValue(own$(), validatedDefConfig$), { equal });
|
|
93
|
+
return asReadable(validatedOwnValue$, {
|
|
95
94
|
set(value) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
95
|
+
if (value !== undefined) {
|
|
96
|
+
const normalizedValue = normalizeValue(value);
|
|
97
|
+
if (normalizedValue === INVALID_VALUE) {
|
|
98
|
+
console.error('Not setting invalid value', value);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
value = normalizedValue;
|
|
102
102
|
}
|
|
103
|
+
own$.set(value);
|
|
103
104
|
},
|
|
104
105
|
update,
|
|
105
106
|
});
|
|
106
107
|
}
|
|
107
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Returns true if the provided argument is a store (ReadableSignal).
|
|
110
|
+
* @param x - argument that is tested
|
|
111
|
+
* @returns true if the argument is a store (ReadableSignal)
|
|
112
|
+
*/
|
|
113
|
+
export const isStore = (x) => !!(x && typeof x === 'function' && 'subscribe' in x);
|
|
114
|
+
/**
|
|
115
|
+
* If the provided argument is already a store, it is returned as is, otherwise, a readable store is created with the provided argument as its initial value.
|
|
116
|
+
* @param x - either a store or a simple value
|
|
117
|
+
* @returns either x if x is already a store, or readable(x) otherwise
|
|
118
|
+
*/
|
|
119
|
+
export const toReadableStore = (x) => (isStore(x) ? x : readable(x));
|
|
120
|
+
/**
|
|
121
|
+
* If the provided argument is already a store, it is returned as is, otherwise, a writable store is created with the provided argument as its initial value.
|
|
122
|
+
* @param x - either a writable store or a simple value
|
|
123
|
+
* @returns either x if x is already a store, or writable(x) otherwise
|
|
124
|
+
*/
|
|
125
|
+
export const toWritableStore = (x) => (isStore(x) ? x : writable(x));
|
|
126
|
+
export const normalizeConfigStores = (keys, config) => {
|
|
127
|
+
const res = {};
|
|
128
|
+
if (config) {
|
|
129
|
+
const configIsStore = isStore(config);
|
|
130
|
+
for (const key of keys) {
|
|
131
|
+
res[key] = configIsStore
|
|
132
|
+
? computed(() => config()[key])
|
|
133
|
+
: toReadableStore(config[key]);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return res;
|
|
137
|
+
};
|
|
138
|
+
export const mergeConfigStores = (keys, config1, config2) => {
|
|
139
|
+
const res = {};
|
|
140
|
+
for (const key of keys) {
|
|
141
|
+
const config1Store = config1?.[key];
|
|
142
|
+
const config2Store = config2?.[key];
|
|
143
|
+
res[key] = config1Store && config2Store ? computed(() => config1Store() ?? config2Store()) : config1Store || config2Store;
|
|
144
|
+
}
|
|
145
|
+
return res;
|
|
146
|
+
};
|
|
108
147
|
/**
|
|
109
148
|
* Returns an object containing, for each property of `defConfig`, a corresponding writable with the normalization and default value logic
|
|
110
149
|
* described in {@link writableWithDefault}. Keys in the returned object are the same as the ones present in `defConfig`,
|
|
@@ -112,8 +151,7 @@ const isStore = (x) => !!(x && typeof x === 'function' && 'subscribe' in x);
|
|
|
112
151
|
*
|
|
113
152
|
* @param defConfig - object containing, for each property, a default value to use in case `config$` does not provide the suitable default
|
|
114
153
|
* value for that property
|
|
115
|
-
* @param
|
|
116
|
-
* for each property of `defConfig` either a store containing the default value or the default value itself
|
|
154
|
+
* @param propsConfig - object defining the config and props
|
|
117
155
|
* @param options - object containing, for each property of `defConfig`, an optional object with the following optional functions: normalizeValue and equal
|
|
118
156
|
* @returns an object containing writables
|
|
119
157
|
*
|
|
@@ -121,8 +159,8 @@ const isStore = (x) => !!(x && typeof x === 'function' && 'subscribe' in x);
|
|
|
121
159
|
* ```ts
|
|
122
160
|
* const defConfig = {propA: 1};
|
|
123
161
|
* const validation = {propA: {normalizeValue: value => +value}};
|
|
124
|
-
* const config
|
|
125
|
-
* const {propA$} = writablesWithDefault(defConfig, config
|
|
162
|
+
* const config = writable({propA: 5});
|
|
163
|
+
* const {propA$} = writablesWithDefault(defConfig, {config}, validation);
|
|
126
164
|
* ```
|
|
127
165
|
*
|
|
128
166
|
* @example With an object containing a value and a store
|
|
@@ -130,19 +168,17 @@ const isStore = (x) => !!(x && typeof x === 'function' && 'subscribe' in x);
|
|
|
130
168
|
* const defConfig = {propA: 1, propB: 2};
|
|
131
169
|
* const validation = {propA: {normalizeValue: value => +value}};
|
|
132
170
|
* const config = {propA: 5, propB: writable(3)};
|
|
133
|
-
* const {propA$, propB$} = writablesWithDefault(defConfig, config, validation);
|
|
171
|
+
* const {propA$, propB$} = writablesWithDefault(defConfig, {config}, validation);
|
|
134
172
|
* ```
|
|
135
173
|
*/
|
|
136
|
-
export const writablesWithDefault = (defConfig,
|
|
174
|
+
export const writablesWithDefault = (defConfig, propsConfig, options) => {
|
|
137
175
|
const res = {};
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
res[`${key}$`] = writableWithDefault(defConfig[key], store, options?.[key]);
|
|
176
|
+
const keys = Object.keys(defConfig);
|
|
177
|
+
const configStores = normalizeConfigStores(keys, propsConfig?.config);
|
|
178
|
+
const props = propsConfig?.props;
|
|
179
|
+
for (const key of keys) {
|
|
180
|
+
const propValue = props?.[key];
|
|
181
|
+
res[`${key}$`] = writableWithDefault(defConfig[key], configStores[key], options?.[key], toWritableStore(propValue));
|
|
146
182
|
}
|
|
147
183
|
return res;
|
|
148
184
|
};
|
|
@@ -150,7 +186,7 @@ export const writablesWithDefault = (defConfig, config, options) => {
|
|
|
150
186
|
* Shortcut for calling both {@link writablesWithDefault} and {@link createPatch} in one call.
|
|
151
187
|
* @param defConfig - object containing, for each property, a default value to use in case `config` does not provide the suitable default
|
|
152
188
|
* value for that property
|
|
153
|
-
* @param
|
|
189
|
+
* @param propsConfig - either a store of objects containing, for each property of `defConfig`, the default value or an object containing
|
|
154
190
|
* for each property of `defConfig` either a store containing the default value or the default value itself
|
|
155
191
|
* @param options - object containing, for each property of `defConfig`, an optional object with the following optional functions: normalizeValue and equal
|
|
156
192
|
* @returns an array with two items: the first one containing the writables (returned by {@link writablesWithDefault}),
|
|
@@ -172,8 +208,8 @@ export const writablesWithDefault = (defConfig, config, options) => {
|
|
|
172
208
|
* const [{propA$, propB$}, patch] = writablesForProps(defConfig, config, validation);
|
|
173
209
|
* ```
|
|
174
210
|
*/
|
|
175
|
-
export const writablesForProps = (defConfig,
|
|
176
|
-
const stores = writablesWithDefault(defConfig,
|
|
211
|
+
export const writablesForProps = (defConfig, propsConfig, options) => {
|
|
212
|
+
const stores = writablesWithDefault(defConfig, propsConfig, options);
|
|
177
213
|
return [stores, createPatch(stores)];
|
|
178
214
|
};
|
|
179
215
|
export const stateStores = (inputStores) => {
|
|
@@ -200,20 +236,23 @@ export const stateStores = (inputStores) => {
|
|
|
200
236
|
}),
|
|
201
237
|
};
|
|
202
238
|
};
|
|
203
|
-
export const bindableDerived = (onChange$, stores, adjustValue) => {
|
|
239
|
+
export const bindableDerived = (onChange$, stores, adjustValue, equal = (currentValue, newValue) => newValue === currentValue) => {
|
|
204
240
|
let currentValue = stores[0]();
|
|
205
|
-
return derived(stores,
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
currentValue
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
241
|
+
return derived(stores, {
|
|
242
|
+
derive(values) {
|
|
243
|
+
const newValue = adjustValue(values);
|
|
244
|
+
const rectifiedValue = !equal(values[0], newValue);
|
|
245
|
+
if (rectifiedValue) {
|
|
246
|
+
stores[0].set(newValue);
|
|
247
|
+
}
|
|
248
|
+
if (rectifiedValue || !equal(currentValue, newValue)) {
|
|
249
|
+
currentValue = newValue;
|
|
250
|
+
// TODO check if we should do this async to avoid issue
|
|
251
|
+
// with angular and react only when rectifiedValue is true?
|
|
252
|
+
onChange$()(newValue);
|
|
253
|
+
}
|
|
254
|
+
return newValue;
|
|
255
|
+
},
|
|
256
|
+
equal,
|
|
218
257
|
});
|
|
219
258
|
};
|
|
@@ -3,5 +3,6 @@ import { INVALID_VALUE } from './stores';
|
|
|
3
3
|
export declare const testToNormalizeValue: <T>(filter: (value: T) => boolean) => (value: T) => typeof INVALID_VALUE | T;
|
|
4
4
|
export declare const typeNumber: WritableWithDefaultOptions<number>;
|
|
5
5
|
export declare const typeBoolean: WritableWithDefaultOptions<boolean>;
|
|
6
|
-
export declare const typeString: WritableWithDefaultOptions<string
|
|
6
|
+
export declare const typeString: WritableWithDefaultOptions<string>;
|
|
7
7
|
export declare const typeFunction: WritableWithDefaultOptions<(...args: any[]) => any>;
|
|
8
|
+
export declare const typeArray: WritableWithDefaultOptions<any[]>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isBoolean, isFunction, isNumber, isString } from './checks';
|
|
1
|
+
import { isArray, isBoolean, isFunction, isNumber, isString } from './checks';
|
|
2
2
|
import { INVALID_VALUE } from './stores';
|
|
3
3
|
export const testToNormalizeValue = (filter) => (value) => filter(value) ? value : INVALID_VALUE;
|
|
4
4
|
export const typeNumber = {
|
|
@@ -14,3 +14,17 @@ export const typeFunction = {
|
|
|
14
14
|
normalizeValue: testToNormalizeValue(isFunction),
|
|
15
15
|
equal: Object.is,
|
|
16
16
|
};
|
|
17
|
+
export const typeArray = {
|
|
18
|
+
normalizeValue: testToNormalizeValue(isArray),
|
|
19
|
+
equal: (a, b) => {
|
|
20
|
+
if (a === b) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
if (a?.length !== b?.length) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return a.every((val, index) => val === b[index]);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
|
2
|
-
// It should be published with your NPM package. It should not be tracked by Git.
|
|
3
|
-
{
|
|
4
|
-
"tsdocVersion": "0.12",
|
|
5
|
-
"toolPackages": [
|
|
6
|
-
{
|
|
7
|
-
"packageName": "@microsoft/api-extractor",
|
|
8
|
-
"packageVersion": "7.36.4"
|
|
9
|
-
}
|
|
10
|
-
]
|
|
11
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|