@sitecore-jss/sitecore-jss-react 21.3.1 → 21.4.0-canary.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.
@@ -12,8 +12,8 @@ var __rest = (this && this.__rest) || function (s, e) {
12
12
  import { mediaApi } from '@sitecore-jss/sitecore-jss/media';
13
13
  import PropTypes from 'prop-types';
14
14
  import React from 'react';
15
- import ReactDOMServer from 'react-dom/server';
16
15
  import { addClassName, convertAttributesToReactProps } from '../utils';
16
+ import { getAttributesString } from '../utils';
17
17
  const getEditableWrapper = (editableMarkup, ...otherProps) => (
18
18
  // create an inline wrapper and use dangerouslySetInnerHTML.
19
19
  // if we try to parse the EE value, the parser will strip invalid or disallowed attributes from html elements - and EE uses several
@@ -55,7 +55,7 @@ export const getEEMarkup = (imageField, imageParams, mediaUrlPrefix, otherProps)
55
55
  if (!imgAttrs) {
56
56
  return getEditableWrapper(imageField.editable);
57
57
  }
58
- const imgHtml = ReactDOMServer.renderToStaticMarkup(React.createElement("img", Object.assign({}, imgAttrs)));
58
+ const imgHtml = `<img ${getAttributesString(imgAttrs)} />`;
59
59
  const editableMarkup = imageField.editable.replace(foundImg.imgTag, imgHtml);
60
60
  return getEditableWrapper(editableMarkup);
61
61
  };
@@ -4,7 +4,11 @@ export const MissingComponent = (props) => {
4
4
  const componentName = props.rendering && props.rendering.componentName
5
5
  ? props.rendering.componentName
6
6
  : 'Unnamed Component';
7
- console.log(`Component props for unimplemented '${componentName}' component`, props);
7
+ // error override would mean component is not unimplemented
8
+ !props.errorOverride &&
9
+ console.log(`Component props for unimplemented '${componentName}' component`, props);
10
+ const errorMessage = props.errorOverride ||
11
+ 'JSS component is missing React implementation. See the developer console for more information.';
8
12
  return (React.createElement("div", { style: {
9
13
  background: 'darkorange',
10
14
  outline: '5px solid orange',
@@ -13,7 +17,7 @@ export const MissingComponent = (props) => {
13
17
  maxWidth: '500px',
14
18
  } },
15
19
  React.createElement("h2", null, componentName),
16
- React.createElement("p", null, "JSS component is missing React implementation. See the developer console for more information.")));
20
+ React.createElement("p", null, errorMessage)));
17
21
  };
18
22
  MissingComponent.propTypes = {
19
23
  rendering: PropTypes.shape({
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { PlaceholderCommon } from './PlaceholderCommon';
3
3
  import { withComponentFactory } from '../enhancers/withComponentFactory';
4
- import { HorizonEditor, ExperienceEditor } from '@sitecore-jss/sitecore-jss/utils';
4
+ import { HorizonEditor } from '@sitecore-jss/sitecore-jss/utils';
5
5
  /**
6
6
  * @param {HtmlElementRendering | ComponentRendering} rendering
7
7
  */
@@ -44,36 +44,8 @@ class PlaceholderComponent extends PlaceholderCommon {
44
44
  }
45
45
  const renderingData = childProps.rendering;
46
46
  const placeholderData = PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, this.props.name);
47
- this.isEmpty = placeholderData.every((rendering) => isRawRendering(rendering));
48
- // This is a workaround to insert Experience Editor-specific markup on the page for empty placeholders.
49
- // It makes sure the markup is inserted by react and thus avoiding hydration errors when in editing mode
50
- if (this.isEmpty && ExperienceEditor.isActive()) {
51
- const codePos = placeholderData.findIndex((component) => component.name === 'code' &&
52
- component.attributes.kind === 'open');
53
- const plhId = placeholderData[codePos].attributes.id;
54
- const emptyPlaceholderMarkup = [
55
- {
56
- name: 'span',
57
- contents: null,
58
- attributes: {
59
- type: 'text/sitecore',
60
- 'sc-part-of': 'placeholder rendering',
61
- style: 'display: none;',
62
- },
63
- },
64
- {
65
- name: 'div',
66
- contents: null,
67
- attributes: {
68
- class: 'scEnabledChrome scEmptyPlaceholder',
69
- 'sc-part-of': 'placeholder',
70
- 'sc-placeholder-id': plhId,
71
- },
72
- },
73
- ];
74
- placeholderData.splice(codePos + 1, 0, ...emptyPlaceholderMarkup);
75
- }
76
47
  const components = this.getComponentsForRenderingData(placeholderData);
48
+ this.isEmpty = placeholderData.every((rendering) => isRawRendering(rendering));
77
49
  if (this.props.renderEmpty && this.isEmpty) {
78
50
  const rendered = this.props.renderEmpty(components);
79
51
  return components.length ? this.renderEmptyPlaceholder(rendered) : rendered;
@@ -15,6 +15,9 @@ import { MissingComponent } from './MissingComponent';
15
15
  import { convertAttributesToReactProps } from '../utils';
16
16
  import { HiddenRendering, HIDDEN_RENDERING_NAME } from './HiddenRendering';
17
17
  import { FEaaSComponent, FEAAS_COMPONENT_RENDERING_NAME } from './FEaaSComponent';
18
+ import { FEaaSWrapper, FEAAS_WRAPPER_RENDERING_NAME } from './FEaaSWrapper';
19
+ import { BYOCComponent, BYOC_COMPONENT_RENDERING_NAME } from './BYOCComponent';
20
+ import { BYOCWrapper, BYOC_WRAPPER_RENDERING_NAME } from './BYOCWrapper';
18
21
  /**
19
22
  * These patterns need for right rendering Dynamic placeholders.
20
23
  * Must be distinguished Splitter components and another placeholders(containers)
@@ -97,8 +100,20 @@ export class PlaceholderCommon extends React.Component {
97
100
  else {
98
101
  component = this.getComponentForRendering(componentRendering);
99
102
  }
100
- if (componentRendering.componentName === FEAAS_COMPONENT_RENDERING_NAME) {
101
- component = FEaaSComponent;
103
+ // Fallback/defaults for Sitecore Component renderings (in case not defined in component factory)
104
+ if (!component) {
105
+ if (componentRendering.componentName === FEAAS_COMPONENT_RENDERING_NAME) {
106
+ component = FEaaSComponent;
107
+ }
108
+ else if (componentRendering.componentName === FEAAS_WRAPPER_RENDERING_NAME) {
109
+ component = FEaaSWrapper;
110
+ }
111
+ else if (componentRendering.componentName === BYOC_COMPONENT_RENDERING_NAME) {
112
+ component = BYOCComponent;
113
+ }
114
+ else if (componentRendering.componentName === BYOC_WRAPPER_RENDERING_NAME) {
115
+ component = BYOCWrapper;
116
+ }
102
117
  }
103
118
  if (!component) {
104
119
  console.error(`Placeholder ${name} contains unknown component ${componentRendering.componentName}. Ensure that a React component exists for it, and that it is registered in your componentFactory.js.`);
package/dist/esm/index.js CHANGED
@@ -4,12 +4,16 @@ export { LayoutServicePageState, GraphQLLayoutService, RestLayoutService, getChi
4
4
  export { trackingApi, } from '@sitecore-jss/sitecore-jss/tracking';
5
5
  export { GraphQLDictionaryService, RestDictionaryService, } from '@sitecore-jss/sitecore-jss/i18n';
6
6
  export { mediaApi } from '@sitecore-jss/sitecore-jss/media';
7
+ export { getFEAASLibraryStylesheetLinks } from '@sitecore-jss/sitecore-jss/feaas';
7
8
  export { Placeholder } from './components/Placeholder';
8
9
  export { Image, getEEMarkup, } from './components/Image';
9
10
  export { RichText, RichTextPropTypes } from './components/RichText';
10
11
  export { Text } from './components/Text';
11
12
  export { DateField } from './components/Date';
12
13
  export { FEaaSComponent, fetchFEaaSComponentServerProps, } from './components/FEaaSComponent';
14
+ export { FEaaSWrapper } from './components/FEaaSWrapper';
15
+ export { BYOCComponent, fetchBYOCComponentServerProps, } from './components/BYOCComponent';
16
+ export { BYOCWrapper } from './components/BYOCWrapper';
13
17
  export { Link, LinkPropTypes } from './components/Link';
14
18
  export { File } from './components/File';
15
19
  export { VisitorIdentification } from './components/VisitorIdentification';
package/dist/esm/utils.js CHANGED
@@ -1,3 +1,15 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { getFieldValue } from '@sitecore-jss/sitecore-jss/layout';
1
13
  import { parse as styleParse } from 'style-attr';
2
14
  // https://stackoverflow.com/a/10426674/9324
3
15
  export const convertKebabCasetoCamelCase = (str) => str.replace(/^.|-./g, (letter, index) => index === 0 ? letter.toLowerCase() : letter.substr(1).toUpperCase());
@@ -45,7 +57,7 @@ export const convertAttributesToReactProps = (attributes) => {
45
57
  /**
46
58
  * "class" property will be transformed into or appended to "className" instead.
47
59
  * @param {string} otherAttrs all other props included on the image component
48
- * * @returns {void}
60
+ * @returns {void}
49
61
  */
50
62
  export const addClassName = (otherAttrs) => {
51
63
  if (otherAttrs.class) {
@@ -61,3 +73,37 @@ export const addClassName = (otherAttrs) => {
61
73
  delete otherAttrs.class;
62
74
  }
63
75
  };
76
+ /**
77
+ * Converts the given tag attributes object to a string
78
+ * @param {Object.<string, unknown>} attributes the attributes object
79
+ * @returns {string} string representation of the attributes
80
+ */
81
+ export const getAttributesString = (attributes) => {
82
+ const { className } = attributes, restAttributes = __rest(attributes, ["className"]);
83
+ const attributesEntries = Object.entries(restAttributes).map(([key, value]) => {
84
+ if (typeof value === 'object') {
85
+ const valueString = JSON.stringify(value)
86
+ .replace(/"|{|}/g, '')
87
+ .replace(/,/g, ';');
88
+ return `${key}="${valueString}"`;
89
+ }
90
+ return `${key}="${value}"`;
91
+ });
92
+ if (className) {
93
+ attributesEntries.push(`class="${className}"`);
94
+ }
95
+ return attributesEntries.join(' ');
96
+ };
97
+ /**
98
+ * Used in FEAAS and BYOC implementations to convert datasource item field values into component props
99
+ * @param {ComponentFields} fields field collection from Sitecore
100
+ * @returns JSON object that can be used as props
101
+ */
102
+ export const getDataFromFields = (fields) => {
103
+ let data = {};
104
+ data = Object.entries(fields).reduce((acc, [key]) => {
105
+ acc[key] = getFieldValue(fields, key);
106
+ return acc;
107
+ }, data);
108
+ return data;
109
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-jss/sitecore-jss-react",
3
- "version": "21.3.1",
3
+ "version": "21.4.0-canary.2",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "sideEffects": false,
@@ -27,6 +27,7 @@
27
27
  "url": "https://github.com/sitecore/jss/issues"
28
28
  },
29
29
  "devDependencies": {
30
+ "@sitecore-feaas/clientside": "^0.3.14",
30
31
  "@types/chai": "^4.3.4",
31
32
  "@types/chai-string": "^1.4.2",
32
33
  "@types/deep-equal": "^1.0.1",
@@ -57,19 +58,19 @@
57
58
  "typescript": "~4.9.3"
58
59
  },
59
60
  "peerDependencies": {
61
+ "@sitecore-feaas/clientside": "^0.3.14",
60
62
  "react": "^18.2.0",
61
63
  "react-dom": "^18.2.0"
62
64
  },
63
65
  "dependencies": {
64
- "@sitecore-feaas/clientside": "^0.3.0",
65
- "@sitecore-jss/sitecore-jss": "21.3.1",
66
+ "@sitecore-jss/sitecore-jss": "^21.4.0-canary.2",
66
67
  "deep-equal": "^2.1.0",
67
68
  "prop-types": "^15.8.1",
68
69
  "style-attr": "^1.3.0"
69
70
  },
70
71
  "description": "",
71
72
  "types": "types/index.d.ts",
72
- "gitHead": "973fe436c7fe943ec07123b315fadbe72313a945",
73
+ "gitHead": "30d79c092ae7bee91d3f6a1a480a89c76e5b262c",
73
74
  "files": [
74
75
  "dist",
75
76
  "types"
@@ -0,0 +1,82 @@
1
+ import React from 'react';
2
+ import { ComponentFields } from '@sitecore-jss/sitecore-jss/layout';
3
+ import { MissingComponentProps } from './MissingComponent';
4
+ import * as FEAAS from '@sitecore-feaas/clientside/react';
5
+ export declare const BYOC_COMPONENT_RENDERING_NAME = "BYOCComponent";
6
+ /**
7
+ * FEaaS props for server rendering.
8
+ */
9
+ type BYOCServerProps = {
10
+ /**
11
+ * Fetched data from server component props for server rendering, based on rendering params.
12
+ */
13
+ fetchedData?: FEAAS.DataScopes;
14
+ };
15
+ /**
16
+ * Data from rendering params on Sitecore's BYOC rendering
17
+ */
18
+ export type BYOCComponentParams = {
19
+ /**
20
+ * Name of the component to render
21
+ */
22
+ ComponentName?: string;
23
+ /**
24
+ * JSON props to pass into rendered component
25
+ */
26
+ ComponentProps?: string;
27
+ /**
28
+ * A string with classes that can be used to apply themes, via SXA functionality
29
+ */
30
+ styles?: string;
31
+ RenderingIdentifier?: string;
32
+ };
33
+ /**
34
+ * Props for BYOCComponent. Includes components list to load external components from.
35
+ */
36
+ export type BYOCComponentClientProps = {
37
+ /**
38
+ * rendering params
39
+ */
40
+ params?: BYOCComponentParams;
41
+ /**
42
+ * fields from datasource items to be passed as rendered child component props
43
+ */
44
+ fields?: ComponentFields;
45
+ /**
46
+ * Error component override. To be shown when Renderer or underlying component throws
47
+ */
48
+ errorComponent?: React.ComponentClass<ErrorComponentProps> | React.FC<ErrorComponentProps>;
49
+ /**
50
+ * Override to indicate missing component situations. Would be shown when BYOC component is not registered
51
+ * or ComponentName is missing
52
+ */
53
+ missingComponentComponent?: React.ComponentClass<MissingComponentProps> | React.FC<MissingComponentProps>;
54
+ };
55
+ export type BYOCComponentProps = BYOCComponentClientProps & BYOCServerProps;
56
+ type ErrorComponentProps = {
57
+ [prop: string]: unknown;
58
+ error?: Error;
59
+ };
60
+ /**
61
+ * BYOCComponent facilitate the rendering of external components. It manages potential errors,
62
+ * missing components, and customization of error messages or alternative rendering components.
63
+ * @param {ByocComponentProps} props component props
64
+ * @returns dynamicly rendered component or Missing Component error frame
65
+ */
66
+ export declare class BYOCComponent extends React.Component<BYOCComponentProps> {
67
+ state: Readonly<{
68
+ error?: Error;
69
+ }>;
70
+ constructor(props: BYOCComponentProps);
71
+ static getDerivedStateFromError(error: Error): {
72
+ error: Error;
73
+ };
74
+ componentDidCatch(error: Error): void;
75
+ render(): JSX.Element;
76
+ }
77
+ /**
78
+ * Fetches server component props required for server rendering, based on rendering params.
79
+ * @param {BYOCComponentParams} params component params
80
+ */
81
+ export declare function fetchBYOCComponentServerProps(params: BYOCComponentParams): Promise<BYOCComponentProps>;
82
+ export {};
@@ -0,0 +1,3 @@
1
+ import { BYOCComponentProps } from './BYOCComponent';
2
+ export declare const BYOC_WRAPPER_RENDERING_NAME = "BYOCWrapper";
3
+ export declare const BYOCWrapper: (props: BYOCComponentProps) => JSX.Element;
@@ -1,3 +1,4 @@
1
+ import * as FEAAS from '@sitecore-feaas/clientside/react';
1
2
  import { ComponentFields, LayoutServicePageState } from '@sitecore-jss/sitecore-jss/layout';
2
3
  export declare const FEAAS_COMPONENT_RENDERING_NAME = "FEaaSComponent";
3
4
  /**
@@ -24,14 +25,14 @@ type FEaaSComponentServerProps = {
24
25
  * HTML template for presentation rendered inside the component
25
26
  */
26
27
  template?: string;
27
- /**
28
- * the date component data was last modified
29
- */
30
- lastModified?: string;
31
28
  /**
32
29
  * Default revision to be fetched. Should be 'staged' for editing/preview. Can be overriden by params.ComponentRevision
33
30
  */
34
31
  revisionFallback?: RevisionType;
32
+ /**
33
+ * Fetched data from server component props for server rendering, based on rendering params.
34
+ */
35
+ fetchedData?: FEAAS.DataScopes;
35
36
  };
36
37
  /**
37
38
  * FEaaS props for client side component. Should be used as fallback when server props are not provided.
@@ -0,0 +1,3 @@
1
+ import { FEaaSComponentProps } from './FEaaSComponent';
2
+ export declare const FEAAS_WRAPPER_RENDERING_NAME = "FEaaSWrapper";
3
+ export declare const FEaaSWrapper: (props: FEaaSComponentProps) => JSX.Element;
@@ -3,5 +3,6 @@ export interface MissingComponentProps {
3
3
  rendering?: {
4
4
  componentName?: string;
5
5
  };
6
+ errorOverride?: string;
6
7
  }
7
8
  export declare const MissingComponent: React.FC<MissingComponentProps>;
package/types/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { LayoutService, LayoutServiceData, LayoutServicePageState, LayoutService
4
4
  export { trackingApi, TrackingRequestOptions, CampaignInstance, GoalInstance, OutcomeInstance, EventInstance, PageViewInstance, } from '@sitecore-jss/sitecore-jss/tracking';
5
5
  export { DictionaryPhrases, DictionaryService, GraphQLDictionaryService, RestDictionaryService, } from '@sitecore-jss/sitecore-jss/i18n';
6
6
  export { mediaApi } from '@sitecore-jss/sitecore-jss/media';
7
+ export { getFEAASLibraryStylesheetLinks } from '@sitecore-jss/sitecore-jss/feaas';
7
8
  export { ComponentFactory } from './components/sharedTypes';
8
9
  export { Placeholder, PlaceholderComponentProps } from './components/Placeholder';
9
10
  export { Image, ImageProps, ImageField, getEEMarkup, ImageFieldValue, ImageSizeParameters, } from './components/Image';
@@ -11,6 +12,9 @@ export { RichText, RichTextProps, RichTextPropTypes, RichTextField } from './com
11
12
  export { Text, TextField } from './components/Text';
12
13
  export { DateField, DateFieldProps } from './components/Date';
13
14
  export { FEaaSComponent, FEaaSComponentProps, FEaaSComponentParams, fetchFEaaSComponentServerProps, } from './components/FEaaSComponent';
15
+ export { FEaaSWrapper } from './components/FEaaSWrapper';
16
+ export { BYOCComponent, BYOCComponentParams, BYOCComponentProps, fetchBYOCComponentServerProps, } from './components/BYOCComponent';
17
+ export { BYOCWrapper } from './components/BYOCWrapper';
14
18
  export { Link, LinkField, LinkFieldValue, LinkProps, LinkPropTypes } from './components/Link';
15
19
  export { File, FileField } from './components/File';
16
20
  export { VisitorIdentification } from './components/VisitorIdentification';
package/types/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ComponentFields } from '@sitecore-jss/sitecore-jss/layout';
1
2
  export declare const convertKebabCasetoCamelCase: (str: string) => string;
2
3
  /**
3
4
  * https://facebook.github.io/react/docs/dom-elements.html
@@ -18,8 +19,24 @@ export declare const convertAttributesToReactProps: (attributes: {
18
19
  /**
19
20
  * "class" property will be transformed into or appended to "className" instead.
20
21
  * @param {string} otherAttrs all other props included on the image component
21
- * * @returns {void}
22
+ * @returns {void}
22
23
  */
23
24
  export declare const addClassName: (otherAttrs: {
24
25
  [key: string]: unknown;
25
26
  }) => void;
27
+ /**
28
+ * Converts the given tag attributes object to a string
29
+ * @param {Object.<string, unknown>} attributes the attributes object
30
+ * @returns {string} string representation of the attributes
31
+ */
32
+ export declare const getAttributesString: (attributes: {
33
+ [key: string]: unknown;
34
+ }) => string;
35
+ /**
36
+ * Used in FEAAS and BYOC implementations to convert datasource item field values into component props
37
+ * @param {ComponentFields} fields field collection from Sitecore
38
+ * @returns JSON object that can be used as props
39
+ */
40
+ export declare const getDataFromFields: (fields: ComponentFields) => {
41
+ [key: string]: unknown;
42
+ };