Creating a custom Carbon add-on component
Complete the following steps to create a Carbon add-on component called componentX
and prepare to integrate it into a custom UIM page.
The steps do not need to be done in this order, but it is currently the recommended order.
- Implement a custom React JavaScript component by completing the following steps:
- In the
custom-carbon-addons
package, create a directory for the component in thereact
directory. For example,custom-carbon-addons/src/react/addons/componentX
. - Create the JavaScript files for
componentX
in the directory. To create add-on components that are based on Carbon components, import the Carbon components from thecarbon-components
node module. - You can do your unit tests in parallel to test the generated HTML output from the component.
- In the
- Implement the look and feel of the component with Sass. Ensure that you follow the Carbon and Cúram design guidance.
- In the
custom-carbon-addon/src/scss/addons
directory, create an SCSS file for the component. For example,componentX.scss
. - Import
componentX.scss
into theindex.scss
file.
- In the
- Implement a query provider in Apollo Client. An Apollo Client query provider enables the cached data from GraphQL to be integrated with a React component. Complete the following steps:
- Create a directory in the
custom-carbon-addon/src/apollo-client-hooks/hooks
directory. For example,componentX
. - Create the JavaScript files for your query provider with reference to the samples in
carbon-addons-devenv/src/apollo-client-hooks/hooks/hook_samples
. Use theApolloClientDataProvider
,ApolloClientQueryProvider
, andApolloClientHookTemplate
React helper components incarbon-addons-devenv/src/apollo-client-hooks/hooks/core
to help with this task. - Create a query that accesses data from the back-end by importing
gql
anduseQuery
from@apollo/client
. If you already have a GraphQL query that serves a number of components, you can use that query.
- Create a directory in the
- Test the Carbon component in Storybook. To test
componentX
in storybook, you can test components in isolation before you integrate them into Cúram.- In the
stories
directory of thecustom-carbon-addon
package, create a directory for the component. For example,custom-carbon-addon/stories/componentX
. Runnpm run storybook
to test components.
- In the
- Create a JavaScript renderer function. You can use the
Renderer.create()
helper function to implement a JavaScript Renderer function to integrate the component into a Java Renderer on the UIM page. You can place your renderer functions in thecustom-carbon-addon/renderers
directory. For example, create theCarbonComponentXViewRenderer
function.