Skip to content

Commit d3c3db1

Browse files
committed
Remove requiredMessage prop
1 parent e940c41 commit d3c3db1

File tree

2 files changed

+26
-70
lines changed

2 files changed

+26
-70
lines changed

packages/react-select/src/Select.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ export interface Props<
262262
value: PropsValue<Option>;
263263
/** Sets the form attribute on the input */
264264
form?: string;
265-
/** Text to display if the component fails the `required` validation */
266-
requiredMessage: () => string;
267265
/** Marks the value-holding input as required for form validation */
268266
required?: boolean;
269267
}
@@ -1562,7 +1560,7 @@ export default class Select<
15621560
tabIndex,
15631561
form,
15641562
menuIsOpen,
1565-
required
1563+
required,
15661564
} = this.props;
15671565
const { Input } = this.getComponents();
15681566
const { inputIsHidden, ariaSelection } = this.state;
@@ -1976,20 +1974,13 @@ export default class Select<
19761974
);
19771975
}
19781976
renderFormField() {
1979-
const { delimiter, isDisabled, isMulti, name, required, requiredMessage } =
1980-
this.props;
1977+
const { delimiter, isDisabled, isMulti, name, required } = this.props;
19811978
const { selectValue } = this.state;
19821979

19831980
if (!name || isDisabled) return;
19841981

19851982
if (required && !this.hasValue()) {
1986-
return (
1987-
<RequiredInput
1988-
name={name}
1989-
onFocus={this.onValueInputFocus}
1990-
requiredMessage={requiredMessage}
1991-
/>
1992-
);
1983+
return <RequiredInput name={name} onFocus={this.onValueInputFocus} />;
19931984
}
19941985

19951986
if (isMulti) {
Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,30 @@
11
/** @jsx jsx */
2-
import {
3-
FocusEventHandler,
4-
FunctionComponent,
5-
useCallback,
6-
useEffect,
7-
useRef,
8-
} from 'react';
2+
import { FocusEventHandler, FunctionComponent } from 'react';
93
import { jsx } from '@emotion/react';
104

11-
interface Props {
5+
const RequiredInput: FunctionComponent<{
126
readonly name: string;
13-
readonly requiredMessage: () => string;
147
readonly onFocus: FocusEventHandler<HTMLInputElement>;
15-
}
16-
17-
const RequiredInput: FunctionComponent<Props> = ({
18-
name,
19-
requiredMessage,
20-
onFocus,
21-
}) => {
22-
const inputRef = useRef<HTMLInputElement>(null);
23-
24-
useEffect(
25-
() => () => {
26-
// Reset validity on unmount
27-
inputRef.current?.setCustomValidity('');
28-
},
29-
[]
30-
);
31-
32-
const onInvalid = useCallback(() => {
33-
inputRef.current?.setCustomValidity(requiredMessage());
34-
}, [requiredMessage]);
35-
36-
useEffect(() => {
37-
onInvalid();
38-
}, [onInvalid]);
39-
40-
return (
41-
<input
42-
required
43-
name={name}
44-
tabIndex={-1}
45-
ref={inputRef}
46-
onFocus={onFocus}
47-
onInvalid={onInvalid}
48-
css={{
49-
label: 'requiredInput',
50-
opacity: 0,
51-
pointerEvents: 'none',
52-
position: 'absolute',
53-
bottom: 0,
54-
left: 0,
55-
right: 0,
56-
width: '100%',
57-
}}
58-
// Prevent `Switching from uncontrolled to controlled` error
59-
value=""
60-
onChange={() => {}}
61-
/>
62-
);
63-
};
8+
}> = ({ name, onFocus }) => (
9+
<input
10+
required
11+
name={name}
12+
tabIndex={-1}
13+
onFocus={onFocus}
14+
css={{
15+
label: 'requiredInput',
16+
opacity: 0,
17+
pointerEvents: 'none',
18+
position: 'absolute',
19+
bottom: 0,
20+
left: 0,
21+
right: 0,
22+
width: '100%',
23+
}}
24+
// Prevent `Switching from uncontrolled to controlled` error
25+
value=""
26+
onChange={() => {}}
27+
/>
28+
);
6429

6530
export default RequiredInput;

0 commit comments

Comments
 (0)