Skip to content

Commit f652637

Browse files
authored
avoid showing placeholder warning every render (#8682)
1 parent 1f35627 commit f652637

File tree

6 files changed

+47
-22
lines changed

6 files changed

+47
-22
lines changed

packages/@react-spectrum/autocomplete/src/SearchAutocomplete.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ function SearchAutocomplete<T extends object>(props: SpectrumSearchAutocompleteP
5050
props = useProviderProps(props);
5151
props = useFormProps(props);
5252

53-
if (props.placeholder && process.env.NODE_ENV !== 'production') {
54-
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead.');
55-
}
53+
let hasWarned = useRef(false);
54+
useEffect(() => {
55+
if (props.placeholder && !hasWarned.current && process.env.NODE_ENV !== 'production') {
56+
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead.');
57+
hasWarned.current = true;
58+
}
59+
}, [props.placeholder]);
5660

5761
let isMobile = useIsMobileDevice();
5862
if (isMobile) {

packages/@react-spectrum/color/src/ColorField.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import {classNames} from '@react-spectrum/utils';
1414
import {ColorChannel, SpectrumColorFieldProps} from '@react-types/color';
1515
import {ColorFieldContext, useContextProps} from 'react-aria-components';
16-
import React, {Ref, useRef} from 'react';
16+
import React, {Ref, useEffect, useRef} from 'react';
1717
import styles from './colorfield.css';
1818
import {TextFieldBase} from '@react-spectrum/textfield';
1919
import {TextFieldRef} from '@react-types/textfield';
@@ -30,9 +30,14 @@ export const ColorField = React.forwardRef(function ColorField(props: SpectrumCo
3030
props = useProviderProps(props);
3131
props = useFormProps(props);
3232
[props] = useContextProps(props, null, ColorFieldContext);
33-
if (props.placeholder && process.env.NODE_ENV !== 'production') {
34-
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/ColorField.html#help-text');
35-
}
33+
34+
let hasWarned = useRef(false);
35+
useEffect(() => {
36+
if (props.placeholder && !hasWarned.current && process.env.NODE_ENV !== 'production') {
37+
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/ColorField.html#help-text');
38+
hasWarned.current = true;
39+
}
40+
}, [props.placeholder]);
3641

3742
if (props.channel) {
3843
return <ColorChannelField {...props} channel={props.channel} forwardedRef={ref} />;

packages/@react-spectrum/combobox/src/ComboBox.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ export const ComboBox = React.forwardRef(function ComboBox<T extends object>(pro
6060
props = useProviderProps(props);
6161
props = useFormProps(props);
6262

63-
if (props.placeholder && process.env.NODE_ENV !== 'production') {
64-
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/ComboBox.html#help-text');
65-
}
63+
let hasWarned = useRef(false);
64+
useEffect(() => {
65+
if (props.placeholder && !hasWarned.current && process.env.NODE_ENV !== 'production') {
66+
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/ComboBox.html#help-text');
67+
hasWarned.current = true;
68+
}
69+
}, [props.placeholder]);
6670

6771
let isMobile = useIsMobileDevice();
6872
if (isMobile) {

packages/@react-spectrum/searchfield/src/SearchField.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import {classNames, useSlotProps} from '@react-spectrum/utils';
1414
import {ClearButton} from '@react-spectrum/button';
1515
import Magnifier from '@spectrum-icons/ui/Magnifier';
16-
import React, {forwardRef, ReactElement, Ref, useRef} from 'react';
16+
import React, {forwardRef, ReactElement, Ref, useEffect, useRef} from 'react';
1717
import {SpectrumSearchFieldProps} from '@react-types/searchfield';
1818
import styles from '@adobe/spectrum-css-temp/components/search/vars.css';
1919
import {TextFieldBase} from '@react-spectrum/textfield';
@@ -42,9 +42,13 @@ export const SearchField = forwardRef(function SearchField(props: SpectrumSearch
4242
...otherProps
4343
} = props;
4444

45-
if (placeholder && process.env.NODE_ENV !== 'production') {
46-
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/SearchField.html#help-text');
47-
}
45+
let hasWarned = useRef(false);
46+
useEffect(() => {
47+
if (placeholder && !hasWarned.current && process.env.NODE_ENV !== 'production') {
48+
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/SearchField.html#help-text');
49+
hasWarned.current = true;
50+
}
51+
}, [placeholder]);
4852

4953
let state = useSearchFieldState(props);
5054
let inputRef = useRef<HTMLInputElement>(null);

packages/@react-spectrum/textfield/src/TextArea.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
import {chain, useLayoutEffect} from '@react-aria/utils';
14-
import React, {Ref, useCallback, useRef} from 'react';
14+
import React, {Ref, useCallback, useEffect, useRef} from 'react';
1515
import {SpectrumTextAreaProps, SpectrumTextFieldBaseProps, TextFieldRef} from '@react-types/textfield';
1616
import {TextFieldBase} from './TextFieldBase';
1717
import {useControlledState} from '@react-stately/utils';
@@ -69,9 +69,13 @@ export const TextArea = React.forwardRef(function TextArea(props: SpectrumTextAr
6969
}
7070
}, [onHeightChange, inputValue, inputRef]);
7171

72-
if (props.placeholder && process.env.NODE_ENV !== 'production') {
73-
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/TextArea.html#help-text');
74-
}
72+
let hasWarned = useRef(false);
73+
useEffect(() => {
74+
if (props.placeholder && !hasWarned.current && process.env.NODE_ENV !== 'production') {
75+
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/TextArea.html#help-text');
76+
hasWarned.current = true;
77+
}
78+
}, [props.placeholder]);
7579

7680
let result = useTextField({
7781
...props,

packages/@react-spectrum/textfield/src/TextField.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import React, {forwardRef, Ref, useRef} from 'react';
13+
import React, {forwardRef, Ref, useEffect, useRef} from 'react';
1414
import {SpectrumTextFieldProps, TextFieldRef} from '@react-types/textfield';
1515
import {TextFieldBase} from './TextFieldBase';
1616
import {useFormProps} from '@react-spectrum/form';
@@ -29,9 +29,13 @@ export const TextField = forwardRef(function TextField(props: SpectrumTextFieldP
2929
let inputRef = useRef<HTMLInputElement>(null);
3030
let result = useTextField(props, inputRef);
3131

32-
if (props.placeholder && process.env.NODE_ENV !== 'production') {
33-
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/TextField.html#help-text');
34-
}
32+
let hasWarned = useRef(false);
33+
useEffect(() => {
34+
if (props.placeholder && !hasWarned.current && process.env.NODE_ENV !== 'production') {
35+
console.warn('Placeholders are deprecated due to accessibility issues. Please use help text instead. See the docs for details: https://react-spectrum.adobe.com/react-spectrum/TextField.html#help-text');
36+
hasWarned.current = true;
37+
}
38+
}, [props.placeholder]);
3539

3640
return (
3741
<TextFieldBase

0 commit comments

Comments
 (0)