Skip to content

Commit c9661dc

Browse files
committed
Use useId hook where applicable
1 parent c65a6bc commit c9661dc

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useId } from 'react';
12
import { getISOLocalDate } from '@wojtekmaj/date-utils';
23

34
type ValidityOptionsProps = {
@@ -17,6 +18,10 @@ export default function ValidityOptions({
1718
setMinDate,
1819
setRequired,
1920
}: ValidityOptionsProps) {
21+
const minDateId = useId();
22+
const maxDateId = useId();
23+
const requiredId = useId();
24+
2025
function onMinChange(event: React.ChangeEvent<HTMLInputElement>) {
2126
const { value } = event.target;
2227

@@ -34,9 +39,9 @@ export default function ValidityOptions({
3439
<legend>Minimum and maximum date</legend>
3540

3641
<div>
37-
<label htmlFor="minDate">Minimum date</label>
42+
<label htmlFor={minDateId}>Minimum date</label>
3843
<input
39-
id="minDate"
44+
id={minDateId}
4045
onChange={onMinChange}
4146
type="datetime-local"
4247
value={minDate ? getISOLocalDate(minDate) : ''}
@@ -48,9 +53,9 @@ export default function ValidityOptions({
4853
</div>
4954

5055
<div>
51-
<label htmlFor="maxDate">Maximum date</label>
56+
<label htmlFor={maxDateId}>Maximum date</label>
5257
<input
53-
id="maxDate"
58+
id={maxDateId}
5459
onChange={onMaxChange}
5560
type="datetime-local"
5661
value={maxDate ? getISOLocalDate(maxDate) : ''}
@@ -64,11 +69,11 @@ export default function ValidityOptions({
6469
<div>
6570
<input
6671
checked={required}
67-
id="required"
72+
id={requiredId}
6873
onChange={(event) => setRequired(event.target.checked)}
6974
type="checkbox"
7075
/>
71-
<label htmlFor="required">Required</label>
76+
<label htmlFor={requiredId}>Required</label>
7277
</div>
7378
</fieldset>
7479
);

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 { getISOLocalDateTime } 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 datetimeId = useId();
13+
1114
const [date] = 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 date and time externally</legend>
2225

2326
<div>
24-
<label htmlFor="datetime">Date and time</label>
27+
<label htmlFor={datetimeId}>Date and time</label>
2528
<input
26-
id="datetime"
29+
id={datetimeId}
2730
onChange={onChange}
2831
type="datetime-local"
2932
value={date && date instanceof Date ? getISOLocalDateTime(date) : date || undefined}

test/ViewOptions.tsx

Lines changed: 18 additions & 10 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;
@@ -23,6 +25,12 @@ export default function ViewOptions({
2325
showNeighboringMonth,
2426
showWeekNumbers,
2527
}: ViewOptionsProps) {
28+
const disabledId = useId();
29+
const showLeadingZerosId = useId();
30+
const showWeekNumbersId = useId();
31+
const showNeighboringMonthId = useId();
32+
const renderInPortalId = useId();
33+
2634
function onDisabledChange(event: React.ChangeEvent<HTMLInputElement>) {
2735
const { checked } = event.target;
2836

@@ -58,48 +66,48 @@ export default function ViewOptions({
5866
<legend>View options</legend>
5967

6068
<div>
61-
<input checked={disabled} id="disabled" onChange={onDisabledChange} type="checkbox" />
62-
<label htmlFor="disabled">Disabled</label>
69+
<input checked={disabled} id={disabledId} onChange={onDisabledChange} type="checkbox" />
70+
<label htmlFor={disabledId}>Disabled</label>
6371
</div>
6472

6573
<div>
6674
<input
6775
checked={showLeadingZeros}
68-
id="showLeadingZeros"
76+
id={showLeadingZerosId}
6977
onChange={onShowLeadingZerosChange}
7078
type="checkbox"
7179
/>
72-
<label htmlFor="showLeadingZeros">Show leading zeros</label>
80+
<label htmlFor={showLeadingZerosId}>Show leading zeros</label>
7381
</div>
7482

7583
<div>
7684
<input
7785
checked={showWeekNumbers}
78-
id="showWeekNumbers"
86+
id={showWeekNumbersId}
7987
onChange={onShowWeekNumbersChange}
8088
type="checkbox"
8189
/>
82-
<label htmlFor="showWeekNumbers">Show week numbers</label>
90+
<label htmlFor={showWeekNumbersId}>Show week numbers</label>
8391
</div>
8492

8593
<div>
8694
<input
8795
checked={showNeighboringMonth}
88-
id="showNeighboringMonth"
96+
id={showNeighboringMonthId}
8997
onChange={onShowNeighboringMonthChange}
9098
type="checkbox"
9199
/>
92-
<label htmlFor="showNeighboringMonth">Show neighboring month's days</label>
100+
<label htmlFor={showNeighboringMonthId}>Show neighboring month's days</label>
93101
</div>
94102

95103
<div>
96104
<input
97105
checked={renderInPortal}
98-
id="renderInPortal"
106+
id={renderInPortalId}
99107
onChange={onRenderInPortalChange}
100108
type="checkbox"
101109
/>
102-
<label htmlFor="renderInPortal">Render in portal</label>
110+
<label htmlFor={renderInPortalId}>Render in portal</label>
103111
</div>
104112
</fieldset>
105113
);

0 commit comments

Comments
 (0)