Skip to content

Commit 937df84

Browse files
committed
Use useId hook where applicable
1 parent 494ebd1 commit 937df84

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed

test/LocaleOptions.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { useRef } from 'react';
1+
import { useId, useRef } from 'react';
22

33
type LocaleOptionsProps = {
44
locale: string | undefined;
55
setLocale: (locale: string | undefined) => void;
66
};
77

88
export default function LocaleOptions({ locale, setLocale }: LocaleOptionsProps) {
9+
const localeDefaultId = useId();
10+
const localeEnUSId = useId();
11+
const localeFrFRId = useId();
12+
const localeArEGId = useId();
13+
const customLocaleId = useId();
914
const customLocale = useRef<HTMLInputElement>(null);
1015

1116
function onChange(event: React.ChangeEvent<HTMLInputElement>) {
@@ -38,54 +43,54 @@ export default function LocaleOptions({ locale, setLocale }: LocaleOptionsProps)
3843
<div>
3944
<input
4045
checked={locale === undefined}
41-
id="localeDefault"
46+
id={localeDefaultId}
4247
name="locale"
4348
onChange={onChange}
4449
type="radio"
4550
value="undefined"
4651
/>
47-
<label htmlFor="localeDefault">Auto</label>
52+
<label htmlFor={localeDefaultId}>Auto</label>
4853
</div>
4954
<div>
5055
<input
5156
checked={locale === 'en-US'}
52-
id="localeEnUS"
57+
id={localeEnUSId}
5358
name="locale"
5459
onChange={onChange}
5560
type="radio"
5661
value="en-US"
5762
/>
58-
<label htmlFor="localeEnUS">en-US</label>
63+
<label htmlFor={localeEnUSId}>en-US</label>
5964
</div>
6065
<div>
6166
<input
6267
checked={locale === 'fr-FR'}
63-
id="localeFrFR"
68+
id={localeFrFRId}
6469
name="locale"
6570
onChange={onChange}
6671
type="radio"
6772
value="fr-FR"
6873
/>
69-
<label htmlFor="localeFrFR">fr-FR</label>
74+
<label htmlFor={localeFrFRId}>fr-FR</label>
7075
</div>
7176
<div>
7277
<input
7378
checked={locale === 'ar-EG'}
74-
id="localeArEG"
79+
id={localeArEGId}
7580
name="locale"
7681
onChange={onChange}
7782
type="radio"
7883
value="ar-EG"
7984
/>
80-
<label htmlFor="localeArEG">ar-EG</label>
85+
<label htmlFor={localeArEGId}>ar-EG</label>
8186
</div>
8287
<form onSubmit={onCustomChange}>
83-
<label htmlFor="customLocale">Custom locale:</label>
88+
<label htmlFor={customLocaleId}>Custom locale:</label>
8489
&nbsp;
8590
<input
8691
key={locale}
8792
defaultValue={locale}
88-
id="customLocale"
93+
id={customLocaleId}
8994
name="customLocale"
9095
pattern="^[a-z]{2}(-[A-Z0-9]{2,3})?$"
9196
ref={customLocale}

test/ValidityOptions.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useId } from 'react';
2+
13
type ValidityOptionsProps = {
24
maxTime?: string;
35
minTime?: string;
@@ -15,6 +17,10 @@ export default function ValidityOptions({
1517
setMinTime,
1618
setRequired,
1719
}: ValidityOptionsProps) {
20+
const minTimeId = useId();
21+
const maxTimeId = useId();
22+
const requiredId = useId();
23+
1824
function onMinChange(event: React.ChangeEvent<HTMLInputElement>) {
1925
const { value } = event.target;
2026

@@ -32,17 +38,17 @@ export default function ValidityOptions({
3238
<legend>Minimum and maximum time</legend>
3339

3440
<div>
35-
<label htmlFor="minTime">Minimum time</label>
36-
<input id="minTime" onChange={onMinChange} step="1" type="time" value={minTime || ''} />
41+
<label htmlFor={minTimeId}>Minimum time</label>
42+
<input id={minTimeId} onChange={onMinChange} step="1" type="time" value={minTime || ''} />
3743
&nbsp;
3844
<button onClick={() => setMinTime(undefined)} type="button">
3945
Clear
4046
</button>
4147
</div>
4248

4349
<div>
44-
<label htmlFor="maxTime">Maximum time</label>
45-
<input id="maxTime" onChange={onMaxChange} step="1" type="time" value={maxTime || ''} />
50+
<label htmlFor={maxTimeId}>Maximum time</label>
51+
<input id={maxTimeId} onChange={onMaxChange} step="1" type="time" value={maxTime || ''} />
4652
&nbsp;
4753
<button onClick={() => setMaxTime(undefined)} type="button">
4854
Clear
@@ -52,11 +58,11 @@ export default function ValidityOptions({
5258
<div>
5359
<input
5460
checked={required}
55-
id="required"
61+
id={requiredId}
5662
onChange={(event) => setRequired(event.target.checked)}
5763
type="checkbox"
5864
/>
59-
<label htmlFor="required">Required</label>
65+
<label htmlFor={requiredId}>Required</label>
6066
</div>
6167
</fieldset>
6268
);

test/ValueOptions.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useId } from 'react';
12
import { getHoursMinutesSeconds } from '@wojtekmaj/date-utils';
23

34
import type { LooseValue } from './shared/types.js';
@@ -8,6 +9,8 @@ type ValueOptionsProps = {
89
};
910

1011
export default function ValueOptions({ setValue, value }: ValueOptionsProps) {
12+
const timeId = useId();
13+
1114
const [time] = Array.isArray(value) ? value : [value];
1215

1316
function onChange(event: React.ChangeEvent<HTMLInputElement>) {
@@ -21,9 +24,9 @@ export default function ValueOptions({ setValue, value }: ValueOptionsProps) {
2124
<legend>Set time externally</legend>
2225

2326
<div>
24-
<label htmlFor="time">Time</label>
27+
<label htmlFor={timeId}>Time</label>
2528
<input
26-
id="time"
29+
id={timeId}
2730
onChange={onChange}
2831
step="1"
2932
type="time"

test/ViewOptions.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useId } from 'react';
2+
13
type ViewOptionsProps = {
24
disabled: boolean;
35
renderInPortal: boolean;
@@ -11,6 +13,9 @@ export default function ViewOptions({
1113
setDisabled,
1214
setRenderInPortal,
1315
}: ViewOptionsProps) {
16+
const disabledId = useId();
17+
const renderInPortalId = useId();
18+
1419
function onDisabledChange(event: React.ChangeEvent<HTMLInputElement>) {
1520
const { checked } = event.target;
1621

@@ -28,18 +33,18 @@ export default function ViewOptions({
2833
<legend>View options</legend>
2934

3035
<div>
31-
<input checked={disabled} id="disabled" onChange={onDisabledChange} type="checkbox" />
32-
<label htmlFor="disabled">Disabled</label>
36+
<input checked={disabled} id={disabledId} onChange={onDisabledChange} type="checkbox" />
37+
<label htmlFor={disabledId}>Disabled</label>
3338
</div>
3439

3540
<div>
3641
<input
3742
checked={renderInPortal}
38-
id="renderInPortal"
43+
id={renderInPortalId}
3944
onChange={onRenderInPortalChange}
4045
type="checkbox"
4146
/>
42-
<label htmlFor="renderInPortal">Render in portal</label>
47+
<label htmlFor={renderInPortalId}>Render in portal</label>
4348
</div>
4449
</fieldset>
4550
);

0 commit comments

Comments
 (0)