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

feat(atomic): expose idiomatic react templating functionality #1677

Merged
merged 5 commits into from
Jan 19, 2022

Conversation

olamothe
Copy link
Member

As discussed in scrum, the goal is to expose a more natural React pattern to render result templates VS the Web Component/HTML way.

Changes included in this PR:

  • Remove atomic-result-template and atomic-field-condition from the output target (These components will no longer appear under @coveo/atomic-react
  • Add a setRenderFunction method to the core Atomic result list web component, which accept a function that receive a result as a parameter, and has to return an HTMLElement
  • In atomic-react, remove the ResultTemplate wrapper, and instead expose a ResultListWrapper, that shadows AtomicResultList, with one additional prop: template, which accept a result as a parameter and must return a JSX.Element. It then does the necessary work to change this to an HTMLElement, which is what Atomic expects.

Also included in this PR:
A patch to fix an issue with the react output plugin, which does not properly camelToDashCase HTML attributes.

https://coveord.atlassian.net/browse/KIT-1336

@olamothe olamothe requested a review from a team as a code owner January 18, 2022 19:25
@github-actions
Copy link

Thanks for your contribution @olamothe !
When your pull-request is ready to be merged, check the box below to merge it

  • Merge! :shipit:

@github-actions
Copy link

github-actions bot commented Jan 18, 2022

Pull Request Report

PR Title

✅ Title follows the conventional commit spec.

Bundle Size

File Old (kb) New (kb) Change (%)
case-assist 185.9 185.9 0
search 262.3 262.3 0
product-listing 201.2 201.2 0
product-recommendation 185.4 185.4 0
recommendation 183.9 183.9 0

@@ -182,7 +197,9 @@ export class AtomicResultList implements InitializableComponent {

private buildListResults() {
return this.resultListState.results.map((result) => {
const template = this.getTemplate(result);
const content = this.renderingFunction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More simple than I expected 😄

@@ -90,6 +99,12 @@ export class AtomicResultList implements InitializableComponent {
*/
@Prop() image: ResultDisplayImageSize = 'icon';

private renderingFunction?: (result: Result) => HTMLElement = undefined;

@Method() public setRenderFunction(render: (result: Result) => HTMLElement) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we set this as @internal ?

Copy link
Member Author

@olamothe olamothe Jan 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this only for documentation ?
Or does it have any other impact to do that ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking of docs, a line to explain what this function is for would be useful, especially since it's not directly used inside Atomic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether we make it public/documented or not, this will need to be maintained since we use it in atomic-react and it's likely going to be used in other frameworks. I have a slight preference for documenting it.

Copy link
Member Author

@olamothe olamothe Jan 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpmarceau / @npushkarskii : Small review for the comment here please !

Thanks ! 🙏

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olamothe done! 😉

@@ -46,6 +46,7 @@ export const config: Config = {
componentCorePackage: '@coveo/atomic',
proxiesFile: '../atomic-react/src/components/stencil-generated/index.ts',
includeDefineCustomElements: true,
excludeComponents: ['atomic-result-template', 'atomic-field-condition'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit unrelated, but maybe the atomic-external should be investigated and perhaps excluded too if it makes thing too complicated (the react context should remove the necessity for it)


function MyTemplate(result: Result) {
return (
<>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this like a Fragment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a way to return a "single root" for a JSX function, without the need for any extra DOM node.

https://reactjs.org/docs/fragments.html

@@ -90,6 +99,12 @@ export class AtomicResultList implements InitializableComponent {
*/
@Prop() image: ResultDisplayImageSize = 'icon';

private renderingFunction?: (result: Result) => HTMLElement = undefined;

@Method() public setRenderFunction(render: (result: Result) => HTMLElement) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking of docs, a line to explain what this function is for would be useful, especially since it's not directly used inside Atomic

@@ -0,0 +1,21 @@
diff --git a/node_modules/@stencil/react-output-target/react-component-lib/createComponent.tsx b/node_modules/@stencil/react-output-target/react-component-lib/createComponent.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool approach to work-around the issue. It would be nice to open a PR for the stencil package too,

Copy link
Contributor

@btaillon-coveo btaillon-coveo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean!

@npushkarskii npushkarskii removed the request for review from jpmarceau January 19, 2022 15:58
Copy link
Contributor

@npushkarskii npushkarskii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just added a couple of edits 👍

@olamothe olamothe merged commit 0876408 into master Jan 19, 2022
@olamothe olamothe deleted the KIT-1336 branch January 19, 2022 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants