@agnos-ui/svelte-headless 0.8.1 → 0.9.0

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/config.d.ts CHANGED
@@ -1,17 +1,7 @@
1
- import type { Partial2Levels, WidgetsConfigStore } from '@agnos-ui/core/config';
1
+ import type { WidgetsConfig, Partial2Levels, WidgetsConfigStore } from '@agnos-ui/core/config';
2
2
  import type { ReadableSignal } from '@amadeus-it-group/tansu';
3
- import type { Widget, WidgetProps, WidgetFactory } from './types';
3
+ import type { Widget, WidgetFactory, WidgetProps } from '@agnos-ui/core/types';
4
4
  export * from '@agnos-ui/core/config';
5
- type WidgetFactoryInput<Config extends {
6
- [widgetName: string]: object;
7
- }, W extends Widget> = {
8
- factory: WidgetFactory<W>;
9
- widgetName?: null | keyof Config;
10
- defaultConfig?: Partial<WidgetProps<W>> | ReadableSignal<Partial<WidgetProps<W>> | undefined>;
11
- events?: Partial<Pick<WidgetProps<W>, keyof WidgetProps<W> & `on${string}Change`>>;
12
- props?: Partial<WidgetProps<W>>;
13
- enablePatchChanged?: true;
14
- };
15
5
  type AdaptParentConfig<Config> = (config: Partial2Levels<Config>) => Partial2Levels<Config>;
16
6
  type CreateWidgetsDefaultConfig<Config extends {
17
7
  [widgetName: string]: object;
@@ -34,6 +24,14 @@ export declare const widgetsConfigFactory: <Config extends {
34
24
  widgetsDefaultConfigKey: symbol;
35
25
  createWidgetsDefaultConfig: CreateWidgetsDefaultConfig<Config>;
36
26
  getContextWidgetConfig: <N extends keyof Config>(widgetName: N) => ReadableSignal<Partial<Config[N]> | undefined>;
37
- callWidgetFactory: <W extends Widget>(input: WidgetFactoryInput<Config, W>) => WidgetSlotContext<W_1>;
27
+ callWidgetFactory: <W extends Widget>(factory: WidgetFactory<W>, options?: {
28
+ defaultConfig?: Partial<WidgetProps<W>> | ReadableSignal<Partial<WidgetProps<W>> | undefined>;
29
+ events?: Partial<Pick<WidgetProps<W>, keyof WidgetProps<W> & `on${string}Change`>>;
30
+ props?: Partial<WidgetProps<W>>;
31
+ }) => WidgetSlotContext<W_1>;
38
32
  };
39
- export declare const widgetsDefaultConfigKey: symbol, createWidgetsDefaultConfig: CreateWidgetsDefaultConfig<WidgetsConfig>, getContextWidgetConfig: <N extends string | number | symbol>(widgetName: N) => ReadableSignal<any>, callWidgetFactory: <W extends Widget>(input: WidgetFactoryInput<WidgetsConfig, W>) => WidgetSlotContext<W_1>;
33
+ export declare const widgetsDefaultConfigKey: symbol, createWidgetsDefaultConfig: CreateWidgetsDefaultConfig<WidgetsConfig>, getContextWidgetConfig: <N extends string | number | symbol>(widgetName: N) => ReadableSignal<any>, callWidgetFactory: <W extends Widget>(factory: WidgetFactory<W>, options?: {
34
+ defaultConfig?: Partial<WidgetProps<W>> | ReadableSignal<Partial<WidgetProps<W>> | undefined>;
35
+ events?: Partial<Pick<WidgetProps<W>, keyof WidgetProps<W> & `on${string}Change`>>;
36
+ props?: Partial<WidgetProps<W>>;
37
+ } | undefined) => WidgetSlotContext<W_1>;
package/config.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createWidgetsConfig } from '@agnos-ui/core/config';
2
+ import { FACTORY_WIDGET_NAME } from '@agnos-ui/core/types';
2
3
  import { computed } from '@amadeus-it-group/tansu';
3
4
  import { getContext, setContext } from 'svelte';
4
5
  import { callWidgetFactoryWithConfig } from './utils/widget.svelte';
@@ -57,19 +58,33 @@ export const widgetsConfigFactory = (widgetsDefaultConfigKey = Symbol('widgetsCo
57
58
  setContext(widgetsDefaultConfigKey, child$);
58
59
  return child$;
59
60
  };
61
+ /**
62
+ * Retrieves a widgets configuration store from the Svelte context hierarchy.
63
+ *
64
+ * @param widgetName - the name of the widget
65
+ * @returns the widgets configuration store
66
+ */
60
67
  const getContextWidgetConfig = (widgetName) => {
61
68
  const widgetsConfig = getContext(widgetsDefaultConfigKey);
62
69
  return computed(() => widgetsConfig?.()[widgetName]);
63
70
  };
64
- const callWidgetFactory = (input) => callWidgetFactoryWithConfig({
65
- factory: input.factory,
66
- defaultConfig: input.defaultConfig,
67
- widgetConfig: input.widgetName ? getContextWidgetConfig(input.widgetName) : null,
68
- events: input.events,
71
+ /**
72
+ * Creates and initializes a widget using the provided factory and configuration options.
73
+ *
74
+ * @param factory - the widget factory
75
+ * @param options - the optional options
76
+ * @param options.defaultConfig - the default configuration for the widget
77
+ * @param options.events - the events to be passed to the widget
78
+ * @param options.props - the props to be passed to the widget
79
+ * @returns the state, api and directives to track and interact with the widget
80
+ */
81
+ const callWidgetFactory = (factory, options) => callWidgetFactoryWithConfig(factory, {
82
+ defaultConfig: options?.defaultConfig,
83
+ widgetConfig: factory[FACTORY_WIDGET_NAME] ? getContextWidgetConfig(factory[FACTORY_WIDGET_NAME]) : null,
84
+ events: options?.events,
69
85
  get props() {
70
- return input.props;
86
+ return options?.props;
71
87
  },
72
- enablePatchChanged: input.enablePatchChanged,
73
88
  });
74
89
  return {
75
90
  /**
@@ -13,6 +13,7 @@ export * from './services/transitions/cssTransitions';
13
13
  export * from './services/transitions/collapse';
14
14
  export * from './services/transitions/baseTransitions';
15
15
  export * from './utils/writables';
16
+ export * from './utils/widget';
16
17
  export * from './utils/stores';
17
18
  export * from './utils/func';
18
19
  export * from './utils/directive';
@@ -13,6 +13,7 @@ export * from './services/transitions/cssTransitions';
13
13
  export * from './services/transitions/collapse';
14
14
  export * from './services/transitions/baseTransitions';
15
15
  export * from './utils/writables';
16
+ export * from './utils/widget';
16
17
  export * from './utils/stores';
17
18
  export * from './utils/func';
18
19
  export * from './utils/directive';
@@ -0,0 +1 @@
1
+ export * from '@agnos-ui/core/utils/widget';
@@ -0,0 +1 @@
1
+ export * from '@agnos-ui/core/utils/widget';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agnos-ui/svelte-headless",
3
3
  "description": "Headless component library for Svelte.",
4
- "version": "0.8.1",
4
+ "version": "0.9.0",
5
5
  "type": "module",
6
6
  "main": "./index.js",
7
7
  "module": "./index.js",
@@ -49,7 +49,7 @@
49
49
  }
50
50
  },
51
51
  "dependencies": {
52
- "@agnos-ui/core": "0.8.1"
52
+ "@agnos-ui/core": "0.9.0"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "@amadeus-it-group/tansu": "^2.0.0",
@@ -3,20 +3,17 @@ import type { Widget, WidgetFactory, WidgetProps, WidgetSlotContext } from '../t
3
3
  /**
4
4
  * Call a widget factory using provided configs.
5
5
  *
6
- * @param parameter - the parameter
7
- * @param parameter.factory - the widget factory to call
8
- * @param parameter.defaultConfig - the default config of the widget
9
- * @param parameter.widgetConfig - the config of the widget, overriding the defaultConfig
10
- * @param parameter.events - the events of the widget
11
- * @param parameter.props - the props of the widget
12
- * @param parameter.enablePatchChanged - enable patching changed props
13
- * @returns the widget
6
+ * @param factory - the widget factory to call
7
+ * @param options - the optional options
8
+ * @param options.defaultConfig - the default config of the widget
9
+ * @param options.widgetConfig - the config of the widget, overriding the defaultConfig
10
+ * @param options.events - the events of the widget
11
+ * @param options.props - the props of the widget
12
+ * @returns the state, api and directives to track and interact with the widget
14
13
  */
15
- export declare const callWidgetFactoryWithConfig: <W extends Widget>(parameter: {
16
- factory: WidgetFactory<W>;
14
+ export declare const callWidgetFactoryWithConfig: <W extends Widget>(factory: WidgetFactory<W>, options?: {
17
15
  defaultConfig?: Partial<WidgetProps<W>> | ReadableSignal<Partial<WidgetProps<W>> | undefined>;
18
16
  widgetConfig?: null | undefined | ReadableSignal<Partial<WidgetProps<W>> | undefined>;
19
17
  events?: Partial<Pick<WidgetProps<W>, keyof WidgetProps<W> & `on${string}Change`>>;
20
18
  props?: Partial<WidgetProps<W>>;
21
- enablePatchChanged?: true;
22
19
  }) => WidgetSlotContext<W>;
@@ -39,18 +39,17 @@ const eventStore = (event, prop) => {
39
39
  /**
40
40
  * Call a widget factory using provided configs.
41
41
  *
42
- * @param parameter - the parameter
43
- * @param parameter.factory - the widget factory to call
44
- * @param parameter.defaultConfig - the default config of the widget
45
- * @param parameter.widgetConfig - the config of the widget, overriding the defaultConfig
46
- * @param parameter.events - the events of the widget
47
- * @param parameter.props - the props of the widget
48
- * @param parameter.enablePatchChanged - enable patching changed props
49
- * @returns the widget
42
+ * @param factory - the widget factory to call
43
+ * @param options - the optional options
44
+ * @param options.defaultConfig - the default config of the widget
45
+ * @param options.widgetConfig - the config of the widget, overriding the defaultConfig
46
+ * @param options.events - the events of the widget
47
+ * @param options.props - the props of the widget
48
+ * @returns the state, api and directives to track and interact with the widget
50
49
  */
51
- export const callWidgetFactoryWithConfig = (parameter) => {
52
- const { factory, defaultConfig, widgetConfig, events, enablePatchChanged } = parameter;
53
- const props = parameter.props ?? {};
50
+ export const callWidgetFactoryWithConfig = (factory, options) => {
51
+ const { defaultConfig, widgetConfig, events } = options ?? {};
52
+ const props = options?.props ?? {};
54
53
  const defaultConfig$ = toReadableStore(defaultConfig);
55
54
  const propsWithEvents = { ...props };
56
55
  if (events) {
@@ -63,12 +62,10 @@ export const callWidgetFactoryWithConfig = (parameter) => {
63
62
  props: propsWithEvents,
64
63
  });
65
64
  const runes = Object.fromEntries(Object.entries(widget.stores).map(([key, val]) => [key.slice(0, -1), fromStore(val)]));
66
- if (enablePatchChanged) {
67
- const patch = createPatchChangedProps(props, widget.patch);
68
- $effect(() => {
69
- patch({ ...parameter.props });
70
- });
71
- }
65
+ const patch = createPatchChangedProps(props, widget.patch);
66
+ $effect(() => {
67
+ patch({ ...options?.props });
68
+ });
72
69
  return {
73
70
  api: widget.api,
74
71
  directives: widget.directives,