Skip to content

Commit 563b046

Browse files
committed
Convert class components that don't have to be class components to function components to reduce bundle size
1 parent fc52085 commit 563b046

File tree

5 files changed

+134
-135
lines changed

5 files changed

+134
-135
lines changed

.changeset/brown-terms-hear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-select': patch
3+
---
4+
5+
Convert class components that don't have to be class components to function components to reduce bundle size

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

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22
/** @jsx jsx */
3-
import { Component, type Node } from 'react';
3+
import { type Node } from 'react';
44
import { jsx, ClassNames } from '@emotion/core';
55
import { CrossIcon } from './indicators';
66
import type { CommonProps } from '../types';
@@ -85,91 +85,90 @@ export type MultiValueRemoveProps = {
8585
},
8686
selectProps: any,
8787
};
88-
export class MultiValueRemove extends Component<MultiValueRemoveProps> {
89-
render() {
90-
const { children, innerProps } = this.props;
91-
return <div {...innerProps}>{children || <CrossIcon size={14} />}</div>;
92-
}
88+
export function MultiValueRemove({
89+
children,
90+
innerProps,
91+
}: MultiValueRemoveProps) {
92+
return <div {...innerProps}>{children || <CrossIcon size={14} />}</div>;
9393
}
9494

95-
class MultiValue extends Component<MultiValueProps> {
96-
static defaultProps = {
97-
cropWithEllipsis: true,
98-
};
99-
render() {
100-
const {
101-
children,
102-
className,
103-
components,
104-
cx,
105-
data,
106-
getStyles,
107-
innerProps,
108-
isDisabled,
109-
removeProps,
110-
selectProps,
111-
} = this.props;
95+
const MultiValue = (props: MultiValueProps) => {
96+
const {
97+
children,
98+
className,
99+
components,
100+
cx,
101+
data,
102+
getStyles,
103+
innerProps,
104+
isDisabled,
105+
removeProps,
106+
selectProps,
107+
} = props;
112108

113-
const { Container, Label, Remove } = components;
109+
const { Container, Label, Remove } = components;
114110

115-
return (
116-
<ClassNames>
117-
{({ css, cx: emotionCx }) => (
118-
<Container
111+
return (
112+
<ClassNames>
113+
{({ css, cx: emotionCx }) => (
114+
<Container
115+
data={data}
116+
innerProps={{
117+
...innerProps,
118+
className: emotionCx(
119+
css(getStyles('multiValue', props)),
120+
cx(
121+
{
122+
'multi-value': true,
123+
'multi-value--is-disabled': isDisabled,
124+
},
125+
className
126+
)
127+
),
128+
}}
129+
selectProps={selectProps}
130+
>
131+
<Label
119132
data={data}
120133
innerProps={{
121-
...innerProps,
122134
className: emotionCx(
123-
css(getStyles('multiValue', this.props)),
135+
css(getStyles('multiValueLabel', props)),
124136
cx(
125137
{
126-
'multi-value': true,
127-
'multi-value--is-disabled': isDisabled,
138+
'multi-value__label': true,
128139
},
129140
className
130141
)
131142
),
132143
}}
133144
selectProps={selectProps}
134145
>
135-
<Label
136-
data={data}
137-
innerProps={{
138-
className: emotionCx(
139-
css(getStyles('multiValueLabel', this.props)),
140-
cx(
141-
{
142-
'multi-value__label': true,
143-
},
144-
className
145-
)
146-
),
147-
}}
148-
selectProps={selectProps}
149-
>
150-
{children}
151-
</Label>
152-
<Remove
153-
data={data}
154-
innerProps={{
155-
className: emotionCx(
156-
css(getStyles('multiValueRemove', this.props)),
157-
cx(
158-
{
159-
'multi-value__remove': true,
160-
},
161-
className
162-
)
163-
),
164-
...removeProps,
165-
}}
166-
selectProps={selectProps}
167-
/>
168-
</Container>
169-
)}
170-
</ClassNames>
171-
);
172-
}
173-
}
146+
{children}
147+
</Label>
148+
<Remove
149+
data={data}
150+
innerProps={{
151+
className: emotionCx(
152+
css(getStyles('multiValueRemove', props)),
153+
cx(
154+
{
155+
'multi-value__remove': true,
156+
},
157+
className
158+
)
159+
),
160+
...removeProps,
161+
}}
162+
selectProps={selectProps}
163+
/>
164+
</Container>
165+
)}
166+
</ClassNames>
167+
);
168+
};
169+
170+
MultiValue.defaultProps = {
171+
cropWithEllipsis: true,
172+
};
174173

175174
export default MultiValue;

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

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22
/** @jsx jsx */
3-
import { Component, type Node } from 'react';
3+
import { type Node } from 'react';
44
import { jsx } from '@emotion/core';
55
import type { CommonProps, KeyboardEventHandler } from '../types';
66

@@ -79,34 +79,25 @@ export const valueContainerCSS = ({
7979
position: 'relative',
8080
overflow: 'hidden',
8181
});
82-
export class ValueContainer extends Component<ValueContainerProps> {
83-
render() {
84-
const {
85-
children,
86-
className,
87-
cx,
88-
isMulti,
89-
getStyles,
90-
hasValue,
91-
} = this.props;
82+
export const ValueContainer = (props: ValueContainerProps) => {
83+
const { children, className, cx, isMulti, getStyles, hasValue } = props;
9284

93-
return (
94-
<div
95-
css={getStyles('valueContainer', this.props)}
96-
className={cx(
97-
{
98-
'value-container': true,
99-
'value-container--is-multi': isMulti,
100-
'value-container--has-value': hasValue,
101-
},
102-
className
103-
)}
104-
>
105-
{children}
106-
</div>
107-
);
108-
}
109-
}
85+
return (
86+
<div
87+
css={getStyles('valueContainer', props)}
88+
className={cx(
89+
{
90+
'value-container': true,
91+
'value-container--is-multi': isMulti,
92+
'value-container--has-value': hasValue,
93+
},
94+
className
95+
)}
96+
>
97+
{children}
98+
</div>
99+
);
100+
};
110101

111102
// ==============================
112103
// Indicator Container
Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
11
// @flow
22
/** @jsx jsx */
3-
import { Component } from 'react';
43
import { jsx } from '@emotion/core';
54

6-
export default class DummyInput extends Component<any> {
7-
render () {
8-
const { in: inProp, out, onExited, appear, enter, exit, innerRef, emotion, ...props } = this.props;
9-
return(
10-
<input
11-
ref={innerRef}
12-
{...props}
13-
css={{
14-
label: 'dummyInput',
15-
// get rid of any default styles
16-
background: 0,
17-
border: 0,
18-
fontSize: 'inherit',
19-
outline: 0,
20-
padding: 0,
21-
// important! without `width` browsers won't allow focus
22-
width: 1,
5+
export default function DummyInput({
6+
in: inProp,
7+
out,
8+
onExited,
9+
appear,
10+
enter,
11+
exit,
12+
innerRef,
13+
emotion,
14+
...props
15+
}: any) {
16+
return (
17+
<input
18+
ref={innerRef}
19+
{...props}
20+
css={{
21+
label: 'dummyInput',
22+
// get rid of any default styles
23+
background: 0,
24+
border: 0,
25+
fontSize: 'inherit',
26+
outline: 0,
27+
padding: 0,
28+
// important! without `width` browsers won't allow focus
29+
width: 1,
2330

24-
// remove cursor on desktop
25-
color: 'transparent',
31+
// remove cursor on desktop
32+
color: 'transparent',
2633

27-
// remove cursor on mobile whilst maintaining "scroll into view" behaviour
28-
left: -100,
29-
opacity: 0,
30-
position: 'relative',
31-
transform: 'scale(0)',
32-
}}
33-
/>
34-
);
35-
}
34+
// remove cursor on mobile whilst maintaining "scroll into view" behaviour
35+
left: -100,
36+
opacity: 0,
37+
position: 'relative',
38+
transform: 'scale(0)',
39+
}}
40+
/>
41+
);
3642
}

packages/react-select/src/internal/ScrollCaptor.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class ScrollCaptor extends Component<CaptorProps> {
4040
}
4141
}
4242
stopListening(el: HTMLElement) {
43-
4443
// all the if statements are to appease Flow 😢
4544
if (typeof el.removeEventListener === 'function') {
4645
el.removeEventListener('wheel', this.onWheel, false);
@@ -134,10 +133,9 @@ type SwitchProps = CaptorProps & {
134133
isEnabled: boolean,
135134
};
136135

137-
export default class ScrollCaptorSwitch extends Component<SwitchProps> {
138-
static defaultProps = { isEnabled: true };
139-
render() {
140-
const { isEnabled, ...props } = this.props;
141-
return isEnabled ? <ScrollCaptor {...props} /> : this.props.children;
142-
}
136+
export default function ScrollCaptorSwitch({
137+
isEnabled = true,
138+
...props
139+
}: SwitchProps) {
140+
return isEnabled ? <ScrollCaptor {...props} /> : props.children;
143141
}

0 commit comments

Comments
 (0)