Skip to content

Commit 67bbc24

Browse files
committed
useId
1 parent 859f21f commit 67bbc24

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
useDebugValue,
1515
useDeferredValue,
1616
useTransition,
17+
useId,
1718
} from "react";
1819

1920
export const hooks = Object.freeze({
@@ -52,6 +53,7 @@ export const defaultScope = {
5253
useDebugValue,
5354
useDeferredValue,
5455
useTransition,
56+
useId,
5557
};
5658

5759
export const isDevelop = window.location.hostname === "localhost";

src/pages/useId.tsx

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,73 @@
1-
import React, { FunctionComponent } from "react";
1+
import React, { FunctionComponent, useId } from "react";
22
import { hooks } from "../constants";
33
import { Layout } from "../layout/layout";
44

5+
type Props = {
6+
title: string;
7+
errorMessage?: string;
8+
};
9+
const InputBlock = ({ title, errorMessage }: Props) => {
10+
const errorMessageId = useId();
11+
12+
return (
13+
<div>
14+
<label>
15+
<span role="textbox">{title}</span>
16+
<input
17+
type="text"
18+
aria-invalid={!!errorMessage}
19+
aria-errormessage={errorMessageId}
20+
/>
21+
</label>
22+
{!!errorMessage && <h3>{errorMessage}</h3>}
23+
<p>errorMessageId: {errorMessageId}</p>
24+
</div>
25+
);
26+
};
27+
528
export const UseId: FunctionComponent = () => {
629
return (
7-
<Layout title={hooks.useId}>
8-
<></>
30+
<Layout title={hooks.useId} code={code}>
31+
<>
32+
<InputBlock title="first_name" errorMessage="error" />
33+
<hr />
34+
<InputBlock title="last_name" />
35+
</>
936
</Layout>
1037
);
1138
};
39+
40+
const code = `
41+
const InputBlock = ({ title, errorMessage }: Props) => {
42+
const errorMessageId = useId();
43+
44+
return (
45+
<div>
46+
<label>
47+
<span role="textbox">{title}</span>
48+
<input
49+
type="text"
50+
aria-invalid={!!errorMessage}
51+
aria-errormessage={errorMessageId}
52+
/>
53+
</label>
54+
{!!errorMessage && <h3>{errorMessage}</h3>}
55+
<p>errorMessageId: {errorMessageId}</p>
56+
</div>
57+
);
58+
};
59+
60+
const UseId = () => {
61+
return (
62+
<>
63+
<InputBlock title="first_name" errorMessage="error" />
64+
<hr />
65+
<InputBlock title="last_name" />
66+
</>
67+
);
68+
};
69+
70+
render(
71+
<UseId />
72+
)
73+
`.trim();

0 commit comments

Comments
 (0)