Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sitecore-jss-react] Pass ComponentProps param to BYOCComponent #1688

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Our versioning strategy is as follows:

### 🐛 Bug Fixes

* [sitecore-jss-react] Use ComponentDataOverride instead of ComponentProps for BYOCComponent ([#1682](https://github.com/Sitecore/jss/pull/1682))
* [sitecore-jss-react] Use ComponentDataOverride instead of ComponentProps for BYOCComponent ([#1682](https://github.com/Sitecore/jss/pull/1682))([#1688](https://github.com/Sitecore/jss/pull/1688))
* `[sitecore-jss-proxy]` Setting "followRedirects" to "true" breaks HEAD requests ([#1630](https://github.com/Sitecore/jss/pull/1630))
* `[templates/nextjs-sxa]` Fix shown horizontal scrollbar in EE mode. ([#1625](https://github.com/Sitecore/jss/pull/1625)), ([#1626](https://github.com/Sitecore/jss/pull/1626))
* `[sitecore-jss-nextjs]` Fix issue when redirects works each every other times. ([#1629](https://github.com/Sitecore/jss/pull/1629))
Expand Down
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