diff --git a/src/Async.js b/src/Async.js index 8b7d8330d6..66810ee168 100644 --- a/src/Async.js +++ b/src/Async.js @@ -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 @@ -34,6 +36,8 @@ const propTypes = { const defaultCache = {}; +const defaultChildren = props => - ); -} diff --git a/src/AsyncCreatable.js b/src/AsyncCreatable.js index 55d064818c..cf3aa0ecae 100644 --- a/src/AsyncCreatable.js +++ b/src/AsyncCreatable.js @@ -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 { @@ -32,13 +33,9 @@ class AsyncCreatableSelect extends React.Component { ); } -}; +} -function defaultChildren (props) { - return ( - ; AsyncCreatableSelect.propTypes = { children: PropTypes.func.isRequired, // Child function responsible for creating the inner Select component; (props: Object): PropTypes.element diff --git a/src/Creatable.js b/src/Creatable.js index 43b26ab5aa..ec1fc44163 100644 --- a/src/Creatable.js +++ b/src/Creatable.js @@ -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) { @@ -21,7 +22,6 @@ class CreatableSelect extends React.Component { newOptionCreator, onNewOptionClick, options = [], - shouldKeyDownEventCreateNewOption } = this.props; if (isValidNewOption({ label: this.inputValue })) { @@ -140,7 +140,7 @@ class CreatableSelect extends React.Component { } } - onOptionSelect (option, event) { + onOptionSelect (option) { if (option === this._createPlaceholderOption) { this.createNewOption(); } else { @@ -154,8 +154,6 @@ class CreatableSelect extends React.Component { render () { const { - newOptionCreator, - shouldKeyDownEventCreateNewOption, ref: refProp, ...restProps } = this.props; @@ -192,19 +190,15 @@ class CreatableSelect extends React.Component { return children(props); } -}; +} -function defaultChildren (props) { - return ( - ; -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] || @@ -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 @@ -240,7 +231,7 @@ function shouldKeyDownEventCreateNewOption ({ keyCode }) { } }; - // Default prop methods +// Default prop methods CreatableSelect.isOptionUnique = isOptionUnique; CreatableSelect.isValidNewOption = isValidNewOption; CreatableSelect.newOptionCreator = newOptionCreator; @@ -305,5 +296,4 @@ CreatableSelect.propTypes = { shouldKeyDownEventCreateNewOption: PropTypes.func, }; - export default CreatableSelect; diff --git a/src/Option.js b/src/Option.js index 7a3de5da9a..4eb85818ad 100644 --- a/src/Option.js +++ b/src/Option.js @@ -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 { @@ -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(); @@ -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; } @@ -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 ? (
+ onMouseDown={blockEvent} + onClick={blockEvent}> {this.props.children}
) : ( @@ -95,7 +94,7 @@ class Option extends React.Component { ); } -}; +} Option.propTypes = { children: PropTypes.node, diff --git a/src/Value.js b/src/Value.js index e282fd18ca..d405e78dfc 100644 --- a/src/Value.js +++ b/src/Value.js @@ -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 { @@ -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; } @@ -91,8 +91,7 @@ class Value extends React.Component { ); } -}; - +} Value.propTypes = { children: PropTypes.node, diff --git a/src/utils/defaultArrowRenderer.js b/src/utils/defaultArrowRenderer.js index 3fee9f0ec4..b72ae139ad 100644 --- a/src/utils/defaultArrowRenderer.js +++ b/src/utils/defaultArrowRenderer.js @@ -1,7 +1,7 @@ import React from 'react'; import PropTypes from 'prop-types'; -export default function arrowRenderer ({ onMouseDown }) { +const arrowRenderer = ({ onMouseDown }) => { return ( { return ( ); }; + +export default clearRenderer; diff --git a/src/utils/defaultFilterOptions.js b/src/utils/defaultFilterOptions.js index 5ae9b72410..9d92331e8c 100644 --- a/src/utils/defaultFilterOptions.js +++ b/src/utils/defaultFilterOptions.js @@ -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); } @@ -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); @@ -55,6 +55,6 @@ function filterOptions (options, filterValue, excludeOptions, props) { (labelTest && props.matchProp !== 'value' && labelTest.indexOf(filterValue) >= 0) ); }); -} +}; export default filterOptions; diff --git a/src/utils/defaultMenuRenderer.js b/src/utils/defaultMenuRenderer.js index 224a2376a5..d2fd6494ac 100644 --- a/src/utils/defaultMenuRenderer.js +++ b/src/utils/defaultMenuRenderer.js @@ -1,12 +1,12 @@ import classNames from 'classnames'; +import PropTypes from 'prop-types'; import React from 'react'; -function menuRenderer ({ +const menuRenderer = ({ focusedOption, focusOption, inputValue, instancePrefix, - labelKey, onFocus, onOptionRef, onSelect, @@ -18,11 +18,11 @@ function menuRenderer ({ selectValue, valueArray, valueKey, -}) { +}) => { let Option = optionComponent; return options.map((option, i) => { - let isSelected = valueArray && valueArray.some(x => x[valueKey] == option[valueKey]); + let isSelected = valueArray && valueArray.some(x => x[valueKey] === option[valueKey]); let isFocused = option === focusedOption; let optionClass = classNames(optionClassName, { 'Select-option': true, @@ -53,6 +53,24 @@ function menuRenderer ({ ); }); -} +}; + +menuRenderer.propTypes = { + focusOption: PropTypes.func, + focusedOption: PropTypes.object, + inputValue: PropTypes.string, + instancePrefix: PropTypes.string, + onFocus: PropTypes.func, + onOptionRef: PropTypes.func, + onSelect: PropTypes.func, + optionClassName: PropTypes.string, + optionComponent: PropTypes.func, + optionRenderer: PropTypes.func, + options: PropTypes.array, + removeValue: PropTypes.func, + selectValue: PropTypes.func, + valueArray: PropTypes.array, + valueKey: PropTypes.string, +}; export default menuRenderer; diff --git a/src/utils/stripDiacritics.js b/src/utils/stripDiacritics.js index 478d26e6b9..d6456ddc91 100644 --- a/src/utils/stripDiacritics.js +++ b/src/utils/stripDiacritics.js @@ -1,4 +1,4 @@ -var map = [ +const map = [ { 'base':'A', 'letters':/[\u0041\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u00C4\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F]/g }, { 'base':'AA','letters':/[\uA732]/g }, { 'base':'AE','letters':/[\u00C6\u01FC\u01E2]/g }, @@ -85,9 +85,11 @@ var map = [ { 'base':'z', 'letters':/[\u007A\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763]/g }, ]; -export default function stripDiacritics (str) { - for (var i = 0; i < map.length; i++) { +const stripDiacritics = str => { + for (let i = 0; i < map.length; i++) { str = str.replace(map[i].letters, map[i].base); } return str; }; + +export default stripDiacritics; diff --git a/src/utils/trim.js b/src/utils/trim.js index 56aef05bd2..4f0172a419 100644 --- a/src/utils/trim.js +++ b/src/utils/trim.js @@ -1,3 +1,3 @@ -export default function trim(str) { - return str.replace(/^\s+|\s+$/g, ''); -} +const trim = str => str.replace(/^\s+|\s+$/g, ''); + +export default trim;