Skip to content

Commit bd453dd

Browse files
committed
convert CreatableSelect to ES6 class
1 parent 2616930 commit bd453dd

1 file changed

Lines changed: 86 additions & 81 deletions

File tree

src/Creatable.js

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,19 @@
11
import React from 'react';
2-
import createClass from 'create-react-class';
32
import PropTypes from 'prop-types';
43
import Select from './Select';
54
import defaultFilterOptions from './utils/defaultFilterOptions';
65
import defaultMenuRenderer from './utils/defaultMenuRenderer';
76

8-
const Creatable = createClass({
9-
displayName: 'CreatableSelect',
7+
class CreatableSelect extends React.Component {
8+
constructor (props, context) {
9+
super(props, context);
1010

11-
propTypes: {
12-
// Child function responsible for creating the inner Select component
13-
// This component can be used to compose HOCs (eg Creatable and Async)
14-
// (props: Object): PropTypes.element
15-
children: PropTypes.func,
16-
17-
// See Select.propTypes.filterOptions
18-
filterOptions: PropTypes.any,
19-
20-
// Searches for any matching option within the set of options.
21-
// This function prevents duplicate options from being created.
22-
// ({ option: Object, options: Array, labelKey: string, valueKey: string }): boolean
23-
isOptionUnique: PropTypes.func,
24-
25-
// Determines if the current input text represents a valid option.
26-
// ({ label: string }): boolean
27-
isValidNewOption: PropTypes.func,
28-
29-
// See Select.propTypes.menuRenderer
30-
menuRenderer: PropTypes.any,
31-
32-
// Factory to create new option.
33-
// ({ label: string, labelKey: string, valueKey: string }): Object
34-
newOptionCreator: PropTypes.func,
35-
36-
// input change handler: function (inputValue) {}
37-
onInputChange: PropTypes.func,
38-
39-
// input keyDown handler: function (event) {}
40-
onInputKeyDown: PropTypes.func,
41-
42-
// new option click handler: function (option) {}
43-
onNewOptionClick: PropTypes.func,
44-
45-
// See Select.propTypes.options
46-
options: PropTypes.array,
47-
48-
// Creates prompt/placeholder option text.
49-
// (filterText: string): string
50-
promptTextCreator: PropTypes.func,
51-
52-
// Decides if a keyDown event (eg its `keyCode`) should result in the creation of a new option.
53-
shouldKeyDownEventCreateNewOption: PropTypes.func,
54-
},
55-
56-
// Default prop methods
57-
statics: {
58-
isOptionUnique,
59-
isValidNewOption,
60-
newOptionCreator,
61-
promptTextCreator,
62-
shouldKeyDownEventCreateNewOption
63-
},
64-
65-
getDefaultProps () {
66-
return {
67-
filterOptions: defaultFilterOptions,
68-
isOptionUnique,
69-
isValidNewOption,
70-
menuRenderer: defaultMenuRenderer,
71-
newOptionCreator,
72-
promptTextCreator,
73-
shouldKeyDownEventCreateNewOption,
74-
};
75-
},
11+
this.filterOptions = this.filterOptions.bind(this);
12+
this.menuRenderer = this.menuRenderer.bind(this);
13+
this.onInputKeyDown = this.onInputKeyDown.bind(this);
14+
this.onInputChange = this.onInputChange.bind(this);
15+
this.onOptionSelect = this.onOptionSelect .bind(this);
16+
}
7617

7718
createNewOption () {
7819
const {
@@ -98,7 +39,7 @@ const Creatable = createClass({
9839
}
9940
}
10041
}
101-
},
42+
}
10243

10344
filterOptions (...params) {
10445
const { filterOptions, isValidNewOption, options, promptTextCreator } = this.props;
@@ -140,7 +81,7 @@ const Creatable = createClass({
14081
}
14182

14283
return filteredOptions;
143-
},
84+
}
14485

14586
isOptionUnique ({
14687
option,
@@ -156,7 +97,7 @@ const Creatable = createClass({
15697
options,
15798
valueKey: this.valueKey
15899
});
159-
},
100+
}
160101

161102
menuRenderer (params) {
162103
const { menuRenderer } = this.props;
@@ -166,7 +107,7 @@ const Creatable = createClass({
166107
onSelect: this.onOptionSelect,
167108
selectValue: this.onOptionSelect
168109
});
169-
},
110+
}
170111

171112
onInputChange (input) {
172113
const { onInputChange } = this.props;
@@ -177,7 +118,7 @@ const Creatable = createClass({
177118

178119
// This value may be needed in between Select mounts (when this.select is null)
179120
this.inputValue = input;
180-
},
121+
}
181122

182123
onInputKeyDown (event) {
183124
const { shouldKeyDownEventCreateNewOption, onInputKeyDown } = this.props;
@@ -195,19 +136,19 @@ const Creatable = createClass({
195136
} else if (onInputKeyDown) {
196137
onInputKeyDown(event);
197138
}
198-
},
139+
}
199140

200141
onOptionSelect (option, event) {
201142
if (option === this._createPlaceholderOption) {
202143
this.createNewOption();
203144
} else {
204145
this.select.selectValue(option);
205146
}
206-
},
147+
}
207148

208149
focus () {
209150
this.select.focus();
210-
},
151+
}
211152

212153
render () {
213154
const {
@@ -245,7 +186,7 @@ const Creatable = createClass({
245186

246187
return children(props);
247188
}
248-
});
189+
};
249190

250191
function defaultChildren (props) {
251192
return (
@@ -269,9 +210,9 @@ function isValidNewOption ({ label }) {
269210
function newOptionCreator ({ label, labelKey, valueKey }) {
270211
const option = {};
271212
option[valueKey] = label;
272-
option[labelKey] = label;
273-
option.className = 'Select-create-option-placeholder';
274-
return option;
213+
option[labelKey] = label;
214+
option.className = 'Select-create-option-placeholder';
215+
return option;
275216
};
276217

277218
function promptTextCreator (label) {
@@ -289,4 +230,68 @@ function shouldKeyDownEventCreateNewOption ({ keyCode }) {
289230
return false;
290231
};
291232

292-
module.exports = Creatable;
233+
// Default prop methods
234+
CreatableSelect.isOptionUnique = isOptionUnique;
235+
CreatableSelect.isValidNewOption = isValidNewOption;
236+
CreatableSelect.newOptionCreator = newOptionCreator;
237+
CreatableSelect.promptTextCreator = promptTextCreator;
238+
CreatableSelect.shouldKeyDownEventCreateNewOption = shouldKeyDownEventCreateNewOption;
239+
240+
241+
CreatableSelect.defaultProps = {
242+
filterOptions: defaultFilterOptions,
243+
isOptionUnique,
244+
isValidNewOption,
245+
menuRenderer: defaultMenuRenderer,
246+
newOptionCreator,
247+
promptTextCreator,
248+
shouldKeyDownEventCreateNewOption
249+
};
250+
251+
CreatableSelect.propTypes = {
252+
// Child function responsible for creating the inner Select component
253+
// This component can be used to compose HOCs (eg Creatable and Async)
254+
// (props: Object): PropTypes.element
255+
children: PropTypes.func,
256+
257+
// See Select.propTypes.filterOptions
258+
filterOptions: PropTypes.any,
259+
260+
// Searches for any matching option within the set of options.
261+
// This function prevents duplicate options from being created.
262+
// ({ option: Object, options: Array, labelKey: string, valueKey: string }): boolean
263+
isOptionUnique: PropTypes.func,
264+
265+
// Determines if the current input text represents a valid option.
266+
// ({ label: string }): boolean
267+
isValidNewOption: PropTypes.func,
268+
269+
// See Select.propTypes.menuRenderer
270+
menuRenderer: PropTypes.any,
271+
272+
// Factory to create new option.
273+
// ({ label: string, labelKey: string, valueKey: string }): Object
274+
newOptionCreator: PropTypes.func,
275+
276+
// input change handler: function (inputValue) {}
277+
onInputChange: PropTypes.func,
278+
279+
// input keyDown handler: function (event) {}
280+
onInputKeyDown: PropTypes.func,
281+
282+
// new option click handler: function (option) {}
283+
onNewOptionClick: PropTypes.func,
284+
285+
// See Select.propTypes.options
286+
options: PropTypes.array,
287+
288+
// Creates prompt/placeholder option text.
289+
// (filterText: string): string
290+
promptTextCreator: PropTypes.func,
291+
292+
// Decides if a keyDown event (eg its `keyCode`) should result in the creation of a new option.
293+
shouldKeyDownEventCreateNewOption: PropTypes.func,
294+
};
295+
296+
297+
module.exports = CreatableSelect;

0 commit comments

Comments
 (0)