@agnos-ui/react-headless 0.0.1-alpha.1
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/Portal.d.ts +4 -0
- package/Portal.js +3 -0
- package/README.md +15 -0
- package/Slot.d.ts +5 -0
- package/Slot.js +25 -0
- package/WidgetsDefaultConfig.d.ts +48 -0
- package/WidgetsDefaultConfig.js +53 -0
- package/index.d.ts +34 -0
- package/index.js +5 -0
- package/package.json +43 -0
- package/utils.d.ts +22 -0
- package/utils.js +67 -0
package/Portal.d.ts
ADDED
package/Portal.js
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @agnos-ui/react-headless
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@agnos-ui/react-headless)
|
|
4
|
+
|
|
5
|
+
Headless widget library for [React](https://react.dev/).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @agnos-ui/react-headless
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Please check [our demo site](https://amadeusitgroup.github.io/AgnosUI/latest/) to see all the available widgets and how to use them.
|
package/Slot.d.ts
ADDED
package/Slot.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Component } from 'react';
|
|
3
|
+
const slotWrapperCache = new WeakMap();
|
|
4
|
+
const slotWrapperFactory = (slotContent) => {
|
|
5
|
+
const SlotWrapper = (props) => _jsx(_Fragment, { children: slotContent(props) });
|
|
6
|
+
return SlotWrapper;
|
|
7
|
+
};
|
|
8
|
+
const slotWrapper = (slotContent) => {
|
|
9
|
+
let res = slotWrapperCache.get(slotContent);
|
|
10
|
+
if (!res) {
|
|
11
|
+
res = slotWrapperFactory(slotContent);
|
|
12
|
+
slotWrapperCache.set(slotContent, res);
|
|
13
|
+
}
|
|
14
|
+
return res;
|
|
15
|
+
};
|
|
16
|
+
/* eslint-disable-next-line @typescript-eslint/ban-types */
|
|
17
|
+
const isReactComponent = (item) => item.prototype instanceof Component;
|
|
18
|
+
export const Slot = ({ slotContent, props }) => {
|
|
19
|
+
const isFunction = typeof slotContent === 'function';
|
|
20
|
+
if (isFunction) {
|
|
21
|
+
const SlotComponent = isReactComponent(slotContent) ? slotContent : slotWrapper(slotContent);
|
|
22
|
+
return _jsx(SlotComponent, { ...props });
|
|
23
|
+
}
|
|
24
|
+
return _jsx(_Fragment, { children: slotContent });
|
|
25
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Partial2Levels } from '@agnos-ui/core';
|
|
2
|
+
import type { PropsWithChildren } from 'react';
|
|
3
|
+
import type { WidgetsConfig } from './utils';
|
|
4
|
+
/**
|
|
5
|
+
* React component that provides in the React context (for all AgnosUI descendant widgets) a new widgets default configuration
|
|
6
|
+
* store that inherits from any widgets default configuration store already defined at an upper level in the React context hierarchy.
|
|
7
|
+
* It contains its own set of widgets configuration properties that override the same properties form the parent configuration.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* The configuration is computed from the parent configuration in two steps:
|
|
11
|
+
* - first step: the parent configuration is transformed by the adaptParentConfig function (if specified).
|
|
12
|
+
* If adaptParentConfig is not specified, this step is skipped.
|
|
13
|
+
* - second step: the configuration from step 1 is merged (2-levels deep) with the properties of the component.
|
|
14
|
+
*
|
|
15
|
+
* @param adaptParentConfig - optional function that receives a 2-levels copy of the widgets default configuration
|
|
16
|
+
* defined at an upper level in the Svelte context hierarchy (or an empty object if there is none) and returns the widgets
|
|
17
|
+
* default configuration to be used.
|
|
18
|
+
* It is called only if the configuration is needed, and was not yet computed for the current value of the parent configuration.
|
|
19
|
+
* It is called in a tansu reactive context, so it can use any tansu store and will be called again if those stores change.
|
|
20
|
+
*
|
|
21
|
+
* @returns the resulting widgets default configuration store, which contains 3 additional properties that are stores:
|
|
22
|
+
* parent$, adaptedParent$ (containing the value computed after the first step), and own$ (that contains only overridding properties).
|
|
23
|
+
* The resulting store is writable, its set function is actually the set function of the own$ store.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* <WidgetsDefaultConfig
|
|
28
|
+
* adaptParentConfig={(parentConfig) => {
|
|
29
|
+
* parentConfig.rating = parentConfig.rating ?? {};
|
|
30
|
+
* parentConfig.rating.className = `${parentConfig.rating.className ?? ''} my-rating-extra-class`
|
|
31
|
+
* return parentConfig;
|
|
32
|
+
* }}
|
|
33
|
+
* rating={{slotStar: MyCustomSlotStar}}
|
|
34
|
+
* />
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare const WidgetsDefaultConfig: ({ children, adaptParentConfig, ...props }: Partial<{
|
|
38
|
+
pagination: Partial<import("./utils").AdaptPropsSlots<import("@agnos-ui/core").PaginationProps>>;
|
|
39
|
+
rating: Partial<import("./utils").AdaptPropsSlots<import("@agnos-ui/core").RatingProps>>;
|
|
40
|
+
select: Partial<import("./utils").AdaptPropsSlots<import("@agnos-ui/core").SelectProps<any>>>;
|
|
41
|
+
modal: Partial<import("./utils").AdaptPropsSlots<import("@agnos-ui/core").ModalProps>>;
|
|
42
|
+
alert: Partial<import("./utils").AdaptPropsSlots<import("@agnos-ui/core").AlertProps>>;
|
|
43
|
+
accordion: Partial<import("./utils").AdaptPropsSlots<import("@agnos-ui/core").AccordionProps>>;
|
|
44
|
+
}> & {
|
|
45
|
+
children?: import("react").ReactNode;
|
|
46
|
+
} & {
|
|
47
|
+
adaptParentConfig?: ((config: Partial2Levels<WidgetsConfig>) => Partial2Levels<WidgetsConfig>) | undefined;
|
|
48
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createWidgetsConfig } from '@agnos-ui/core';
|
|
3
|
+
import { useContext, useEffect, useMemo } from 'react';
|
|
4
|
+
import { widgetsConfigContext } from './utils';
|
|
5
|
+
/**
|
|
6
|
+
* React component that provides in the React context (for all AgnosUI descendant widgets) a new widgets default configuration
|
|
7
|
+
* store that inherits from any widgets default configuration store already defined at an upper level in the React context hierarchy.
|
|
8
|
+
* It contains its own set of widgets configuration properties that override the same properties form the parent configuration.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* The configuration is computed from the parent configuration in two steps:
|
|
12
|
+
* - first step: the parent configuration is transformed by the adaptParentConfig function (if specified).
|
|
13
|
+
* If adaptParentConfig is not specified, this step is skipped.
|
|
14
|
+
* - second step: the configuration from step 1 is merged (2-levels deep) with the properties of the component.
|
|
15
|
+
*
|
|
16
|
+
* @param adaptParentConfig - optional function that receives a 2-levels copy of the widgets default configuration
|
|
17
|
+
* defined at an upper level in the Svelte context hierarchy (or an empty object if there is none) and returns the widgets
|
|
18
|
+
* default configuration to be used.
|
|
19
|
+
* It is called only if the configuration is needed, and was not yet computed for the current value of the parent configuration.
|
|
20
|
+
* It is called in a tansu reactive context, so it can use any tansu store and will be called again if those stores change.
|
|
21
|
+
*
|
|
22
|
+
* @returns the resulting widgets default configuration store, which contains 3 additional properties that are stores:
|
|
23
|
+
* parent$, adaptedParent$ (containing the value computed after the first step), and own$ (that contains only overridding properties).
|
|
24
|
+
* The resulting store is writable, its set function is actually the set function of the own$ store.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* <WidgetsDefaultConfig
|
|
29
|
+
* adaptParentConfig={(parentConfig) => {
|
|
30
|
+
* parentConfig.rating = parentConfig.rating ?? {};
|
|
31
|
+
* parentConfig.rating.className = `${parentConfig.rating.className ?? ''} my-rating-extra-class`
|
|
32
|
+
* return parentConfig;
|
|
33
|
+
* }}
|
|
34
|
+
* rating={{slotStar: MyCustomSlotStar}}
|
|
35
|
+
* />
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export const WidgetsDefaultConfig = ({ children, adaptParentConfig, ...props }) => {
|
|
39
|
+
const config$ = useContext(widgetsConfigContext);
|
|
40
|
+
let storeRecreated = false;
|
|
41
|
+
const store$ = useMemo(() => {
|
|
42
|
+
const store = createWidgetsConfig(config$, adaptParentConfig);
|
|
43
|
+
store.set(props);
|
|
44
|
+
storeRecreated = true;
|
|
45
|
+
return store;
|
|
46
|
+
}, [config$, adaptParentConfig]);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (!storeRecreated) {
|
|
49
|
+
store$.set(props);
|
|
50
|
+
}
|
|
51
|
+
}, [props]);
|
|
52
|
+
return _jsx(widgetsConfigContext.Provider, { value: store$, children: children });
|
|
53
|
+
};
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export * from '@agnos-ui/core';
|
|
2
|
+
export * from './Portal';
|
|
3
|
+
export * from './Slot';
|
|
4
|
+
export * from './WidgetsDefaultConfig';
|
|
5
|
+
export * from './utils';
|
|
6
|
+
import type { WidgetProps, WidgetState } from '@agnos-ui/core';
|
|
7
|
+
import type { AdaptSlotContentProps, AdaptWidgetSlots } from './utils';
|
|
8
|
+
export type { SlotContent, WidgetsConfig } from './utils';
|
|
9
|
+
export type AccordionWidget = AdaptWidgetSlots<import('@agnos-ui/core').AccordionWidget>;
|
|
10
|
+
export type AccordionProps = WidgetProps<AccordionWidget>;
|
|
11
|
+
export type AccordionState = WidgetState<AccordionWidget>;
|
|
12
|
+
export type AccordionItemWidget = AdaptWidgetSlots<import('@agnos-ui/core').AccordionItemWidget>;
|
|
13
|
+
export type AccordionItemProps = WidgetProps<AccordionItemWidget>;
|
|
14
|
+
export type AccordionItemState = WidgetState<AccordionItemWidget>;
|
|
15
|
+
export type AccordionItemContext = AdaptSlotContentProps<import('@agnos-ui/core').AccordionItemContext>;
|
|
16
|
+
export type AlertWidget = AdaptWidgetSlots<import('@agnos-ui/core').AlertWidget>;
|
|
17
|
+
export type AlertProps = WidgetProps<AlertWidget>;
|
|
18
|
+
export type AlertState = WidgetState<AlertWidget>;
|
|
19
|
+
export type AlertContext = AdaptSlotContentProps<import('@agnos-ui/core').AlertContext>;
|
|
20
|
+
export type ModalWidget = AdaptWidgetSlots<import('@agnos-ui/core').ModalWidget>;
|
|
21
|
+
export type ModalProps = WidgetProps<ModalWidget>;
|
|
22
|
+
export type ModalState = WidgetState<ModalWidget>;
|
|
23
|
+
export type ModalContext = AdaptSlotContentProps<import('@agnos-ui/core').ModalContext>;
|
|
24
|
+
export type PaginationWidget = AdaptWidgetSlots<import('@agnos-ui/core').PaginationWidget>;
|
|
25
|
+
export type PaginationProps = WidgetProps<PaginationWidget>;
|
|
26
|
+
export type PaginationState = WidgetState<PaginationWidget>;
|
|
27
|
+
export type PaginationContext = AdaptSlotContentProps<import('@agnos-ui/core').PaginationContext>;
|
|
28
|
+
export type PaginationNumberContext = AdaptSlotContentProps<import('@agnos-ui/core').PaginationNumberContext>;
|
|
29
|
+
export type RatingWidget = AdaptWidgetSlots<import('@agnos-ui/core').RatingWidget>;
|
|
30
|
+
export type RatingProps = WidgetProps<RatingWidget>;
|
|
31
|
+
export type RatingState = WidgetState<RatingWidget>;
|
|
32
|
+
export type SelectWidget<Item> = AdaptWidgetSlots<import('@agnos-ui/core').SelectWidget<Item>>;
|
|
33
|
+
export type SelectProps<Item> = WidgetProps<SelectWidget<Item>>;
|
|
34
|
+
export type SelectState<Item> = WidgetState<SelectWidget<Item>>;
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agnos-ui/react-headless",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"main": "./index.js",
|
|
5
|
+
"module": "./index.js",
|
|
6
|
+
"types": "./index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"default": "./index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@agnos-ui/core": "0.0.1-alpha.1",
|
|
15
|
+
"@amadeus-it-group/tansu": "0.0.22",
|
|
16
|
+
"classnames": "^2.3.2"
|
|
17
|
+
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"react": "*",
|
|
20
|
+
"react-dom": "*"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"version": "0.0.1-alpha.1",
|
|
24
|
+
"homepage": "https://amadeusitgroup.github.io/AgnosUI/latest/",
|
|
25
|
+
"bugs": "https://github.com/AmadeusITGroup/AgnosUI/issues",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/AmadeusITGroup/AgnosUI.git",
|
|
30
|
+
"directory": "react/headless"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"react",
|
|
34
|
+
"headless",
|
|
35
|
+
"components",
|
|
36
|
+
"widgets",
|
|
37
|
+
"accordion",
|
|
38
|
+
"alert",
|
|
39
|
+
"modal",
|
|
40
|
+
"pagination",
|
|
41
|
+
"rating"
|
|
42
|
+
]
|
|
43
|
+
}
|
package/utils.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Directive, Widget, WidgetFactory, WidgetProps, WidgetState, WidgetsConfigStore } from '@agnos-ui/core';
|
|
2
|
+
import type { ReadableSignal } from '@amadeus-it-group/tansu';
|
|
3
|
+
import type { RefCallback } from 'react';
|
|
4
|
+
import type { SlotContent as CoreSlotContent, WidgetsConfig as CoreWidgetsConfig, WidgetSlotContext } from '@agnos-ui/core';
|
|
5
|
+
export type SlotContent<Props extends object = object> = CoreSlotContent<Props> | ((props: Props) => React.ReactNode) | React.ComponentType<Props> | React.ReactNode;
|
|
6
|
+
export type AdaptSlotContentProps<Props extends Record<string, any>> = Props extends WidgetSlotContext<infer U> ? WidgetSlotContext<AdaptWidgetSlots<U>> & AdaptPropsSlots<Omit<Props, keyof WidgetSlotContext<any>>> : AdaptPropsSlots<Props>;
|
|
7
|
+
export type AdaptPropsSlots<Props> = Omit<Props, `slot${string}`> & {
|
|
8
|
+
[K in keyof Props & `slot${string}`]: Props[K] extends CoreSlotContent<infer U> ? SlotContent<AdaptSlotContentProps<U>> : Props[K];
|
|
9
|
+
};
|
|
10
|
+
export type WidgetsConfig = {
|
|
11
|
+
[WidgetName in keyof CoreWidgetsConfig]: AdaptPropsSlots<CoreWidgetsConfig[WidgetName]>;
|
|
12
|
+
};
|
|
13
|
+
export type AdaptWidgetSlots<W extends Widget> = Widget<AdaptPropsSlots<WidgetProps<W>>, AdaptPropsSlots<WidgetState<W>>, W['api'], W['actions'], W['directives']>;
|
|
14
|
+
export declare function useWidget<Factory extends (...arg: any[]) => Widget>(createWidget: Factory, props?: Partial<WidgetProps<ReturnType<Factory>>>, ...initProps: Parameters<Factory>): [WidgetState<ReturnType<Factory>>, ReturnType<Factory>];
|
|
15
|
+
export declare function useObservable<T>(store$: ReadableSignal<T>): T;
|
|
16
|
+
export declare function useDirective(directive: Directive<void>): RefCallback<HTMLElement>;
|
|
17
|
+
export declare function useDirective<T>(directive: Directive<T>, args: T): RefCallback<HTMLElement>;
|
|
18
|
+
/**
|
|
19
|
+
* React context which can be used to provide or consume the widgets default configuration store.
|
|
20
|
+
*/
|
|
21
|
+
export declare const widgetsConfigContext: import("react").Context<WidgetsConfigStore<WidgetsConfig> | undefined>;
|
|
22
|
+
export declare const useWidgetWithConfig: <W extends Widget<object, object, object, object, object>>(factory: WidgetFactory<W>, props: Partial<AdaptPropsSlots<WidgetProps<W>>> | undefined, widgetName: keyof WidgetsConfig | null, defaultProps?: Partial<AdaptPropsSlots<WidgetProps<W>>> | undefined) => [AdaptPropsSlots<WidgetState<W>>, AdaptWidgetSlots<W>];
|
package/utils.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { findChangedProperties } from '@agnos-ui/core';
|
|
2
|
+
import { asReadable, computed, writable } from '@amadeus-it-group/tansu';
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
export function useWidget(createWidget, props = {}, ...initProps) {
|
|
5
|
+
const apiRef = useRef(undefined);
|
|
6
|
+
if (!apiRef.current) {
|
|
7
|
+
apiRef.current = createWidget(...initProps);
|
|
8
|
+
apiRef.current.patch(props);
|
|
9
|
+
}
|
|
10
|
+
const api = apiRef.current;
|
|
11
|
+
const previousProps = useRef(props);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const changedProps = findChangedProperties(previousProps.current, props);
|
|
14
|
+
previousProps.current = props;
|
|
15
|
+
if (changedProps) {
|
|
16
|
+
api.patch(changedProps);
|
|
17
|
+
}
|
|
18
|
+
}, [props]);
|
|
19
|
+
const state = useObservable(api.state$);
|
|
20
|
+
return [state, api];
|
|
21
|
+
}
|
|
22
|
+
export function useObservable(store$) {
|
|
23
|
+
const [value, setValue] = useState(() => store$());
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
return store$.subscribe((arg) => {
|
|
26
|
+
// We're passing an update function to setValue here instead of just doing setValue(arg) so that
|
|
27
|
+
// if arg is a function, it is properly used as the value instead of being called as an update function
|
|
28
|
+
setValue(() => arg);
|
|
29
|
+
});
|
|
30
|
+
}, [store$]);
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
export function useDirective(directive, args) {
|
|
34
|
+
const instance = useRef();
|
|
35
|
+
const propsRef = useRef();
|
|
36
|
+
const ref = useCallback((element) => {
|
|
37
|
+
instance.current?.destroy?.();
|
|
38
|
+
instance.current = undefined;
|
|
39
|
+
if (element) {
|
|
40
|
+
instance.current = directive(element, propsRef.current);
|
|
41
|
+
}
|
|
42
|
+
}, [directive]);
|
|
43
|
+
propsRef.current = args;
|
|
44
|
+
instance.current?.update?.(args);
|
|
45
|
+
return ref;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* React context which can be used to provide or consume the widgets default configuration store.
|
|
49
|
+
*/
|
|
50
|
+
export const widgetsConfigContext = createContext(undefined);
|
|
51
|
+
const propsEqual = (a, b) => !findChangedProperties(a, b);
|
|
52
|
+
const usePropsAsStore = (props) => {
|
|
53
|
+
const storeRef = useRef();
|
|
54
|
+
if (!storeRef.current) {
|
|
55
|
+
storeRef.current = writable({ ...props }, { equal: propsEqual });
|
|
56
|
+
}
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
storeRef.current.set({ ...props });
|
|
59
|
+
}, [props]);
|
|
60
|
+
return useMemo(() => asReadable(storeRef.current), [storeRef.current]);
|
|
61
|
+
};
|
|
62
|
+
const useWidgetContext = (widgetName, defaultConfig) => {
|
|
63
|
+
const widgetsConfig = useContext(widgetsConfigContext);
|
|
64
|
+
const defaultConfig$ = usePropsAsStore(defaultConfig);
|
|
65
|
+
return useMemo(() => computed(() => ({ ...defaultConfig$(), ...(widgetName ? widgetsConfig?.()[widgetName] : undefined) })), [widgetsConfig]);
|
|
66
|
+
};
|
|
67
|
+
export const useWidgetWithConfig = (factory, props, widgetName, defaultProps) => useWidget(factory, props, useWidgetContext(widgetName, defaultProps));
|