@sitecore-feaas/clientside 0.5.12 → 0.5.13-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/src/ui/react.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React from 'react'
1
+ import React, { ReactComponentElement } from 'react'
2
2
  import * as BYOC from '@sitecore/byoc/react'
3
3
  import { HTMLNode, parseHTMLContent, renderHTMLContent } from '../dom/html.js'
4
4
  import { DataSettings } from '../utils/settings.js'
@@ -18,6 +18,16 @@ export { External, FEAASExternalPropsObject, getComponent, linkComponents, regis
18
18
  export * from './index.js'
19
19
  export { renderHTMLContent }
20
20
 
21
+ var ReactElementComponents: Record<string, React.Component | React.FC> = {}
22
+ /**
23
+ * Set custom implementation for a HTML tag name. Useful for Next.js Image component, handling image optimization.
24
+ *
25
+ * @param Component
26
+ */
27
+ export function setElementImplementation(tagName: string, Component: React.Component | React.FC) {
28
+ ReactElementComponents[tagName] = Component
29
+ }
30
+
21
31
  type FEEASReactComponentFetch = string[] | string | null | undefined | boolean
22
32
  export function normalizeFetchAttribute(fetch: FEEASReactComponentFetch) {
23
33
  if (fetch == null || fetch === true) {
@@ -96,6 +106,10 @@ export function FEAASReactComponent({
96
106
  HTMLNodeToReact(
97
107
  node,
98
108
  (node, attributes) => {
109
+ // Override rendering of html tag if user provided
110
+ if (ReactElementComponents[node.localName]) {
111
+ return [ReactElementComponents[node.localName], attributes]
112
+ }
99
113
  if (node.localName == 'feaas-external') {
100
114
  const camelized = {} as any
101
115
  for (var prop in attributes) {
@@ -1,7 +1,14 @@
1
1
  import React, { useContext } from 'react'
2
2
  import ReactDOM from 'react-dom'
3
- import { describe, expect, it, spy } from 'vitest'
4
- import { Component, registerComponent, ExternalComponent } from '../src/ui/react.js'
3
+ import { describe, expect, it, beforeEach, vi } from 'vitest'
4
+ import {
5
+ Component,
6
+ ExternalComponent,
7
+ FEAASElement,
8
+ registerComponent,
9
+ setElementImplementation
10
+ } from '../src/ui/react.js'
11
+ import { afterEach } from 'node:test'
5
12
 
6
13
  function Hello(props: { firstName: string; telephone: string }) {
7
14
  const context = useContext(DataContext)
@@ -88,7 +95,7 @@ describe('React', () => {
88
95
  it('render react component', async () => {
89
96
  await validate(
90
97
  <section>
91
- <Component src='library/test'></Component>
98
+ <Component src='library/test' />
92
99
  </section>,
93
100
  '<section><feaas-component class="-feaas" library="library" component="test" src="library/test"></feaas-component></section>'
94
101
  )
@@ -283,8 +290,8 @@ Typeof superobject object, {"a":1,"b":2})</div></feaas-external></feaas-componen
283
290
  '<section><feaas-component class="-feaas" library="a" component="b" src="a/b" fetch="template" data="{&quot;_&quot;:{&quot;telephone&quot;:321,&quot;superobject&quot;:{&quot;1&quot;:2}}}"></feaas-component></section>'
284
291
  )
285
292
 
286
- await document.querySelector('feaas-component')?.whenRendered
287
- await document.querySelector('feaas-external')?.whenRendered
293
+ await (document.querySelector('feaas-component') as FEAASElement)?.whenRendered
294
+ await (document.querySelector('feaas-external') as FEAASElement)?.whenRendered
288
295
 
289
296
  expect(document.querySelector('feaas-component').outerHTML).toEqual(
290
297
  '<feaas-component class="-feaas" library="a" component="b" src="a/b" fetch="template" data="{&quot;_&quot;:{&quot;telephone&quot;:321,&quot;superobject&quot;:{&quot;1&quot;:2}}}"><feaas-external data-external-id="MyTestDataComponent" first-name="Yarik" data-path-attributes="_" telephone="321" superobject="{&quot;1&quot;:2}" data-attributes-keys="telephone,superobject" datasources="{&quot;_&quot;:{&quot;telephone&quot;:321,&quot;superobject&quot;:{&quot;1&quot;:2}}}"><div>\n' +
@@ -312,4 +319,24 @@ Typeof superobject object, {"a":1,"b":2})</div></feaas-external></feaas-componen
312
319
  'And context value is INHERITED</div></feaas-external></feaas-component></section>'
313
320
  )
314
321
  })
322
+
323
+ describe('setImageComponent', () => {
324
+ afterEach(() => {
325
+ setElementImplementation('img', null)
326
+ })
327
+ it('allow component customization', async () => {
328
+ const ImageComponent = (props: any) => React.createElement('x-img', props)
329
+ setElementImplementation('img', ImageComponent)
330
+ await validate(
331
+ <section>
332
+ <Component
333
+ src='library/test'
334
+ template={'<img data-path-src="abc" alt="test">'}
335
+ data={{ abc: 321 }}
336
+ ></Component>
337
+ </section>,
338
+ '<section><feaas-component class="-feaas" library="library" component="test" src="library/test" data="{&quot;abc&quot;:321}"><x-img data-path-src="abc" alt="test" src="321"></x-img></feaas-component></section>'
339
+ )
340
+ })
341
+ })
315
342
  })
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "extends": "../../tsconfig.base.json",
3
3
  "compilerOptions": {
4
- "types": ["react", "react-dom", "node"],
4
+ "types": ["react", "react-dom", "node", "vitest"],
5
5
  "rootDir": ".",
6
6
  "noEmit": true
7
7
  },
@@ -281,7 +281,7 @@ export declare class FEAASElement<Props = any, Payload = any> extends WebCompone
281
281
  };
282
282
  contextType?: React.Context<any>;
283
283
  };
284
- getReactElement(target?: FEAASElement, props?: any): JSX.Element;
284
+ getReactElement(target?: FEAASElement, props?: any): React.JSX.Element;
285
285
  getReactWrapper(target?: FEAASElement, displayName?: string): React.FunctionComponent<{}>;
286
286
  uid: string;
287
287
  getUID(): string;
@@ -54,7 +54,7 @@ export declare class FEAASContext extends FEAASElement<FEAASContextProps, typeof
54
54
  onStatusChange: FEAASContextImports.SDKStatusCallback;
55
55
  whenSDKReady: ControlledPromise<FEAASContextImports.SDK>;
56
56
  getPortalRoot(): HTMLElement | ShadowRoot | FEAASElement<any, any>;
57
- render({ children, library }: React.PropsWithChildren<FEAASContextProps>, { AppContext, Chakra: { Button, Tooltip }, React }: typeof this.payload): JSX.Element;
57
+ render({ children, library }: React.PropsWithChildren<FEAASContextProps>, { AppContext, Chakra: { Button, Tooltip }, React }: typeof this.payload): FEAASContextImports.React.JSX.Element;
58
58
  }
59
59
  declare global {
60
60
  namespace JSX {
@@ -23,7 +23,7 @@ export default class FEAASExternal extends FEAASElement<FEAASExternalProps, Reac
23
23
  getProps(): FEAASExternalProps;
24
24
  static Preview(props: Partial<FEAASComponentProps<any>> & {
25
25
  src: string;
26
- }): JSX.Element;
26
+ }): React.JSX.Element;
27
27
  static linkComponents(components: Promise<any>[]): () => any;
28
28
  static registered: BYOC.RegisteredComponents;
29
29
  static registerComponent: typeof BYOC.registerComponent;
@@ -51,7 +51,7 @@ export declare class FEAASPicker extends FEAASElement<FEAASPickerProps, typeof F
51
51
  };
52
52
  load(): Promise<typeof FEAASPickerImports>;
53
53
  connectedCallback(): void;
54
- render({ placeholder, context, mode, dialog, children, variant }: React.PropsWithChildren<FEAASPickerProps>, { Picker, React, useContext, EnvironmentContext, useLibrary, useSDK, BYOC }: typeof FEAASPickerImports): JSX.Element;
54
+ render({ placeholder, context, mode, dialog, children, variant }: React.PropsWithChildren<FEAASPickerProps>, { Picker, React, useContext, EnvironmentContext, useLibrary, useSDK, BYOC }: typeof FEAASPickerImports): FEAASPickerImports.React.JSX.Element;
55
55
  }
56
56
  declare global {
57
57
  namespace JSX {
@@ -6,6 +6,12 @@ export * from '../headless.js';
6
6
  export { External, FEAASExternalPropsObject, getComponent, linkComponents, registerComponent } from './FEAASExternal.js';
7
7
  export * from './index.js';
8
8
  export { renderHTMLContent };
9
+ /**
10
+ * Set custom implementation for a HTML tag name. Useful for Next.js Image component, handling image optimization.
11
+ *
12
+ * @param Component
13
+ */
14
+ export declare function setElementImplementation(tagName: string, Component: React.Component | React.FC): void;
9
15
  type FEEASReactComponentFetch = string[] | string | null | undefined | boolean;
10
16
  export declare function normalizeFetchAttribute(fetch: FEEASReactComponentFetch): any[];
11
17
  export type FEAASReactComponentProps = FEAASComponentProps & {
@@ -20,7 +26,7 @@ export type FEAASReactComponentProps = FEAASComponentProps & {
20
26
  * @param {FEAASCDNStylesheetParams | FEAASCDNStylesheetSource} props - The properties for including the stylesheet.
21
27
  * @returns {JSX.Element} The <link> element for the stylesheet.
22
28
  */
23
- export declare const FEAASReactStylesheet: (props: FEAASCDNStylesheetParams | FEAASCDNStylesheetSource) => JSX.Element;
29
+ export declare const FEAASReactStylesheet: (props: FEAASCDNStylesheetParams | FEAASCDNStylesheetSource) => React.JSX.Element;
24
30
  /**
25
31
  * An asynchronous wrapper over FEAAS.Component for use in React, especially suited for Next.js.
26
32
  * This component fetches HTML and data asynchronously for server-side rendering with data mapping.
@@ -28,7 +34,7 @@ export declare const FEAASReactStylesheet: (props: FEAASCDNStylesheetParams | FE
28
34
  * @param {FEAASReactComponentProps} props - The properties for the component.
29
35
  * @returns {Promise<JSX.Element>} The JSX element representing the component.
30
36
  */
31
- export declare const FEAASReactServerComponent: ({ data, fetch, ...props }: FEAASReactComponentProps) => Promise<JSX.Element>;
37
+ export declare const FEAASReactServerComponent: ({ data, fetch, ...props }: FEAASReactComponentProps) => Promise<React.JSX.Element>;
32
38
  /**
33
39
  * A wrapper over FEAAS.Component for use in React. It converts a passed template HTML into React,
34
40
  * allowing external components in the render tree. Data can be provided in JSON format.
@@ -38,7 +44,7 @@ export declare const FEAASReactServerComponent: ({ data, fetch, ...props }: FEAA
38
44
  */
39
45
  export declare function FEAASReactComponent({ data, lastModified, fetchedData, ...props }: FEAASReactComponentProps & {
40
46
  fetchedData?: any;
41
- }): JSX.Element;
47
+ }): React.JSX.Element;
42
48
  /**
43
49
  * Converts an HTMLNode into a React tree, ensuring proper context access for external components.
44
50
  *
@@ -57,6 +63,6 @@ export declare const ExternalComponent: typeof BYOC.Component;
57
63
  export declare const ReactExternalComponent: typeof BYOC.RegularComponent;
58
64
  export declare const NextExternalComponent: typeof BYOC.NextComponent;
59
65
  export declare const Component: typeof FEAASReactComponent;
60
- export declare const ServerComponent: ({ data, fetch, ...props }: FEAASReactComponentProps) => Promise<JSX.Element>;
61
- export declare const Stylesheet: (props: FEAASCDNStylesheetParams | FEAASCDNStylesheetSource) => JSX.Element;
66
+ export declare const ServerComponent: ({ data, fetch, ...props }: FEAASReactComponentProps) => Promise<React.JSX.Element>;
67
+ export declare const Stylesheet: (props: FEAASCDNStylesheetParams | FEAASCDNStylesheetSource) => React.JSX.Element;
62
68
  //#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