Skip to content

Commit bd402ef

Browse files
dakahnjoshblackjnm2377kodiakhq[bot]
authored
feat(react): add usePrefix hook and examples (#9687)
* feat(Carbon-React): add usePrefix hook and examples * chore(test): update snapshots * chore(test): update snapshots * chore(test): remove test on prefix class * chore(test): add usePrefix unit tests Co-authored-by: Josh Black <josh@josh.black> Co-authored-by: Josefina Mancilla <32556167+jnm2377@users.noreply.github.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent d577bc7 commit bd402ef

File tree

7 files changed

+82
-16
lines changed

7 files changed

+82
-16
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,24 @@ Map {
875875
},
876876
},
877877
"ContentSwitcher" => Object {
878+
"contextType": Object {
879+
"$$typeof": Symbol(react.context),
880+
"Consumer": Object {
881+
"$$typeof": Symbol(react.context),
882+
"_calculateChangedBits": null,
883+
"_context": [Circular],
884+
},
885+
"Provider": Object {
886+
"$$typeof": Symbol(react.provider),
887+
"_context": [Circular],
888+
},
889+
"_calculateChangedBits": null,
890+
"_currentRenderer": null,
891+
"_currentRenderer2": null,
892+
"_currentValue": "bx",
893+
"_currentValue2": "bx",
894+
"_threadCount": 0,
895+
},
878896
"defaultProps": Object {
879897
"onChange": [Function],
880898
"selectedIndex": 0,

packages/react/src/components/Accordion/Accordion.Skeleton.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import PropTypes from 'prop-types';
99
import React from 'react';
1010
import cx from 'classnames';
1111
import { ChevronRight16 } from '@carbon/icons-react';
12-
import { settings } from 'carbon-components';
1312
import SkeletonText from '../SkeletonText';
1413
import deprecate from '../../prop-types/deprecate';
15-
16-
const { prefix } = settings;
14+
import { usePrefix } from '../../internal/usePrefix';
1715

1816
function AccordionSkeleton({ align, open, count, className, ...rest }) {
17+
const prefix = usePrefix();
1918
const classes = cx(`${prefix}--accordion`, `${prefix}--skeleton`, className, {
2019
[`${prefix}--accordion--${align}`]: align,
2120
});
@@ -77,6 +76,7 @@ AccordionSkeleton.defaultProps = {
7776
};
7877

7978
function AccordionSkeletonItem() {
79+
const prefix = usePrefix();
8080
return (
8181
<li className={`${prefix}--accordion__item`}>
8282
<span className={`${prefix}--accordion__heading`}>

packages/react/src/components/Accordion/Accordion.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import { settings } from 'carbon-components';
98
import cx from 'classnames';
9+
import { usePrefix } from '../../internal/usePrefix';
1010
import PropTypes from 'prop-types';
1111
import React from 'react';
1212

13-
const { prefix } = settings;
14-
1513
function Accordion({
1614
align,
1715
children,
@@ -20,6 +18,7 @@ function Accordion({
2018
size,
2119
...rest
2220
}) {
21+
const prefix = usePrefix();
2322
const className = cx(`${prefix}--accordion`, customClassName, {
2423
[`${prefix}--accordion--${align}`]: align,
2524
[`${prefix}--accordion--${size}`]: size,

packages/react/src/components/ContentSwitcher/ContentSwitcher-test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import React from 'react';
99
import ContentSwitcher from '../ContentSwitcher';
1010
import Switch from '../Switch';
1111
import { mount, shallow } from 'enzyme';
12-
import { settings } from 'carbon-components';
13-
14-
const { prefix } = settings;
1512

1613
describe('ContentSwitcher', () => {
1714
describe('component initial rendering', () => {
@@ -24,10 +21,6 @@ describe('ContentSwitcher', () => {
2421

2522
const children = wrapper.find(Switch);
2623

27-
it('should have the correct class', () => {
28-
expect(wrapper.hasClass(`${prefix}--content-switcher`)).toEqual(true);
29-
});
30-
3124
it('should render children as expected', () => {
3225
expect(children.length).toEqual(2);
3326
});

packages/react/src/components/ContentSwitcher/ContentSwitcher.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
import PropTypes from 'prop-types';
99
import React from 'react';
1010
import classNames from 'classnames';
11-
import { settings } from 'carbon-components';
1211
import { composeEventHandlers } from '../../tools/events';
1312
import { getNextIndex, matches, keys } from '../../internal/keyboard';
1413
import deprecate from '../../prop-types/deprecate';
15-
16-
const { prefix } = settings;
14+
import { PrefixContext } from '../../internal/usePrefix';
1715

1816
export default class ContentSwitcher extends React.Component {
1917
/**
@@ -68,6 +66,8 @@ export default class ContentSwitcher extends React.Component {
6866
size: PropTypes.oneOf(['sm', 'md', 'lg', 'xl']),
6967
};
7068

69+
static contextType = PrefixContext;
70+
7171
static defaultProps = {
7272
selectedIndex: 0,
7373
selectionMode: 'automatic',
@@ -130,6 +130,7 @@ export default class ContentSwitcher extends React.Component {
130130
};
131131

132132
render() {
133+
const prefix = this.context;
133134
const {
134135
children,
135136
className,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright IBM Corp. 2020
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import { cleanup, render } from '@testing-library/react';
9+
import React from 'react';
10+
import { usePrefix, PrefixContext } from '../usePrefix';
11+
12+
describe('usePrefix', () => {
13+
afterEach(cleanup);
14+
15+
it('should emit the default prefix without context', () => {
16+
let value = null;
17+
18+
function TestComponent() {
19+
value = usePrefix();
20+
return null;
21+
}
22+
23+
render(<TestComponent />);
24+
expect(value).toBe('bx');
25+
});
26+
27+
it('should emit the prefix in context', () => {
28+
function TestComponent() {
29+
return <span data-testid="test">test</span>;
30+
}
31+
32+
const { getByTestId } = render(
33+
<PrefixContext.Provider value="test">
34+
<TestComponent />
35+
</PrefixContext.Provider>
36+
);
37+
38+
expect(getByTestId('test')).toHaveTextContent('test');
39+
});
40+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2018
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import { settings } from 'carbon-components';
9+
import React from 'react';
10+
11+
export const PrefixContext = React.createContext(settings.prefix);
12+
13+
export function usePrefix() {
14+
return React.useContext(PrefixContext);
15+
}

0 commit comments

Comments
 (0)