-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wb1812.2.idcomponent] Add the Id component
- Loading branch information
1 parent
50269c0
commit 4056bee
Showing
5 changed files
with
76 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/wonder-blocks-core": minor | ||
--- | ||
|
||
- Add the `Id` component for cases where `useId` cannot be used directly |
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,32 @@ | ||
import * as React from "react"; | ||
|
||
import {Meta} from "@storybook/react"; | ||
import {View, Id} from "@khanacademy/wonder-blocks-core"; | ||
import {Body, BodyMonospace} from "@khanacademy/wonder-blocks-typography"; | ||
import {Strut} from "@khanacademy/wonder-blocks-layout"; | ||
import {spacing} from "@khanacademy/wonder-blocks-tokens"; | ||
|
||
export default { | ||
title: "Packages / Core / Id", | ||
|
||
parameters: { | ||
chromatic: { | ||
// We don't need a snapshot for this. | ||
disableSnapshot: true, | ||
}, | ||
}, | ||
} as Meta; | ||
|
||
export const BasicExample = () => ( | ||
<View> | ||
<Id> | ||
{(id) => ( | ||
<View style={{flexDirection: "row"}}> | ||
<Body>Generated identifier: </Body> | ||
<Strut size={spacing.xSmall_8} /> | ||
<BodyMonospace>{id}</BodyMonospace> | ||
</View> | ||
)} | ||
</Id> | ||
</View> | ||
); |
17 changes: 17 additions & 0 deletions
17
packages/wonder-blocks-core/src/components/__tests__/id.test.tsx
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,17 @@ | ||
import * as React from "react"; | ||
|
||
import {render} from "@testing-library/react"; | ||
import {Id} from "../id"; | ||
|
||
describe("Id", () => { | ||
it("should provide an id to the children", () => { | ||
// Arrange | ||
const childrenFn = jest.fn().mockReturnValue(null); | ||
|
||
// Act | ||
render(<Id>{childrenFn}</Id>); | ||
|
||
// Assert | ||
expect(childrenFn).toHaveBeenCalledWith(expect.any(String)); | ||
}); | ||
}); |
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,21 @@ | ||
import {useId} from "react"; | ||
import * as React from "react"; | ||
|
||
type Props = { | ||
/** | ||
* A function that to render children with the given identifier. | ||
*/ | ||
children: (id: string) => React.ReactNode; | ||
}; | ||
|
||
/** | ||
* A component that provides a unique identifier to its children. | ||
* | ||
* This component is useful when you need to generate unique identifiers for | ||
* elements in components that cannot use hooks. Where possible, use `useId` | ||
* instead. | ||
*/ | ||
export const Id = ({children}: Props) => { | ||
const id = useId(); | ||
return <>{children(id)}</>; | ||
}; |
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