Skip to content

Commit bbaaa00

Browse files
committed
feat(use-id): react에 내장된 useId 훅을 사용하도록 구현 변경
1 parent 94ccad2 commit bbaaa00

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

src/components/Forms/FormControl/__snapshots__/FormControl.test.tsx.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ exports[`FormControl > Snapshot > With multiple field 1`] = `
206206
class="c2 c3"
207207
color="txt-black-darkest"
208208
data-testid="bezier-react-form-label"
209-
for="field-1"
210-
id="field-1-label"
209+
for="field-:r3:"
210+
id="field-:r3:-label"
211211
>
212212
First Inner Label
213213
</label>
@@ -220,7 +220,7 @@ exports[`FormControl > Snapshot > With multiple field 1`] = `
220220
<input
221221
autocomplete="off"
222222
class="c6"
223-
id="field-1"
223+
id="field-:r3:"
224224
size="36"
225225
value=""
226226
/>
@@ -237,8 +237,8 @@ exports[`FormControl > Snapshot > With multiple field 1`] = `
237237
class="c2 c3"
238238
color="txt-black-darkest"
239239
data-testid="bezier-react-form-label"
240-
for="field-2"
241-
id="field-2-label"
240+
for="field-:r4:"
241+
id="field-:r4:-label"
242242
>
243243
Second Inner Label
244244
</label>
@@ -252,7 +252,7 @@ exports[`FormControl > Snapshot > With multiple field 1`] = `
252252
aria-invalid="true"
253253
autocomplete="off"
254254
class="c6"
255-
id="field-2"
255+
id="field-:r4:"
256256
size="36"
257257
value=""
258258
/>
@@ -532,8 +532,8 @@ exports[`FormControl > Snapshot > With multiple field and left label position 1`
532532
class="c7 c4"
533533
color="txt-black-darkest"
534534
data-testid="bezier-react-form-label"
535-
for="field-3"
536-
id="field-3-label"
535+
for="field-:r6:"
536+
id="field-:r6:-label"
537537
>
538538
First Inner Label
539539
</label>
@@ -546,7 +546,7 @@ exports[`FormControl > Snapshot > With multiple field and left label position 1`
546546
<input
547547
autocomplete="off"
548548
class="c9"
549-
id="field-3"
549+
id="field-:r6:"
550550
size="36"
551551
value=""
552552
/>
@@ -563,8 +563,8 @@ exports[`FormControl > Snapshot > With multiple field and left label position 1`
563563
class="c7 c4"
564564
color="txt-black-darkest"
565565
data-testid="bezier-react-form-label"
566-
for="field-4"
567-
id="field-4-label"
566+
for="field-:r7:"
567+
id="field-:r7:-label"
568568
>
569569
Second Inner Label
570570
</label>
@@ -578,7 +578,7 @@ exports[`FormControl > Snapshot > With multiple field and left label position 1`
578578
aria-invalid="true"
579579
autocomplete="off"
580580
class="c9"
581-
id="field-4"
581+
id="field-:r7:"
582582
size="36"
583583
value=""
584584
/>

src/hooks/useId.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ describe('useId >', () => {
66
it('should return unique id', () => {
77
const { result } = renderHook(() => useId())
88

9-
expect(result.current).toBe('1')
9+
expect(result.current).toBe(':r0:')
1010
})
1111

1212
it('should return unique id with 1 added on the next call', () => {
1313
const { result } = renderHook(() => useId())
1414

15-
expect(result.current).toBe('2')
15+
expect(result.current).toBe(':r1:')
1616
})
1717

1818
it('should return id prop when given id prop', () => {
@@ -24,6 +24,6 @@ describe('useId >', () => {
2424
it('should return unique id with a prefix when given prefix prop', () => {
2525
const { result } = renderHook(() => useId(undefined, 'prefix'))
2626

27-
expect(result.current).toBe('prefix-3')
27+
expect(result.current).toBe('prefix-:r3:')
2828
})
2929
})

src/hooks/useId.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
/* @see https://github.com/chakra-ui/chakra-ui/blob/fa474bea3dcbdd4bbf2a26925f938d6e75a50c6d/packages/hooks/src/use-id.ts */
21
/* External dependencies */
3-
import { useState, useEffect, useMemo } from 'react'
2+
import { useId as useIdentifier, useMemo } from 'react'
43
import { compact } from 'lodash-es'
54

6-
const idRef = Object.seal({ current: 1 })
7-
8-
const generateId = () => {
9-
const id = idRef.current
10-
idRef.current += 1
11-
return id
12-
}
13-
145
function useId(idProp?: string, prefix?: string) {
15-
const [id, setId] = useState(idRef.current)
16-
17-
useEffect(() => {
18-
if (idProp) { return }
19-
setId(generateId())
20-
}, [idProp])
6+
const id = useIdentifier()
217

228
return useMemo(() => (
239
idProp || compact([prefix, id]).join('-')

0 commit comments

Comments
 (0)