|
1 |
| -import React, { FunctionComponent } from "react"; |
| 1 | +import React, { FunctionComponent, useId } from "react"; |
2 | 2 | import { hooks } from "../constants";
|
3 | 3 | import { Layout } from "../layout/layout";
|
4 | 4 |
|
| 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 | + |
5 | 28 | export const UseId: FunctionComponent = () => {
|
6 | 29 | 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 | + </> |
9 | 36 | </Layout>
|
10 | 37 | );
|
11 | 38 | };
|
| 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