Skip to content

Commit b6cb576

Browse files
author
刘欢
committed
feat: add inputProps prop to pass props to internal input
- Add inputProps to BaseSelectProps, SelectInputProps and InputProps - Merge user inputProps with default props in Content/index.tsx - Add test case for inputProps - Update README with API documentation Close ant-design/ant-design#38012
1 parent 740b93b commit b6cb576

6 files changed

Lines changed: 39 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export default () => (
126126
| optionRender | Custom rendering options | (oriOption: FlattenOptionData\<BaseOptionType\> , info: { index: number }) => React.ReactNode | - |
127127
| labelRender | Custom rendering label | (props: LabelInValueType) => React.ReactNode | - |
128128
| maxCount | The max number of items can be selected | number | - |
129+
| inputProps | props passed to the internal input element | React.InputHTMLAttributes<HTMLInputElement> | - |
129130

130131
### Methods
131132

src/BaseSelect/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ export interface BaseSelectPrivateProps {
130130
export type BaseSelectPropsWithoutPrivate = Omit<BaseSelectProps, keyof BaseSelectPrivateProps>;
131131

132132
export interface BaseSelectProps
133-
extends
134-
BaseSelectPrivateProps,
133+
extends BaseSelectPrivateProps,
135134
React.AriaAttributes,
136135
Pick<React.HTMLAttributes<HTMLElement>, 'role'> {
137136
// Style
@@ -229,6 +228,10 @@ export interface BaseSelectProps
229228

230229
// >>> Components
231230
components?: ComponentsConfig;
231+
232+
// >>> Input
233+
/** Props passed to the internal input element */
234+
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
232235
}
233236

234237
export const isMultiple = (mode: Mode) => mode === 'tags' || mode === 'multiple';

src/SelectInput/Content/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ export interface SharedContentProps {
1010
}
1111

1212
const SelectContent = React.forwardRef<HTMLInputElement>(function SelectContent(_, ref) {
13-
const { multiple, onInputKeyDown, tabIndex } = useSelectInputContext();
13+
const {
14+
multiple,
15+
onInputKeyDown,
16+
tabIndex,
17+
inputProps: userInputProps,
18+
} = useSelectInputContext();
1419
const baseProps = useBaseProps();
1520
const { showSearch } = baseProps;
1621

1722
const ariaProps = pickAttrs(baseProps, { aria: true });
1823

1924
const sharedInputProps: SharedContentProps['inputProps'] = {
2025
...ariaProps,
26+
...userInputProps,
2127
onKeyDown: onInputKeyDown,
2228
readOnly: !showSearch,
2329
tabIndex,

src/SelectInput/Input.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export interface InputProps {
2121
syncWidth?: boolean;
2222
/** autoComplete for input */
2323
autoComplete?: string;
24+
/** Props passed to the internal input element */
25+
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
2426
}
2527

2628
const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
@@ -33,6 +35,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
3335
value,
3436
className,
3537
autoComplete,
38+
inputProps,
3639
...restProps
3740
} = props;
3841
const {
@@ -164,6 +167,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
164167
id,
165168
type: mode === 'combobox' ? 'text' : 'search',
166169
...restProps,
170+
...inputProps,
167171
ref: inputRef as React.Ref<HTMLInputElement>,
168172
style: {
169173
...styles?.input,

src/SelectInput/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export interface SelectInputProps extends Omit<React.HTMLAttributes<HTMLDivEleme
4848
focused?: boolean;
4949
components: ComponentsConfig;
5050
children?: React.ReactElement;
51+
/** Props passed to the internal input element */
52+
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
5153
}
5254

5355
const DEFAULT_OMIT_PROPS = [

tests/BaseSelect.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,24 @@ describe('BaseSelect', () => {
190190
expect(container.querySelector('.custom-root-element')).toBeTruthy();
191191
expect(container.querySelector('.rc-select')).toBeFalsy();
192192
});
193+
194+
it('should pass inputProps to internal input', () => {
195+
const { container } = render(
196+
<BaseSelect
197+
prefixCls="rc-select"
198+
showSearch
199+
inputProps={{ spellCheck: false }}
200+
OptionList={OptionList}
201+
displayValues={[]}
202+
emptyOptions
203+
id="test"
204+
onDisplayValuesChange={() => {}}
205+
onSearch={() => {}}
206+
searchValue=""
207+
/>,
208+
);
209+
210+
const input = container.querySelector('input');
211+
expect(input?.getAttribute('spellcheck')).toBe('false');
212+
});
193213
});

0 commit comments

Comments
 (0)