Skip to content

Commit

Permalink
[sitecore-jss-react] Pass ComponentProps param to BYOCComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
illiakovalenko committed Dec 7, 2023
1 parent 31a4710 commit f0f058b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { BYOCComponent } from './BYOCComponent';
import { MissingComponent, MissingComponentProps } from './MissingComponent';

describe('BYOCComponent', () => {
it('should render with props when ComponentDataOverride is provided', () => {
it('should render with props when ComponentProps is provided', () => {
const mockProps = {
params: {
ComponentName: 'Foo',
ComponentDataOverride: JSON.stringify({ prop1: 'value1' }),
ComponentProps: JSON.stringify({ prop1: 'value1' }),
},
fetchedData: {},
};
Expand Down Expand Up @@ -38,7 +38,7 @@ describe('BYOCComponent', () => {
const mockProps = {
params: {
ComponentName: 'Foo',
ComponentDataOverride: JSON.stringify({ prop1: 'value1' }),
ComponentProps: JSON.stringify({ prop1: 'value1' }),
},
fetchedData,
};
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('BYOCComponent', () => {
const mockProps = {
params: {
ComponentName: 'Foo',
ComponentDataOverride: JSON.stringify({ prop1: 'value1' }),
ComponentProps: JSON.stringify({ prop1: 'value1' }),
},
};
const Foo = () => <p id="foo-content">Test</p>;
Expand All @@ -93,7 +93,7 @@ describe('Error handling', () => {
const props = {
params: {
ComponentName: 'ExampleComponent',
ComponentDataOverride: 'invalid-json',
ComponentProps: 'invalid-json',
},
fetchedData: {},
};
Expand All @@ -108,7 +108,7 @@ describe('Error handling', () => {
errorComponent: customErrorComponent,
params: {
ComponentName: 'ExampleComponent',
ComponentDataOverride: 'invalid-json',
ComponentProps: 'invalid-json',
},
fetchedData: {},
};
Expand Down
8 changes: 6 additions & 2 deletions packages/sitecore-jss-react/src/components/BYOCComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export type BYOCComponentParams = {
/**
* JSON props to pass into rendered component
*/
ComponentProps?: string;
/**
* A JSON object with data sources to be fetched and passed to the component
*/
ComponentDataOverride?: string;
/**
* A string with classes that can be used to apply themes, via SXA functionality
Expand Down Expand Up @@ -133,9 +137,9 @@ export class BYOCComponent extends React.Component<BYOCComponentProps> {

let componentProps: { [key: string]: any } = null;

if (props.params?.ComponentDataOverride) {
if (props.params?.ComponentProps) {
try {
componentProps = JSON.parse(props.params.ComponentDataOverride) ?? {};
componentProps = JSON.parse(props.params.ComponentProps) ?? {};
} catch (e) {
console.error(
`Parsing props for ${componentName} component from rendering params failed. Error: ${e}`
Expand Down

0 comments on commit f0f058b

Please sign in to comment.