Skip to content

Commit

Permalink
Refactored Creatable.js
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-sakharov committed Dec 22, 2017
1 parent d205e64 commit 69868a0
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions src/Creatable.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import PropTypes from 'prop-types';
import Select from './Select';
import React from 'react';

import defaultFilterOptions from './utils/defaultFilterOptions';
import defaultMenuRenderer from './utils/defaultMenuRenderer';
import Select from './Select';

class CreatableSelect extends React.Component {
constructor (props, context) {
Expand All @@ -21,7 +22,6 @@ class CreatableSelect extends React.Component {
newOptionCreator,
onNewOptionClick,
options = [],
shouldKeyDownEventCreateNewOption
} = this.props;

if (isValidNewOption({ label: this.inputValue })) {
Expand Down Expand Up @@ -140,7 +140,7 @@ class CreatableSelect extends React.Component {
}
}

onOptionSelect (option, event) {
onOptionSelect (option) {
if (option === this._createPlaceholderOption) {
this.createNewOption();
} else {
Expand All @@ -154,8 +154,6 @@ class CreatableSelect extends React.Component {

render () {
const {
newOptionCreator,
shouldKeyDownEventCreateNewOption,
ref: refProp,
...restProps
} = this.props;
Expand Down Expand Up @@ -192,19 +190,15 @@ class CreatableSelect extends React.Component {

return children(props);
}
};
}

function defaultChildren (props) {
return (
<Select {...props} />
);
};
const defaultChildren = props => <Select {...props} />;

function isOptionUnique ({ option, options, labelKey, valueKey }) {
const isOptionUnique = ({ option, options, labelKey, valueKey }) => {
if (!options || !options.length) {
return true;
}

return options
.filter((existingOption) =>
existingOption[labelKey] === option[labelKey] ||
Expand All @@ -213,23 +207,20 @@ function isOptionUnique ({ option, options, labelKey, valueKey }) {
.length === 0;
};

function isValidNewOption ({ label }) {
return !!label;
};
const isValidNewOption = ({ label }) => !!label;

function newOptionCreator ({ label, labelKey, valueKey }) {
const newOptionCreator = ({ label, labelKey, valueKey }) => {
const option = {};
option[valueKey] = label;
option[labelKey] = label;
option.className = 'Select-create-option-placeholder';

return option;
};

function promptTextCreator (label) {
return `Create option "${label}"`;
}
const promptTextCreator = label => `Create option "${label}"`;

function shouldKeyDownEventCreateNewOption ({ keyCode }) {
const shouldKeyDownEventCreateNewOption = ({ keyCode }) => {
switch (keyCode) {
case 9: // TAB
case 13: // ENTER
Expand All @@ -240,7 +231,7 @@ function shouldKeyDownEventCreateNewOption ({ keyCode }) {
}
};

// Default prop methods
// Default prop methods
CreatableSelect.isOptionUnique = isOptionUnique;
CreatableSelect.isValidNewOption = isValidNewOption;
CreatableSelect.newOptionCreator = newOptionCreator;
Expand Down Expand Up @@ -305,5 +296,4 @@ CreatableSelect.propTypes = {
shouldKeyDownEventCreateNewOption: PropTypes.func,
};


export default CreatableSelect;

0 comments on commit 69868a0

Please sign in to comment.