Skip to content

Commit 7cdb8a6

Browse files
committed
Pass and sanitize CommonProps passed to Group and Input
1 parent 45f6f29 commit 7cdb8a6

File tree

6 files changed

+66
-38
lines changed

6 files changed

+66
-38
lines changed

.changeset/shy-onions-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-select': minor
3+
---
4+
5+
Pass and sanitize CommonProps passed to Group and Input components

packages/react-select/src/Select.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -771,25 +771,25 @@ export default class Select extends Component<Props, State> {
771771
cx,
772772
getStyles,
773773
getValue,
774-
setValue,
775774
selectOption,
775+
setValue,
776776
props,
777777
} = this;
778778
const { isMulti, isRtl, options } = props;
779779
const hasValue = this.hasValue();
780780

781781
return {
782-
cx,
783782
clearValue,
783+
cx,
784784
getStyles,
785785
getValue,
786786
hasValue,
787787
isMulti,
788788
isRtl,
789789
options,
790790
selectOption,
791-
setValue,
792791
selectProps: props,
792+
setValue,
793793
theme: this.getTheme(),
794794
};
795795
}
@@ -1446,6 +1446,7 @@ export default class Select extends Component<Props, State> {
14461446
} = this.props;
14471447
const { Input } = this.components;
14481448
const { inputIsHidden } = this.state;
1449+
const { commonProps } = this;
14491450

14501451
const id = inputId || this.getElementId('input');
14511452

@@ -1475,27 +1476,22 @@ export default class Select extends Component<Props, State> {
14751476
);
14761477
}
14771478

1478-
const { cx, theme, selectProps } = this.commonProps;
1479-
14801479
return (
14811480
<Input
1481+
{...commonProps}
14821482
autoCapitalize="none"
14831483
autoComplete="off"
14841484
autoCorrect="off"
1485-
cx={cx}
1486-
getStyles={this.getStyles}
14871485
id={id}
14881486
innerRef={this.getInputRef}
14891487
isDisabled={isDisabled}
14901488
isHidden={inputIsHidden}
14911489
onBlur={this.onInputBlur}
14921490
onChange={this.handleInputChange}
14931491
onFocus={this.onInputFocus}
1494-
selectProps={selectProps}
14951492
spellCheck="false"
14961493
tabIndex={tabIndex}
14971494
form={form}
1498-
theme={theme}
14991495
type="text"
15001496
value={inputValue}
15011497
{...ariaAttributes}

packages/react-select/src/components/Group.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/** @jsx jsx */
33
import { type Node, type ComponentType } from 'react';
44
import { jsx } from '@emotion/core';
5+
import { cleanCommonProps } from '../utils';
56

67
import type { CommonProps } from '../types';
78

@@ -14,6 +15,8 @@ type ComponentProps = {
1415
headingProps: any,
1516
/** Label to be displayed in the heading component. */
1617
label: Node,
18+
/* The data of the group. */
19+
data: any,
1720
};
1821
export type GroupProps = CommonProps & ComponentProps;
1922

@@ -67,12 +70,13 @@ export const groupHeadingCSS = ({ theme: { spacing } }: GroupProps) => ({
6770
});
6871

6972
export const GroupHeading = (props: any) => {
70-
const { className, cx, getStyles, theme, selectProps, ...cleanProps } = props;
73+
const { getStyles, cx, className } = props;
74+
const { data, ...innerProps } = cleanCommonProps(props);
7175
return (
7276
<div
73-
css={getStyles('groupHeading', { theme, ...cleanProps })}
77+
css={getStyles('groupHeading', props)}
7478
className={cx({ 'group-heading': true }, className)}
75-
{...cleanProps}
79+
{...innerProps}
7680
/>
7781
);
7882
};

packages/react-select/src/components/Input.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import { type ElementRef } from 'react';
44
import { jsx } from '@emotion/core';
55
import AutosizeInput from 'react-input-autosize';
66

7-
import type { PropsWithStyles, ClassNamesState } from '../types';
7+
import type { CommonProps } from '../types';
8+
import { cleanCommonProps } from '../utils';
89

9-
export type InputProps = PropsWithStyles & {
10-
cx: (?ClassNamesState, ?string) => string | void,
10+
export type InputProps = CommonProps & {
1111
/** Reference to the internal element */
1212
innerRef: (ElementRef<*>) => void,
1313
/** Set whether the input should be visible. Does not affect input size. */
1414
isHidden: boolean,
1515
/** Whether the input is disabled */
1616
isDisabled?: boolean,
17-
className?: string,
1817
/** The ID of the form that the input belongs to */
1918
form?: string,
2019
};
@@ -40,26 +39,23 @@ const inputStyle = isHidden => ({
4039
color: 'inherit',
4140
});
4241

43-
const Input = ({
44-
className,
45-
cx,
46-
getStyles,
47-
innerRef,
48-
isHidden,
49-
isDisabled,
50-
theme,
51-
selectProps,
52-
...props
53-
}: InputProps) => (
54-
<div css={getStyles('input', { theme, ...props })}>
55-
<AutosizeInput
56-
className={cx({ input: true }, className)}
57-
inputRef={innerRef}
58-
inputStyle={inputStyle(isHidden)}
59-
disabled={isDisabled}
60-
{...props}
61-
/>
62-
</div>
63-
);
42+
const Input = (props: InputProps) => {
43+
const { className, cx, getStyles } = props;
44+
const { innerRef, isDisabled, isHidden, ...innerProps } = cleanCommonProps(
45+
props
46+
);
47+
48+
return (
49+
<div css={getStyles('input', props)}>
50+
<AutosizeInput
51+
className={cx({ input: true }, className)}
52+
inputRef={innerRef}
53+
inputStyle={inputStyle(isHidden)}
54+
disabled={isDisabled}
55+
{...innerProps}
56+
/>
57+
</div>
58+
);
59+
};
6460

6561
export default Input;

packages/react-select/src/types.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ export type CommonProps = {
6464
See the `styles` object for the properties available.
6565
*/
6666
getStyles: (string, any) => {},
67-
theme: Theme,
6867
getValue: () => ValueType,
6968
hasValue: boolean,
7069
isMulti: boolean,
70+
isRtl: boolean,
7171
options: OptionsType,
7272
selectOption: OptionType => void,
7373
selectProps: any,
7474
setValue: (ValueType, ActionTypes) => void,
75+
theme: Theme,
7576
};
7677

7778
export type ActionTypes =

packages/react-select/src/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { type ElementRef } from 'react';
44
import type {
55
ClassNamesState,
6+
CommonProps,
67
InputActionMeta,
78
OptionsType,
89
ValueType,
@@ -67,6 +68,31 @@ export const cleanValue = (value: ValueType): OptionsType => {
6768
return [];
6869
};
6970

71+
// ==============================
72+
// Clean Common Props
73+
// ==============================
74+
75+
export const cleanCommonProps = (props: CommonProps): any => {
76+
//className
77+
const {
78+
className, // not listed in commonProps documentation, needs to be removed to allow Emotion to generate classNames
79+
clearValue,
80+
cx,
81+
getStyles,
82+
getValue,
83+
hasValue,
84+
isMulti,
85+
isRtl,
86+
options, // not listed in commonProps documentation
87+
selectOption,
88+
selectProps,
89+
setValue,
90+
theme, // not listed in commonProps documentation
91+
...innerProps
92+
} = props;
93+
return { ...innerProps };
94+
};
95+
7096
// ==============================
7197
// Handle Input Change
7298
// ==============================

0 commit comments

Comments
 (0)