File tree Expand file tree Collapse file tree 3 files changed +17
-31
lines changed
components/Forms/FormControl/__snapshots__ Expand file tree Collapse file tree 3 files changed +17
-31
lines changed Original file line number Diff line number Diff line change @@ -206,8 +206,8 @@ exports[`FormControl > Snapshot > With multiple field 1`] = `
206
206
class = " c2 c3"
207
207
color = " txt-black-darkest"
208
208
data-testid = " bezier-react-form-label"
209
- for = " field-1 "
210
- id = " field-1 -label"
209
+ for = " field-:r3: "
210
+ id = " field-:r3: -label"
211
211
>
212
212
First Inner Label
213
213
</label >
@@ -220,7 +220,7 @@ exports[`FormControl > Snapshot > With multiple field 1`] = `
220
220
<input
221
221
autocomplete = " off"
222
222
class = " c6"
223
- id = " field-1 "
223
+ id = " field-:r3: "
224
224
size = " 36"
225
225
value = " "
226
226
/>
@@ -237,8 +237,8 @@ exports[`FormControl > Snapshot > With multiple field 1`] = `
237
237
class = " c2 c3"
238
238
color = " txt-black-darkest"
239
239
data-testid = " bezier-react-form-label"
240
- for = " field-2 "
241
- id = " field-2 -label"
240
+ for = " field-:r4: "
241
+ id = " field-:r4: -label"
242
242
>
243
243
Second Inner Label
244
244
</label >
@@ -252,7 +252,7 @@ exports[`FormControl > Snapshot > With multiple field 1`] = `
252
252
aria-invalid = " true"
253
253
autocomplete = " off"
254
254
class = " c6"
255
- id = " field-2 "
255
+ id = " field-:r4: "
256
256
size = " 36"
257
257
value = " "
258
258
/>
@@ -532,8 +532,8 @@ exports[`FormControl > Snapshot > With multiple field and left label position 1`
532
532
class = " c7 c4"
533
533
color = " txt-black-darkest"
534
534
data-testid = " bezier-react-form-label"
535
- for = " field-3 "
536
- id = " field-3 -label"
535
+ for = " field-:r6: "
536
+ id = " field-:r6: -label"
537
537
>
538
538
First Inner Label
539
539
</label >
@@ -546,7 +546,7 @@ exports[`FormControl > Snapshot > With multiple field and left label position 1`
546
546
<input
547
547
autocomplete = " off"
548
548
class = " c9"
549
- id = " field-3 "
549
+ id = " field-:r6: "
550
550
size = " 36"
551
551
value = " "
552
552
/>
@@ -563,8 +563,8 @@ exports[`FormControl > Snapshot > With multiple field and left label position 1`
563
563
class = " c7 c4"
564
564
color = " txt-black-darkest"
565
565
data-testid = " bezier-react-form-label"
566
- for = " field-4 "
567
- id = " field-4 -label"
566
+ for = " field-:r7: "
567
+ id = " field-:r7: -label"
568
568
>
569
569
Second Inner Label
570
570
</label >
@@ -578,7 +578,7 @@ exports[`FormControl > Snapshot > With multiple field and left label position 1`
578
578
aria-invalid = " true"
579
579
autocomplete = " off"
580
580
class = " c9"
581
- id = " field-4 "
581
+ id = " field-:r7: "
582
582
size = " 36"
583
583
value = " "
584
584
/>
Original file line number Diff line number Diff line change @@ -6,13 +6,13 @@ describe('useId >', () => {
6
6
it ( 'should return unique id' , ( ) => {
7
7
const { result } = renderHook ( ( ) => useId ( ) )
8
8
9
- expect ( result . current ) . toBe ( '1 ' )
9
+ expect ( result . current ) . toBe ( ':r0: ' )
10
10
} )
11
11
12
12
it ( 'should return unique id with 1 added on the next call' , ( ) => {
13
13
const { result } = renderHook ( ( ) => useId ( ) )
14
14
15
- expect ( result . current ) . toBe ( '2 ' )
15
+ expect ( result . current ) . toBe ( ':r1: ' )
16
16
} )
17
17
18
18
it ( 'should return id prop when given id prop' , ( ) => {
@@ -24,6 +24,6 @@ describe('useId >', () => {
24
24
it ( 'should return unique id with a prefix when given prefix prop' , ( ) => {
25
25
const { result } = renderHook ( ( ) => useId ( undefined , 'prefix' ) )
26
26
27
- expect ( result . current ) . toBe ( 'prefix-3 ' )
27
+ expect ( result . current ) . toBe ( 'prefix-:r3: ' )
28
28
} )
29
29
} )
Original file line number Diff line number Diff line change 1
- /* @see https://github.com/chakra-ui/chakra-ui/blob/fa474bea3dcbdd4bbf2a26925f938d6e75a50c6d/packages/hooks/src/use-id.ts */
2
1
/* External dependencies */
3
- import { useState , useEffect , useMemo } from 'react'
2
+ import { useId as useIdentifier , useMemo } from 'react'
4
3
import { compact } from 'lodash-es'
5
4
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
-
14
5
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 ( )
21
7
22
8
return useMemo ( ( ) => (
23
9
idProp || compact ( [ prefix , id ] ) . join ( '-' )
You can’t perform that action at this time.
0 commit comments