Skip to content

Commit

Permalink
useId
Browse files Browse the repository at this point in the history
  • Loading branch information
mk668a committed Jul 29, 2022
1 parent 859f21f commit 67bbc24
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useDebugValue,
useDeferredValue,
useTransition,
useId,
} from "react";

export const hooks = Object.freeze({
Expand Down Expand Up @@ -52,6 +53,7 @@ export const defaultScope = {
useDebugValue,
useDeferredValue,
useTransition,
useId,
};

export const isDevelop = window.location.hostname === "localhost";
Expand Down
68 changes: 65 additions & 3 deletions src/pages/useId.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
import React, { FunctionComponent } from "react";
import React, { FunctionComponent, useId } from "react";
import { hooks } from "../constants";
import { Layout } from "../layout/layout";

type Props = {
title: string;
errorMessage?: string;
};
const InputBlock = ({ title, errorMessage }: Props) => {
const errorMessageId = useId();

return (
<div>
<label>
<span role="textbox">{title}</span>
<input
type="text"
aria-invalid={!!errorMessage}
aria-errormessage={errorMessageId}
/>
</label>
{!!errorMessage && <h3>{errorMessage}</h3>}
<p>errorMessageId: {errorMessageId}</p>
</div>
);
};

export const UseId: FunctionComponent = () => {
return (
<Layout title={hooks.useId}>
<></>
<Layout title={hooks.useId} code={code}>
<>
<InputBlock title="first_name" errorMessage="error" />
<hr />
<InputBlock title="last_name" />
</>
</Layout>
);
};

const code = `
const InputBlock = ({ title, errorMessage }: Props) => {
const errorMessageId = useId();
return (
<div>
<label>
<span role="textbox">{title}</span>
<input
type="text"
aria-invalid={!!errorMessage}
aria-errormessage={errorMessageId}
/>
</label>
{!!errorMessage && <h3>{errorMessage}</h3>}
<p>errorMessageId: {errorMessageId}</p>
</div>
);
};
const UseId = () => {
return (
<>
<InputBlock title="first_name" errorMessage="error" />
<hr />
<InputBlock title="last_name" />
</>
);
};
render(
<UseId />
)
`.trim();

0 comments on commit 67bbc24

Please sign in to comment.