Skip to content

Commit 593d01b

Browse files
SaraVieirasiddharthkp
authored andcommitted
Fix textarea id changing on re render (#3309)
* fix textarea id rerendering * add autoid
1 parent 2548a06 commit 593d01b

File tree

6 files changed

+17
-12
lines changed

6 files changed

+17
-12
lines changed

packages/components/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"dependencies": {
3030
"@codesandbox/common": "^1.0.8",
3131
"@codesandbox/template-icons": "^1.1.0",
32+
"@reach/auto-id": "^0.7.1",
3233
"@reach/visually-hidden": "^0.7.0",
3334
"@styled-system/css": "^5.1.4",
3435
"add": "^2.0.6",

packages/components/src/components/Input/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import styled from 'styled-components';
33
import css from '@styled-system/css';
44
import VisuallyHidden from '@reach/visually-hidden';
5-
import { uniqueId } from 'lodash-es';
5+
import { useId } from '@reach/auto-id';
66
import { Text } from '../Text';
77

88
const placeholderStyles = {
@@ -35,8 +35,7 @@ export const Input: React.FC<IInputProps> = ({
3535
label,
3636
...props
3737
}) => {
38-
const id = props.id || uniqueId('form_');
39-
38+
const id = useId(props.id);
4039
return (
4140
<>
4241
{props.placeholder && !label ? (

packages/components/src/components/SearchInput/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import styled from 'styled-components';
33
import css from '@styled-system/css';
44
import VisuallyHidden from '@reach/visually-hidden';
5-
import { uniqueId } from 'lodash-es';
5+
import { useId } from '@reach/auto-id';
66
import { Text } from '../Text';
77
import { InputComponent } from '../Input';
88
import { Element } from '../Element';
@@ -51,7 +51,7 @@ interface ISearchProps extends React.InputHTMLAttributes<HTMLInputElement> {
5151
}
5252

5353
export const SearchInput: React.FC<ISearchProps> = ({ label, ...props }) => {
54-
const id = props.id || uniqueId('form_');
54+
const id = useId(props.id);
5555

5656
return (
5757
<>

packages/components/src/components/Select/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useCallback } from 'react';
22
import styled from 'styled-components';
33
import css from '@styled-system/css';
44
import VisuallyHidden from '@reach/visually-hidden';
5-
import { uniqueId } from 'lodash-es';
5+
import { useId } from '@reach/auto-id';
66
import { Text } from '../Text';
77
import { Element } from '../Element';
88

@@ -74,7 +74,7 @@ export const Select: React.FC<ISelectProps> = ({
7474
[icon]
7575
);
7676

77-
const id = props.id || uniqueId('form_');
77+
const id = useId(props.id);
7878

7979
return (
8080
<>

packages/components/src/components/Textarea/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import styled, {
55
} from 'styled-components';
66
import css from '@styled-system/css';
77
import VisuallyHidden from '@reach/visually-hidden';
8-
import { uniqueId } from 'lodash-es';
8+
import { useId } from '@reach/auto-id';
99
import { Text } from '../Text';
1010
import { Stack } from '../Stack';
1111
import { InputComponent } from '../Input';
@@ -53,9 +53,9 @@ export const Textarea: React.FC<ITextareaProps> = ({
5353
autosize,
5454
...props
5555
}) => {
56-
const id = props.id || uniqueId('form_');
5756
const [wordCount, setWordCount] = useState(0);
5857
const [value, setValue] = useState('');
58+
const id = useId(props.id);
5959

6060
const Wrapper = useCallback(
6161
({ children }) =>
@@ -98,15 +98,15 @@ export const Textarea: React.FC<ITextareaProps> = ({
9898
<>
9999
{props.placeholder && !label ? (
100100
<VisuallyHidden>
101-
<label htmlFor={id}>{props.placeholder}</label>
101+
<label htmlFor={props.id || id}>{props.placeholder}</label>
102102
</VisuallyHidden>
103103
) : null}
104104
{label ? (
105105
<Text
106106
as="label"
107107
size={2}
108108
marginBottom={2}
109-
htmlFor={id}
109+
htmlFor={props.id || id}
110110
style={{ display: 'block' }}
111111
>
112112
{label}
@@ -117,7 +117,7 @@ export const Textarea: React.FC<ITextareaProps> = ({
117117
value={value}
118118
onChange={update}
119119
onKeyPress={keyPress}
120-
id={id}
120+
id={props.id || id}
121121
{...props}
122122
/>
123123
{maxLength ? (

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3850,6 +3850,11 @@
38503850
hey-listen "^1.0.8"
38513851
style-value-types "^3.1.6"
38523852

3853+
"@reach/auto-id@^0.7.1":
3854+
version "0.7.1"
3855+
resolved "https://registry.yarnpkg.com/@reach/auto-id/-/auto-id-0.7.1.tgz#68ff4345b12c5ead6d0374a574480090b18a1b31"
3856+
integrity sha512-7bE/tCFc6KBMKkSfquJFpeZic+OHe+RAE2OHCN66felnleVwm6hr128d2UAp5vb1H12F6fm1kCUV9s3xNqSwMQ==
3857+
38533858
"@reach/router@^1.2.1":
38543859
version "1.2.1"
38553860
resolved "https://registry.yarnpkg.com/@reach/router/-/router-1.2.1.tgz#34ae3541a5ac44fa7796e5506a5d7274a162be4e"

0 commit comments

Comments
 (0)