-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(IdPrefix): add component, docs, tests (#12442)
* feat(IdPrefix): add component, docs, tests * feat(IdPrefix): add component, docs, tests * fix(IdPrefix): update useId to use prefix, tests, and export IdPrefix * chore: update snaps * docs(IdPrefix): fix stories * test(snaps): update snap Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d3f351d
commit 133d294
Showing
11 changed files
with
230 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Story, Props, Source, Preview } from '@storybook/addon-docs/blocks'; | ||
import { IdPrefix } from '../IdPrefix'; | ||
|
||
# Prefix | ||
|
||
[Source code](https://github.com/carbon-design-system/carbon/tree/main/packages/react/src/components/ClassPrefix) | ||
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
|
||
## Table of Contents | ||
|
||
- [Overview](#overview) | ||
- [Component API](#component-api) | ||
- [Feedback](#feedback) | ||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
## Overview | ||
|
||
The `IdPrefix` component is used to change the prefix applied to the | ||
automatically generated `id` attributes placed on certain DOM elements. | ||
|
||
<Preview> | ||
<Story id="components-idprefix--default" /> | ||
</Preview> | ||
|
||
This component is used intended to be used in limited cases, primarily only if | ||
you have id conflics when using v10 and v11 packages at the same time during | ||
migration. | ||
|
||
In React, you can use `IdPrefix` anywhere in your component tree and specify the | ||
prefix with the `prefix` prop. Most often it's used in the project root wrapping | ||
the entire project: | ||
|
||
```jsx | ||
import { IdPrefix } from '@carbon/react'; | ||
|
||
export default function MyApp() { | ||
return ( | ||
<IdPrefix prefix="custom"> | ||
<Page /> | ||
</IdPrefix> | ||
); | ||
} | ||
``` | ||
|
||
## Component API | ||
|
||
<Props /> | ||
|
||
## Feedback | ||
|
||
Help us improve this component by providing feedback, asking questions on Slack, | ||
or updating this file on | ||
[GitHub](https://github.com/carbon-design-system/carbon/edit/main/packages/react/src/components/ClassPrefix/ClassPrefix.mdx). |
37 changes: 37 additions & 0 deletions
37
packages/react/src/components/IdPrefix/IdPrefix.stories.js
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,37 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { IdPrefix } from '.'; | ||
import { useIdPrefix } from '../../internal/useIdPrefix'; | ||
import mdx from './IdPrefix.mdx'; | ||
|
||
export default { | ||
title: 'Components/IdPrefix', | ||
component: IdPrefix, | ||
parameters: { | ||
docs: { | ||
page: mdx, | ||
}, | ||
}, | ||
}; | ||
|
||
export const Default = () => { | ||
function ExampleComponent() { | ||
const idPrefix = useIdPrefix(); | ||
return <p>The current id prefix is: {idPrefix}</p>; | ||
} | ||
|
||
return ( | ||
<> | ||
<ExampleComponent /> | ||
<IdPrefix prefix="custom"> | ||
<ExampleComponent /> | ||
</IdPrefix> | ||
</> | ||
); | ||
}; |
31 changes: 31 additions & 0 deletions
31
packages/react/src/components/IdPrefix/__tests__/IdPrefix-test.js
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,31 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { IdPrefix } from '../../IdPrefix'; | ||
import { useIdPrefix } from '../../../internal/useIdPrefix'; | ||
|
||
describe('IdPrefix', () => { | ||
it('should set the prefix value used by usePrefix', () => { | ||
const calls = []; | ||
|
||
function TestComponent() { | ||
const prefix = useIdPrefix(); | ||
calls.push(prefix); | ||
return null; | ||
} | ||
|
||
render( | ||
<IdPrefix prefix="custom"> | ||
<TestComponent /> | ||
</IdPrefix> | ||
); | ||
|
||
expect(calls).toEqual(['custom']); | ||
}); | ||
}); |
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,29 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { IdPrefixContext } from '../../internal/useIdPrefix'; | ||
|
||
function IdPrefix({ children, prefix }) { | ||
return ( | ||
<IdPrefixContext.Provider value={prefix}> | ||
{children} | ||
</IdPrefixContext.Provider> | ||
); | ||
} | ||
|
||
IdPrefix.propTypes = { | ||
children: PropTypes.node, | ||
|
||
/** | ||
* The value used to prefix the auto-generated id placed on some DOM elements | ||
*/ | ||
prefix: PropTypes.string, | ||
}; | ||
|
||
export { IdPrefix }; |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright IBM Corp. 2020 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { cleanup, render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { useIdPrefix, IdPrefixContext } from '../useIdPrefix'; | ||
|
||
describe('usePrefix', () => { | ||
afterEach(cleanup); | ||
|
||
it('should emit the default prefix without context', () => { | ||
let value = null; | ||
|
||
function TestComponent() { | ||
value = useIdPrefix(); | ||
return null; | ||
} | ||
|
||
render(<TestComponent />); | ||
expect(value).toBe(null); | ||
}); | ||
|
||
it('should emit the prefix in context', () => { | ||
function TestComponent() { | ||
const contextValue = useIdPrefix(); | ||
return <span data-testid="test">{contextValue}</span>; | ||
} | ||
|
||
const { getByTestId } = render( | ||
<IdPrefixContext.Provider value="test"> | ||
<TestComponent /> | ||
</IdPrefixContext.Provider> | ||
); | ||
|
||
expect(getByTestId('test')).toHaveTextContent('test'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
export const IdPrefixContext = React.createContext(null); | ||
|
||
export function useIdPrefix() { | ||
return React.useContext(IdPrefixContext); | ||
} |