Skip to content

Commit

Permalink
Merge pull request #2243 from yuri-sakharov/refactor/src-files
Browse files Browse the repository at this point in the history
Minor src files refactor
  • Loading branch information
JedWatson authored Jan 5, 2018
2 parents 26cf265 + 9185809 commit 99acc31
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 92 deletions.
14 changes: 6 additions & 8 deletions src/Async.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Select from './Select';

import stripDiacritics from './utils/stripDiacritics';

const propTypes = {
autoload: PropTypes.bool.isRequired, // automatically call the `loadOptions` prop on-mount; defaults to true
cache: PropTypes.any, // object to use to cache results; set to null/false to disable caching
Expand Down Expand Up @@ -34,6 +36,8 @@ const propTypes = {

const defaultCache = {};

const defaultChildren = props => <Select {...props} />;

const defaultProps = {
autoload: true,
cache: defaultCache,
Expand Down Expand Up @@ -183,7 +187,7 @@ export default class Async extends Component {
}

render () {
const { children, loadingPlaceholder, multi, onChange, placeholder, value } = this.props;
const { children, loadingPlaceholder, placeholder } = this.props;
const { isLoading, options } = this.state;

const props = {
Expand All @@ -204,9 +208,3 @@ export default class Async extends Component {

Async.propTypes = propTypes;
Async.defaultProps = defaultProps;

function defaultChildren (props) {
return (
<Select {...props} />
);
}
13 changes: 5 additions & 8 deletions src/AsyncCreatable.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 Async from './Async';
import Creatable from './Creatable';
import Select from './Select';

class AsyncCreatableSelect extends React.Component {

Expand Down Expand Up @@ -32,13 +33,9 @@ class AsyncCreatableSelect extends React.Component {
</Async>
);
}
};
}

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

AsyncCreatableSelect.propTypes = {
children: PropTypes.func.isRequired, // Child function responsible for creating the inner Select component; (props: Object): PropTypes.element
Expand Down
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;
45 changes: 22 additions & 23 deletions src/Option.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';

const blockEvent = event => {
event.preventDefault();
event.stopPropagation();
if ((event.target.tagName !== 'A') || !('href' in event.target)) {
return;
}
if (event.target.target) {
window.open(event.target.href, event.target.target);
} else {
window.location.href = event.target.href;
}
};

class Option extends React.Component {

Expand All @@ -16,20 +29,6 @@ class Option extends React.Component {
this.onFocus = this.onFocus.bind(this);
}


blockEvent (event) {
event.preventDefault();
event.stopPropagation();
if ((event.target.tagName !== 'A') || !('href' in event.target)) {
return;
}
if (event.target.target) {
window.open(event.target.href, event.target.target);
} else {
window.location.href = event.target.href;
}
}

handleMouseDown (event) {
event.preventDefault();
event.stopPropagation();
Expand All @@ -52,12 +51,12 @@ class Option extends React.Component {
this.handleMouseDown(event);
}

handleTouchMove (event) {
handleTouchMove () {
// Set a flag that the view is being dragged
this.dragging = true;
}

handleTouchStart (event) {
handleTouchStart () {
// Set a flag that the view is not being dragged
this.dragging = false;
}
Expand All @@ -69,13 +68,13 @@ class Option extends React.Component {
}

render () {
var { option, instancePrefix, optionIndex } = this.props;
var className = classNames(this.props.className, option.className);
const { option, instancePrefix, optionIndex } = this.props;
const className = classNames(this.props.className, option.className);

return option.disabled ? (
<div className={className}
onMouseDown={this.blockEvent}
onClick={this.blockEvent}>
onMouseDown={blockEvent}
onClick={blockEvent}>
{this.props.children}
</div>
) : (
Expand All @@ -95,7 +94,7 @@ class Option extends React.Component {
</div>
);
}
};
}

Option.propTypes = {
children: PropTypes.node,
Expand Down
11 changes: 5 additions & 6 deletions src/Value.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';

class Value extends React.Component {

Expand Down Expand Up @@ -43,12 +43,12 @@ class Value extends React.Component {
this.onRemove(event);
}

handleTouchMove (event) {
handleTouchMove () {
// Set a flag that the view is being dragged
this.dragging = true;
}

handleTouchStart (event) {
handleTouchStart () {
// Set a flag that the view is not being dragged
this.dragging = false;
}
Expand Down Expand Up @@ -91,8 +91,7 @@ class Value extends React.Component {
</div>
);
}
};

}

Value.propTypes = {
children: PropTypes.node,
Expand Down
4 changes: 3 additions & 1 deletion src/utils/defaultArrowRenderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';

export default function arrowRenderer ({ onMouseDown }) {
const arrowRenderer = ({ onMouseDown }) => {
return (
<span
className="Select-arrow"
Expand All @@ -13,3 +13,5 @@ export default function arrowRenderer ({ onMouseDown }) {
arrowRenderer.propTypes = {
onMouseDown: PropTypes.func,
};

export default arrowRenderer;
4 changes: 3 additions & 1 deletion src/utils/defaultClearRenderer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react';

export default function clearRenderer () {
const clearRenderer = () => {
return (
<span
className="Select-clear"
dangerouslySetInnerHTML={{ __html: '&times;' }}
/>
);
};

export default clearRenderer;
20 changes: 10 additions & 10 deletions src/utils/defaultFilterOptions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import stripDiacritics from './stripDiacritics';
import trim from './trim';

function isValid(value) {
const isValid = value => {
return typeof (value) !== 'undefined' && value !== null && value !== '';
}
};

function filterOptions (options, filterValue, excludeOptions, props) {
const filterOptions = (options, filterValue, excludeOptions, props) => {
if (props.ignoreAccents) {
filterValue = stripDiacritics(filterValue);
}
Expand All @@ -25,17 +25,17 @@ function filterOptions (options, filterValue, excludeOptions, props) {
if (props.filterOption) return props.filterOption.call(this, option, filterValue);
if (!filterValue) return true;

var value = option[props.valueKey];
var label = option[props.labelKey];
var hasValue = isValid(value);
var hasLabel = isValid(label);
const value = option[props.valueKey];
const label = option[props.labelKey];
const hasValue = isValid(value);
const hasLabel = isValid(label);

if (!hasValue && !hasLabel) {
return false;
}

var valueTest = hasValue ? String(value) : null;
var labelTest = hasLabel ? String(label) : null;
let valueTest = hasValue ? String(value) : null;
let labelTest = hasLabel ? String(label) : null;

if (props.ignoreAccents) {
if (valueTest && props.matchProp !== 'label') valueTest = stripDiacritics(valueTest);
Expand All @@ -55,6 +55,6 @@ function filterOptions (options, filterValue, excludeOptions, props) {
(labelTest && props.matchProp !== 'value' && labelTest.indexOf(filterValue) >= 0)
);
});
}
};

export default filterOptions;
Loading

0 comments on commit 99acc31

Please sign in to comment.