-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useComponentPropOverrides hook and withComponentPropOverrid…
…es HOC for custom props
- Loading branch information
1 parent
ba3ff7e
commit 5b53e4c
Showing
12 changed files
with
768 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,41 @@ | ||
// NOTE: This file is used by the example app. frontend-build expects the file | ||
/* eslint-disable no-console */ | ||
|
||
// NOTE: This file is used by the example app. frontend-build expects the file | ||
// to be in the root of the repository. This is not used by the actual frontend-platform library. | ||
// Also note that in an actual application this file would be added to .gitignore. | ||
// Also note that in an actual application, this file would be added to .gitignore. | ||
const config = { | ||
JS_FILE_VAR: 'JS_FILE_VAR_VALUE_FOR_EXAMPLE_APP', | ||
componentPropOverrides: { | ||
targets: { | ||
example: { | ||
'data-dd-privacy': 'mask', // Custom `data-*` attribute (e.g., Datadog) | ||
'data-hj-suppress': '', // Custom `data-*` attribute (e.g., Hotjar) | ||
className: 'fs-mask', // Custom `className` attribute (e.g., Fullstory) | ||
onClick: (e) => { // Custom `onClick` attribute | ||
console.log('[env.config] onClick event for example', e); | ||
}, | ||
style: { // Custom `style` attribute | ||
background: 'blue', | ||
color: 'white', | ||
}, | ||
}, | ||
example2: { | ||
'data-dd-privacy': 'mask', // Custom `data-*` attribute (e.g., Datadog) | ||
'data-hj-suppress': '', // Custom `data-*` attribute (e.g., Hotjar) | ||
className: 'fs-mask', // Custom `className` attribute (e.g., Fullstory) | ||
onClick: (e) => { // Custom `onClick` attribute | ||
console.log('[env.config] onClick event for example2', e); | ||
}, | ||
style: { // Custom `style` attribute | ||
background: 'blue', | ||
color: 'white', | ||
}, | ||
}, | ||
example3: { | ||
'data-dd-action-name': 'example name', // Custom `data-*` attribute (e.g., Datadog) | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { | ||
forwardRef, useContext, useEffect, useRef, useState, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { AppContext, useComponentPropOverrides, withComponentPropOverrides } from '@edx/frontend-platform/react'; | ||
|
||
// Example via `useComponentPropOverrides` (hook) | ||
const ExampleComponentWithDefaultPropOverrides = forwardRef(({ children, ...rest }, ref) => { | ||
const propOverrides = useComponentPropOverrides('example', rest); | ||
return <span ref={ref} {...propOverrides}>{children}</span>; | ||
}); | ||
ExampleComponentWithDefaultPropOverrides.displayName = 'ExampleComponentWithDefaultPropOverrides'; | ||
ExampleComponentWithDefaultPropOverrides.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
const ExampleComponentWithAllowedPropOverrides = forwardRef(({ children, ...rest }, ref) => { | ||
const propOverrides = useComponentPropOverrides('example2', rest, { | ||
allowedPropNames: ['className', 'style', 'onClick'], | ||
}); | ||
return <span ref={ref} {...propOverrides}>{children}</span>; | ||
}); | ||
ExampleComponentWithAllowedPropOverrides.displayName = 'ExampleComponentWithAllowedPropOverrides'; | ||
ExampleComponentWithAllowedPropOverrides.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
// Example via `withComponentPropOverrides` (HOC) | ||
const ExampleComponent = forwardRef(({ children, ...rest }, ref) => ( | ||
<span ref={ref} {...rest}>{children}</span> | ||
)); | ||
ExampleComponent.displayName = 'ExampleComponent'; | ||
ExampleComponent.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
const ExampleComponentWithPropOverrides3 = withComponentPropOverrides('example3')(ExampleComponent); | ||
|
||
function jsonStringify(obj) { | ||
const replacer = (key, value) => { | ||
if (typeof value === 'function') { | ||
return '[Function]'; | ||
} | ||
return value; | ||
}; | ||
return JSON.stringify(obj, replacer, 2); | ||
} | ||
|
||
function useExample() { | ||
const ref = useRef(null); | ||
const [node, setNode] = useState(null); | ||
|
||
useEffect(() => { | ||
if (ref.current) { | ||
setNode(ref.current.outerHTML); | ||
} | ||
}, []); | ||
|
||
return { | ||
ref, | ||
node, | ||
}; | ||
} | ||
|
||
export default function ComponentPropOverridesPage() { | ||
const { config } = useContext(AppContext); | ||
const firstExample = useExample(); | ||
const secondExample = useExample(); | ||
const thirdExample = useExample(); | ||
|
||
const { componentPropOverrides } = config; | ||
|
||
return ( | ||
<div> | ||
<h1>Example usage of <code>componentPropOverrides</code> from configuration</h1> | ||
|
||
<h2>Current configuration</h2> | ||
{componentPropOverrides ? ( | ||
<pre> | ||
{jsonStringify({ componentPropOverrides })} | ||
</pre> | ||
) : ( | ||
<p> | ||
No <code>componentPropOverrides</code> configuration found. Consider updating this | ||
application's <code>env.config.js</code> to configure any custom props. | ||
</p> | ||
)} | ||
|
||
<h2>Examples</h2> | ||
<p> | ||
The following examples below demonstrate | ||
using <code>useComponentPropOverrides</code> and <code>withComponentPropOverrides</code> to | ||
extend any component's base props based on the application's configuration. Inspect the DOM | ||
elements for the rendered example components below to observe the configured attributes/values. | ||
</p> | ||
|
||
{/* Example 1 (useComponentPropOverrides) */} | ||
<h3><code>useComponentPropOverrides</code> (hook)</h3> | ||
<h4>Default support prop overrides</h4> | ||
<p> | ||
By default, only <code>data-*</code> attributes and <code>className</code> props are | ||
supported; other props will be ignored. You may opt-in to non-default prop | ||
overrides by extending the <code>allowedPropNames</code> option. | ||
</p> | ||
<p> | ||
<ExampleComponentWithDefaultPropOverrides | ||
ref={firstExample.ref} | ||
// eslint-disable-next-line no-console | ||
onClick={(e) => console.log('ExampleComponentWithPropOverrides clicked', e)} | ||
style={{ borderBottom: '4px solid red' }} | ||
className="example-class" | ||
> | ||
Example 1 | ||
</ExampleComponentWithDefaultPropOverrides> | ||
</p> | ||
<i>Result:</i>{' '} | ||
<pre> | ||
<code>{firstExample.node}</code> | ||
</pre> | ||
|
||
{/* Example 2 (useComponentPropOverrides) */} | ||
<h4>Opt-in to specific prop overrides with <code>allowedPropNames</code></h4> | ||
<p> | ||
<ExampleComponentWithAllowedPropOverrides | ||
ref={secondExample.ref} | ||
// eslint-disable-next-line no-console | ||
onClick={(e) => console.log('ExampleComponentWithPropOverrides clicked', e)} | ||
style={{ borderBottom: '4px solid red' }} | ||
className="example-class" | ||
> | ||
Example 2 | ||
</ExampleComponentWithAllowedPropOverrides> | ||
</p> | ||
<i>Result:</i>{' '} | ||
<pre> | ||
<code>{secondExample.node}</code> | ||
</pre> | ||
|
||
{/* Example 3 (withComponentPropOverrides) */} | ||
<h3><code>withComponentPropOverrides</code> (HOC)</h3> | ||
<p> | ||
<ExampleComponentWithPropOverrides3 ref={thirdExample.ref}> | ||
Example 3 | ||
</ExampleComponentWithPropOverrides3> | ||
</p> | ||
<i>Result:</i>{' '} | ||
<pre> | ||
<code>{thirdExample.node}</code> | ||
</pre> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.