Skip to content

Commit effbecf

Browse files
committed
Remove requiredMessage prop
1 parent 5c61798 commit effbecf

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
}
@@ -1571,7 +1569,7 @@ export default class Select<
15711569
tabIndex,
15721570
form,
15731571
menuIsOpen,
1574-
required
1572+
required,
15751573
} = this.props;
15761574
const { Input } = this.getComponents();
15771575
const { inputIsHidden, ariaSelection } = this.state;
@@ -1985,20 +1983,13 @@ export default class Select<
19851983
);
19861984
}
19871985
renderFormField() {
1988-
const { delimiter, isDisabled, isMulti, name, required, requiredMessage } =
1989-
this.props;
1986+
const { delimiter, isDisabled, isMulti, name, required } = this.props;
19901987
const { selectValue } = this.state;
19911988

19921989
if (!name || isDisabled) return;
19931990

19941991
if (required && !this.hasValue()) {
1995-
return (
1996-
<RequiredInput
1997-
name={name}
1998-
onFocus={this.onValueInputFocus}
1999-
requiredMessage={requiredMessage}
2000-
/>
2001-
);
1992+
return <RequiredInput name={name} onFocus={this.onValueInputFocus} />;
20021993
}
20031994

20041995
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)