react-select-plus
Version:
A fork of react-select with support for option groups
1,389 lines (1,219 loc) • 47 kB
JavaScript
/*!
Copyright (c) 2016 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/react-select
*/
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
var _reactInputAutosize = require('react-input-autosize');
var _reactInputAutosize2 = _interopRequireDefault(_reactInputAutosize);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _utilsDefaultArrowRenderer = require('./utils/defaultArrowRenderer');
var _utilsDefaultArrowRenderer2 = _interopRequireDefault(_utilsDefaultArrowRenderer);
var _utilsDefaultFilterOptions = require('./utils/defaultFilterOptions');
var _utilsDefaultFilterOptions2 = _interopRequireDefault(_utilsDefaultFilterOptions);
var _utilsDefaultMenuRenderer = require('./utils/defaultMenuRenderer');
var _utilsDefaultMenuRenderer2 = _interopRequireDefault(_utilsDefaultMenuRenderer);
var _utilsDefaultClearRenderer = require('./utils/defaultClearRenderer');
var _utilsDefaultClearRenderer2 = _interopRequireDefault(_utilsDefaultClearRenderer);
var _utilsStripDiacritics = require('./utils/stripDiacritics');
var _utilsStripDiacritics2 = _interopRequireDefault(_utilsStripDiacritics);
var _Async = require('./Async');
var _Async2 = _interopRequireDefault(_Async);
var _AsyncCreatable = require('./AsyncCreatable');
var _AsyncCreatable2 = _interopRequireDefault(_AsyncCreatable);
var _Creatable = require('./Creatable');
var _Creatable2 = _interopRequireDefault(_Creatable);
var _Dropdown = require('./Dropdown');
var _Dropdown2 = _interopRequireDefault(_Dropdown);
var _Option = require('./Option');
var _Option2 = _interopRequireDefault(_Option);
var _OptionGroup = require('./OptionGroup');
var _OptionGroup2 = _interopRequireDefault(_OptionGroup);
var _Value = require('./Value');
var _Value2 = _interopRequireDefault(_Value);
function clone(obj) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) {
copy[attr] = obj[attr];
};
}
return copy;
}
function isGroup(option) {
return option && Array.isArray(option.options);
}
function stringifyValue(value) {
var valueType = typeof value;
if (valueType === 'string') {
return value;
} else if (valueType === 'object') {
return JSON.stringify(value);
} else if (valueType === 'number' || valueType === 'boolean') {
return String(value);
} else {
return '';
}
}
var stringOrNode = _react2['default'].PropTypes.oneOfType([_react2['default'].PropTypes.string, _react2['default'].PropTypes.node]);
var instanceId = 1;
var invalidOptions = {};
var Select = _react2['default'].createClass({
displayName: 'Select',
propTypes: {
addLabelText: _react2['default'].PropTypes.string, // placeholder displayed when you want to add a label on a multi-value input
'aria-describedby': _react2['default'].PropTypes.string, // HTML ID(s) of element(s) that should be used to describe this input (for assistive tech)
'aria-label': _react2['default'].PropTypes.string, // Aria label (for assistive tech)
'aria-labelledby': _react2['default'].PropTypes.string, // HTML ID of an element that should be used as the label (for assistive tech)
arrowRenderer: _react2['default'].PropTypes.func, // Create drop-down caret element
autoBlur: _react2['default'].PropTypes.bool, // automatically blur the component when an option is selected
autofocus: _react2['default'].PropTypes.bool, // autofocus the component on mount
autosize: _react2['default'].PropTypes.bool, // whether to enable autosizing or not
backspaceRemoves: _react2['default'].PropTypes.bool, // whether backspace removes an item if there is no text input
backspaceToRemoveMessage: _react2['default'].PropTypes.string, // Message to use for screenreaders to press backspace to remove the current item - {label} is replaced with the item label
className: _react2['default'].PropTypes.string, // className for the outer element
clearAllText: stringOrNode, // title for the "clear" control when multi: true
clearRenderer: _react2['default'].PropTypes.func, // create clearable x element
clearValueText: stringOrNode, // title for the "clear" control
clearable: _react2['default'].PropTypes.bool, // should it be possible to reset value
deleteRemoves: _react2['default'].PropTypes.bool, // whether backspace removes an item if there is no text input
delimiter: _react2['default'].PropTypes.string, // delimiter to use to join multiple values for the hidden field value
disabled: _react2['default'].PropTypes.bool, // whether the Select is disabled or not
dropdownComponent: _react2['default'].PropTypes.func, // dropdown component to render the menu in
escapeClearsValue: _react2['default'].PropTypes.bool, // whether escape clears the value when the menu is closed
filterOption: _react2['default'].PropTypes.func, // method to filter a single option (option, filterString)
filterOptions: _react2['default'].PropTypes.any, // boolean to enable default filtering or function to filter the options array ([options], filterString, [values])
ignoreAccents: _react2['default'].PropTypes.bool, // whether to strip diacritics when filtering
ignoreCase: _react2['default'].PropTypes.bool, // whether to perform case-insensitive filtering
inputProps: _react2['default'].PropTypes.object, // custom attributes for the Input
inputRenderer: _react2['default'].PropTypes.func, // returns a custom input component
instanceId: _react2['default'].PropTypes.string, // set the components instanceId
isLoading: _react2['default'].PropTypes.bool, // whether the Select is loading externally or not (such as options being loaded)
isOpen: _react2['default'].PropTypes.bool, // whether the Select dropdown menu is open or not
joinValues: _react2['default'].PropTypes.bool, // joins multiple values into a single form field with the delimiter (legacy mode)
labelKey: _react2['default'].PropTypes.string, // path of the label value in option objects
matchPos: _react2['default'].PropTypes.string, // (any|start) match the start or entire string when filtering
matchProp: _react2['default'].PropTypes.string, // (any|label|value) which option property to filter on
menuBuffer: _react2['default'].PropTypes.number, // optional buffer (in px) between the bottom of the viewport and the bottom of the menu
menuContainerStyle: _react2['default'].PropTypes.object, // optional style to apply to the menu container
menuRenderer: _react2['default'].PropTypes.func, // renders a custom menu with options
menuStyle: _react2['default'].PropTypes.object, // optional style to apply to the menu
multi: _react2['default'].PropTypes.bool, // multi-value input
name: _react2['default'].PropTypes.string, // generates a hidden <input /> tag with this field name for html forms
noResultsText: stringOrNode, // placeholder displayed when there are no matching search results
onBlur: _react2['default'].PropTypes.func, // onBlur handler: function (event) {}
onBlurResetsInput: _react2['default'].PropTypes.bool, // whether input is cleared on blur
onChange: _react2['default'].PropTypes.func, // onChange handler: function (newValue) {}
onClose: _react2['default'].PropTypes.func, // fires when the menu is closed
onCloseResetsInput: _react2['default'].PropTypes.bool, // whether input is cleared when menu is closed through the arrow
onFocus: _react2['default'].PropTypes.func, // onFocus handler: function (event) {}
onInputChange: _react2['default'].PropTypes.func, // onInputChange handler: function (inputValue) {}
onInputKeyDown: _react2['default'].PropTypes.func, // input keyDown handler: function (event) {}
onMenuScrollToBottom: _react2['default'].PropTypes.func, // fires when the menu is scrolled to the bottom; can be used to paginate options
onOpen: _react2['default'].PropTypes.func, // fires when the menu is opened
onValueClick: _react2['default'].PropTypes.func, // onClick handler for value labels: function (value, event) {}
openAfterFocus: _react2['default'].PropTypes.bool, // boolean to enable opening dropdown when focused
openOnFocus: _react2['default'].PropTypes.bool, // always open options menu on focus
optionClassName: _react2['default'].PropTypes.string, // additional class(es) to apply to the <Option /> elements
optionComponent: _react2['default'].PropTypes.func, // option component to render in dropdown
optionGroupComponent: _react2['default'].PropTypes.func, // option group component to render in dropdown
optionRenderer: _react2['default'].PropTypes.func, // optionRenderer: function (option) {}
options: _react2['default'].PropTypes.array, // array of options
pageSize: _react2['default'].PropTypes.number, // number of entries to page when using page up/down keys
placeholder: stringOrNode, // field placeholder, displayed when there's no value
renderInvalidValues: _react2['default'].PropTypes.bool, // boolean to enable rendering values that do not match any options
required: _react2['default'].PropTypes.bool, // applies HTML5 required attribute when needed
resetValue: _react2['default'].PropTypes.any, // value to use when you clear the control
scrollMenuIntoView: _react2['default'].PropTypes.bool, // boolean to enable the viewport to shift so that the full menu fully visible when engaged
searchable: _react2['default'].PropTypes.bool, // whether to enable searching feature or not
simpleValue: _react2['default'].PropTypes.bool, // pass the value to onChange as a simple value (legacy pre 1.0 mode), defaults to false
style: _react2['default'].PropTypes.object, // optional style to apply to the control
tabIndex: _react2['default'].PropTypes.string, // optional tab index of the control
tabSelectsValue: _react2['default'].PropTypes.bool, // whether to treat tabbing out while focused to be value selection
value: _react2['default'].PropTypes.any, // initial field value
valueComponent: _react2['default'].PropTypes.func, // value component to render
valueKey: _react2['default'].PropTypes.string, // path of the label value in option objects
valueRenderer: _react2['default'].PropTypes.func, // valueRenderer: function (option) {}
wrapperStyle: _react2['default'].PropTypes.object },
// optional style to apply to the component wrapper
statics: { Async: _Async2['default'], AsyncCreatable: _AsyncCreatable2['default'], Creatable: _Creatable2['default'] },
getDefaultProps: function getDefaultProps() {
return {
addLabelText: 'Add "{label}"?',
arrowRenderer: _utilsDefaultArrowRenderer2['default'],
autosize: true,
backspaceRemoves: true,
backspaceToRemoveMessage: 'Press backspace to remove {label}',
clearable: true,
clearAllText: 'Clear all',
clearRenderer: _utilsDefaultClearRenderer2['default'],
clearValueText: 'Clear value',
deleteRemoves: true,
delimiter: ',',
disabled: false,
dropdownComponent: _Dropdown2['default'],
escapeClearsValue: true,
filterOptions: _utilsDefaultFilterOptions2['default'],
ignoreAccents: true,
ignoreCase: true,
inputProps: {},
isLoading: false,
joinValues: false,
labelKey: 'label',
matchPos: 'any',
matchProp: 'any',
menuBuffer: 0,
menuRenderer: _utilsDefaultMenuRenderer2['default'],
multi: false,
noResultsText: 'No results found',
onBlurResetsInput: true,
onCloseResetsInput: true,
openAfterFocus: false,
optionComponent: _Option2['default'],
optionGroupComponent: _OptionGroup2['default'],
pageSize: 5,
placeholder: 'Select...',
renderInvalidValues: false,
required: false,
scrollMenuIntoView: true,
searchable: true,
simpleValue: false,
tabSelectsValue: true,
valueComponent: _Value2['default'],
valueKey: 'value'
};
},
getInitialState: function getInitialState() {
return {
inputValue: '',
isFocused: false,
isOpen: this.props.isOpen != null ? this.props.isOpen : false,
isPseudoFocused: false,
required: false
};
},
componentWillMount: function componentWillMount() {
this._flatOptions = this.flattenOptions(this.props.options);
this._instancePrefix = 'react-select-' + (this.props.instanceId || ++instanceId) + '-';
var valueArray = this.getValueArray(this.props.value);
if (this.props.required) {
this.setState({
required: this.handleRequired(valueArray[0], this.props.multi)
});
}
},
componentDidMount: function componentDidMount() {
if (this.props.autofocus) {
this.focus();
}
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
if (nextProps.options !== this.props.options) {
this._flatOptions = this.flattenOptions(nextProps.options);
}
var valueArray = this.getValueArray(nextProps.value, nextProps);
if (!nextProps.isOpen && this.props.isOpen) {
this.closeMenu();
}
if (nextProps.required) {
this.setState({
required: this.handleRequired(valueArray[0], nextProps.multi)
});
}
},
componentWillUpdate: function componentWillUpdate(nextProps, nextState) {
if (nextState.isOpen !== this.state.isOpen) {
this.toggleTouchOutsideEvent(nextState.isOpen);
var handler = nextState.isOpen ? nextProps.onOpen : nextProps.onClose;
handler && handler();
}
},
componentDidUpdate: function componentDidUpdate(prevProps, prevState) {
// focus to the selected option
if (this.menu && this.focused && this.state.isOpen && !this.hasScrolledToOption) {
var focusedOptionNode = _reactDom2['default'].findDOMNode(this.focused);
var focusedOptionPreviousSibling = focusedOptionNode.previousSibling;
var focusedOptionParent = focusedOptionNode.parentElement;
var menuNode = _reactDom2['default'].findDOMNode(this.menu);
if (focusedOptionPreviousSibling) {
menuNode.scrollTop = focusedOptionPreviousSibling.offsetTop;
} else if (focusedOptionParent && focusedOptionParent === 'Select-menu') {
menuNode.scrollTop = focusedOptionParent.offsetTop;
} else {
menuNode.scrollTop = focusedOptionNode.offsetTop;
}
var paddingTop = parseInt(window.getComputedStyle(menuNode, null).paddingTop, 10);
if (menuNode.scrollTop <= paddingTop) menuNode.scrollTop = 0;
this.hasScrolledToOption = true;
} else if (!this.state.isOpen) {
this.hasScrolledToOption = false;
}
if (this._scrollToFocusedOptionOnUpdate && this.focused && this.menu) {
this._scrollToFocusedOptionOnUpdate = false;
var focusedDOM = _reactDom2['default'].findDOMNode(this.focused);
var menuDOM = _reactDom2['default'].findDOMNode(this.menu);
var focusedRect = focusedDOM.getBoundingClientRect();
var menuRect = menuDOM.getBoundingClientRect();
if (focusedRect.bottom > menuRect.bottom || focusedRect.top < menuRect.top) {
menuDOM.scrollTop = focusedDOM.offsetTop + focusedDOM.clientHeight - menuDOM.offsetHeight;
}
}
if (this.props.scrollMenuIntoView && this.menuContainer) {
var menuContainerRect = this.menuContainer.getBoundingClientRect();
if (window.innerHeight < menuContainerRect.bottom + this.props.menuBuffer) {
window.scrollBy(0, menuContainerRect.bottom + this.props.menuBuffer - window.innerHeight);
}
}
if (prevProps.disabled !== this.props.disabled) {
this.setState({ isFocused: false }); // eslint-disable-line react/no-did-update-set-state
this.closeMenu();
}
},
componentWillUnmount: function componentWillUnmount() {
if (!document.removeEventListener && document.detachEvent) {
document.detachEvent('ontouchstart', this.handleTouchOutside);
} else {
document.removeEventListener('touchstart', this.handleTouchOutside);
}
},
toggleTouchOutsideEvent: function toggleTouchOutsideEvent(enabled) {
if (enabled) {
if (!document.addEventListener && document.attachEvent) {
document.attachEvent('ontouchstart', this.handleTouchOutside);
} else {
document.addEventListener('touchstart', this.handleTouchOutside);
}
} else {
if (!document.removeEventListener && document.detachEvent) {
document.detachEvent('ontouchstart', this.handleTouchOutside);
} else {
document.removeEventListener('touchstart', this.handleTouchOutside);
}
}
},
handleTouchOutside: function handleTouchOutside(event) {
// handle touch outside on ios to dismiss menu
if (this.wrapper && !this.wrapper.contains(event.target) && this.menuContainer && !this.menuContainer.contains(event.target)) {
this.closeMenu();
}
},
focus: function focus() {
if (!this.input) return;
this.input.focus();
if (this.props.openAfterFocus) {
this.setState({
isOpen: true
});
}
},
blurInput: function blurInput() {
if (!this.input) return;
this.input.blur();
},
handleTouchMove: function handleTouchMove(event) {
// Set a flag that the view is being dragged
this.dragging = true;
},
handleTouchStart: function handleTouchStart(event) {
// Set a flag that the view is not being dragged
this.dragging = false;
},
handleTouchEnd: function handleTouchEnd(event) {
// Check if the view is being dragged, In this case
// we don't want to fire the click event (because the user only wants to scroll)
if (this.dragging) return;
// Fire the mouse events
this.handleMouseDown(event);
},
handleTouchEndClearValue: function handleTouchEndClearValue(event) {
// Check if the view is being dragged, In this case
// we don't want to fire the click event (because the user only wants to scroll)
if (this.dragging) return;
// Clear the value
this.clearValue(event);
},
handleMouseDown: function handleMouseDown(event) {
// if the event was triggered by a mousedown and not the primary
// button, or if the component is disabled, ignore it.
if (this.props.disabled || event.type === 'mousedown' && event.button !== 0) {
return;
}
if (event.target.tagName === 'INPUT') {
return;
}
// prevent default event handlers
event.stopPropagation();
event.preventDefault();
// for the non-searchable select, toggle the menu
if (!this.props.searchable) {
this.focus();
return this.setState({
isOpen: !this.state.isOpen
});
}
if (this.state.isFocused) {
// On iOS, we can get into a state where we think the input is focused but it isn't really,
// since iOS ignores programmatic calls to input.focus() that weren't triggered by a click event.
// Call focus() again here to be safe.
this.focus();
var input = this.input;
if (typeof input.getInput === 'function') {
// Get the actual DOM input if the ref is an <AutosizeInput /> component
input = input.getInput();
}
// clears the value so that the cursor will be at the end of input when the component re-renders
input.value = '';
// if the input is focused, ensure the menu is open
this.setState({
isOpen: true,
isPseudoFocused: false
});
} else {
// otherwise, focus the input and open the menu
this._openAfterFocus = this.props.openOnFocus;
this.focus();
}
},
handleMouseDownOnArrow: function handleMouseDownOnArrow(event) {
// if the event was triggered by a mousedown and not the primary
// button, or if the component is disabled, ignore it.
if (this.props.disabled || event.type === 'mousedown' && event.button !== 0) {
return;
}
// If the menu isn't open, let the event bubble to the main handleMouseDown
if (!this.state.isOpen) {
return;
}
// prevent default event handlers
event.stopPropagation();
event.preventDefault();
// close the menu
this.closeMenu();
},
handleMouseDownOnMenu: function handleMouseDownOnMenu(event) {
// if the event was triggered by a mousedown and not the primary
// button, or if the component is disabled, ignore it.
if (this.props.disabled || event.type === 'mousedown' && event.button !== 0) {
return;
}
event.stopPropagation();
event.preventDefault();
this._openAfterFocus = true;
this.focus();
},
closeMenu: function closeMenu() {
var _this = this;
if (this.props.onCloseResetsInput) {
this.setState({
isOpen: false,
isPseudoFocused: this.state.isFocused && !this.props.multi,
inputValue: ''
}, function () {
if (_this.props.onInputChange) _this.props.onInputChange('');
});
} else {
this.setState({
isOpen: false,
isPseudoFocused: this.state.isFocused && !this.props.multi,
inputValue: this.state.inputValue
});
}
this.hasScrolledToOption = false;
},
handleInputFocus: function handleInputFocus(event) {
if (this.props.disabled) return;
var isOpen = this.state.isOpen || this._openAfterFocus || this.props.openOnFocus;
if (this.props.onFocus) {
this.props.onFocus(event);
}
this.setState({
isFocused: true,
isOpen: isOpen
});
this._openAfterFocus = false;
},
handleInputBlur: function handleInputBlur(event) {
// The check for menu.contains(activeElement) is necessary to prevent IE11's scrollbar from closing the menu in certain contexts.
if (this.menu && (this.menu === document.activeElement || this.menu.contains(document.activeElement))) {
this.focus();
return;
}
if (this.props.onBlur) {
this.props.onBlur(event);
}
var onBlurredState = {
isFocused: false,
isOpen: false,
isPseudoFocused: false
};
if (this.props.onBlurResetsInput) {
onBlurredState.inputValue = '';
}
this.setState(onBlurredState);
},
handleInputChange: function handleInputChange(event) {
var newInputValue = event.target.value;
if (this.state.inputValue !== event.target.value && this.props.onInputChange) {
var nextState = this.props.onInputChange(newInputValue);
// Note: != used deliberately here to catch undefined and null
if (nextState != null && typeof nextState !== 'object') {
newInputValue = '' + nextState;
}
}
this.setState({
isOpen: true,
isPseudoFocused: false,
inputValue: newInputValue
});
},
handleKeyDown: function handleKeyDown(event) {
if (this.props.disabled) return;
if (typeof this.props.onInputKeyDown === 'function') {
this.props.onInputKeyDown(event);
if (event.defaultPrevented) {
return;
}
}
switch (event.keyCode) {
case 8:
// backspace
if (!this.state.inputValue && this.props.backspaceRemoves) {
event.preventDefault();
this.popValue();
}
return;
case 9:
// tab
if (event.shiftKey || !this.state.isOpen || !this.props.tabSelectsValue) {
return;
}
this.selectFocusedOption();
return;
case 13:
// enter
if (!this.state.isOpen) {
this.setState({
isOpen: true
});
return;
};
event.stopPropagation();
this.selectFocusedOption();
break;
case 27:
// escape
if (this.state.isOpen) {
this.closeMenu();
event.stopPropagation();
} else if (this.props.clearable && this.props.escapeClearsValue) {
this.clearValue(event);
event.stopPropagation();
}
break;
case 38:
// up
this.focusPreviousOption();
break;
case 40:
// down
this.focusNextOption();
break;
case 33:
// page up
this.focusPageUpOption();
break;
case 34:
// page down
this.focusPageDownOption();
break;
case 35:
// end key
if (event.shiftKey) {
return;
}
this.focusEndOption();
break;
case 36:
// home key
if (event.shiftKey) {
return;
}
this.focusStartOption();
break;
case 46:
// backspace
if (!this.state.inputValue && this.props.deleteRemoves) {
event.preventDefault();
this.popValue();
}
return;
default:
return;
}
event.preventDefault();
},
handleValueClick: function handleValueClick(option, event) {
if (!this.props.onValueClick) return;
this.props.onValueClick(option, event);
},
handleMenuScroll: function handleMenuScroll(event) {
if (!this.props.onMenuScrollToBottom) return;
var target = event.target;
if (target.scrollHeight > target.offsetHeight && !(target.scrollHeight - target.offsetHeight - target.scrollTop)) {
this.props.onMenuScrollToBottom();
}
},
handleRequired: function handleRequired(value, multi) {
if (!value) return true;
return multi ? value.length === 0 : Object.keys(value).length === 0;
},
getOptionLabel: function getOptionLabel(op) {
return op[this.props.labelKey];
},
/**
* Turns a value into an array from the given options
* @param {String|Number|Array} value - the value of the select input
* @param {Object} nextProps - optionally specify the nextProps so the returned array uses the latest configuration
* @returns {Array} the value of the select represented in an array
*/
getValueArray: function getValueArray(value, nextProps) {
var _this2 = this;
/** support optionally passing in the `nextProps` so `componentWillReceiveProps` updates will function as expected */
var props = typeof nextProps === 'object' ? nextProps : this.props;
if (props.multi) {
if (typeof value === 'string') value = value.split(props.delimiter);
if (!Array.isArray(value)) {
if (value === null || value === undefined) return [];
value = [value];
}
return value.map(function (value) {
return _this2.expandValue(value, props);
}).filter(function (i) {
return i;
});
}
var expandedValue = this.expandValue(value, props);
return expandedValue ? [expandedValue] : [];
},
/**
* Retrieve a value from the given options and valueKey
* @param {String|Number|Array} value - the selected value(s)
* @param {Object} props - the Select component's props (or nextProps)
*/
expandValue: function expandValue(value, props) {
var valueType = typeof value;
if (valueType !== 'string' && valueType !== 'number' && valueType !== 'boolean') return value;
var _props = this.props;
var labelKey = _props.labelKey;
var valueKey = _props.valueKey;
var renderInvalidValues = _props.renderInvalidValues;
var options = this._flatOptions;
if (!options || value === '') return;
for (var i = 0; i < options.length; i++) {
if (options[i][valueKey] === value) return options[i];
}
// no matching option, return an invalid option if renderInvalidValues is enabled
if (renderInvalidValues) {
var _ref;
invalidOptions[value] = invalidOptions[value] || (_ref = {
invalid: true
}, _defineProperty(_ref, labelKey, value), _defineProperty(_ref, valueKey, value), _ref);
return invalidOptions[value];
}
},
setValue: function setValue(value) {
var _this3 = this;
if (this.props.autoBlur) {
this.blurInput();
}
if (!this.props.onChange) return;
if (this.props.required) {
var required = this.handleRequired(value, this.props.multi);
this.setState({ required: required });
}
if (this.props.simpleValue && value) {
value = this.props.multi ? value.map(function (i) {
return i[_this3.props.valueKey];
}).join(this.props.delimiter) : value[this.props.valueKey];
}
this.props.onChange(value);
},
selectValue: function selectValue(value) {
var _this4 = this;
//NOTE: update value in the callback to make sure the input value is empty so that there are no styling issues (Chrome had issue otherwise)
this.hasScrolledToOption = false;
if (this.props.multi) {
this.setState({
inputValue: '',
focusedIndex: null
}, function () {
_this4.addValue(value);
if (_this4.props.onInputChange) _this4.props.onInputChange('');
});
} else {
this.setState({
isOpen: false,
inputValue: '',
isPseudoFocused: this.state.isFocused
}, function () {
_this4.setValue(value);
if (_this4.props.onInputChange) _this4.props.onInputChange('');
});
}
},
addValue: function addValue(value) {
var valueArray = this.getValueArray(this.props.value);
var visibleOptions = this._visibleOptions.filter(function (val) {
return !val.disabled;
});
var lastValueIndex = visibleOptions.indexOf(value);
this.setValue(valueArray.concat(value));
if (visibleOptions.length - 1 === lastValueIndex) {
// the last option was selected; focus the second-last one
this.focusOption(visibleOptions[lastValueIndex - 1]);
} else if (visibleOptions.length > lastValueIndex) {
// focus the option below the selected one
this.focusOption(visibleOptions[lastValueIndex + 1]);
}
},
popValue: function popValue() {
var valueArray = this.getValueArray(this.props.value);
if (!valueArray.length) return;
if (valueArray[valueArray.length - 1].clearableValue === false) return;
this.setValue(valueArray.slice(0, valueArray.length - 1));
},
removeValue: function removeValue(value) {
var valueArray = this.getValueArray(this.props.value);
this.setValue(valueArray.filter(function (i) {
return i !== value;
}));
},
clearValue: function clearValue(event) {
var _this5 = this;
// if the event was triggered by a mousedown and not the primary
// button, ignore it.
if (event && event.type === 'mousedown' && event.button !== 0) {
return;
}
event.stopPropagation();
event.preventDefault();
this.setValue(this.getResetValue());
this.setState({
isOpen: false,
inputValue: ''
}, function () {
_this5.focus();
if (_this5.props.onInputChange) _this5.props.onInputChange('');
});
},
getResetValue: function getResetValue() {
if (this.props.resetValue !== undefined) {
return this.props.resetValue;
} else if (this.props.multi) {
return [];
} else {
return null;
}
},
focusOption: function focusOption(option) {
this.setState({
focusedOption: option
});
},
focusNextOption: function focusNextOption() {
this.focusAdjacentOption('next');
},
focusPreviousOption: function focusPreviousOption() {
this.focusAdjacentOption('previous');
},
focusPageUpOption: function focusPageUpOption() {
this.focusAdjacentOption('page_up');
},
focusPageDownOption: function focusPageDownOption() {
this.focusAdjacentOption('page_down');
},
focusStartOption: function focusStartOption() {
this.focusAdjacentOption('start');
},
focusEndOption: function focusEndOption() {
this.focusAdjacentOption('end');
},
focusAdjacentOption: function focusAdjacentOption(dir) {
var _this6 = this;
var options = this._visibleOptions.map(function (option, index) {
return { option: option, index: index };
}).filter(function (option) {
return !option.option.disabled;
});
this._scrollToFocusedOptionOnUpdate = true;
if (!this.state.isOpen) {
this.setState({
isOpen: true,
inputValue: '',
focusedOption: this._focusedOption || (options.length ? options[dir === 'next' ? 0 : options.length - 1].option : null)
}, function () {
if (_this6.props.onInputChange) _this6.props.onInputChange('');
});
return;
}
if (!options.length) return;
var focusedIndex = -1;
for (var i = 0; i < options.length; i++) {
if (this._focusedOption === options[i].option) {
focusedIndex = i;
break;
}
}
if (dir === 'next' && focusedIndex !== -1) {
focusedIndex = (focusedIndex + 1) % options.length;
} else if (dir === 'previous') {
if (focusedIndex > 0) {
focusedIndex = focusedIndex - 1;
} else {
focusedIndex = options.length - 1;
}
} else if (dir === 'start') {
focusedIndex = 0;
} else if (dir === 'end') {
focusedIndex = options.length - 1;
} else if (dir === 'page_up') {
var potentialIndex = focusedIndex - this.props.pageSize;
if (potentialIndex < 0) {
focusedIndex = 0;
} else {
focusedIndex = potentialIndex;
}
} else if (dir === 'page_down') {
var potentialIndex = focusedIndex + this.props.pageSize;
if (potentialIndex > options.length - 1) {
focusedIndex = options.length - 1;
} else {
focusedIndex = potentialIndex;
}
}
if (focusedIndex === -1) {
focusedIndex = 0;
}
this.setState({
focusedIndex: options[focusedIndex].index,
focusedOption: options[focusedIndex].option
});
},
getFocusedOption: function getFocusedOption() {
return this._focusedOption;
},
getInputValue: function getInputValue() {
return this.state.inputValue;
},
selectFocusedOption: function selectFocusedOption() {
if (this._focusedOption) {
return this.selectValue(this._focusedOption);
}
},
renderLoading: function renderLoading() {
if (!this.props.isLoading) return;
return _react2['default'].createElement(
'span',
{ className: 'Select-loading-zone', 'aria-hidden': 'true' },
_react2['default'].createElement('span', { className: 'Select-loading' })
);
},
renderValue: function renderValue(valueArray, isOpen) {
var _this7 = this;
var renderLabel = this.props.valueRenderer || this.getOptionLabel;
var ValueComponent = this.props.valueComponent;
if (!valueArray.length) {
return !this.state.inputValue ? _react2['default'].createElement(
'div',
{ className: 'Select-placeholder' },
this.props.placeholder
) : null;
}
var onClick = this.props.onValueClick ? this.handleValueClick : null;
if (this.props.multi) {
return valueArray.map(function (value, i) {
return _react2['default'].createElement(
ValueComponent,
{
id: _this7._instancePrefix + '-value-' + i,
instancePrefix: _this7._instancePrefix,
disabled: _this7.props.disabled || value.clearableValue === false,
key: 'value-' + i + '-' + value[_this7.props.valueKey],
onClick: onClick,
onRemove: _this7.removeValue,
value: value
},
renderLabel(value, i),
_react2['default'].createElement(
'span',
{ className: 'Select-aria-only' },
' '
)
);
});
} else if (!this.state.inputValue) {
if (isOpen) onClick = null;
return _react2['default'].createElement(
ValueComponent,
{
id: this._instancePrefix + '-value-item',
disabled: this.props.disabled,
instancePrefix: this._instancePrefix,
onClick: onClick,
value: valueArray[0]
},
renderLabel(valueArray[0])
);
}
},
renderInput: function renderInput(valueArray, focusedOptionIndex) {
var _classNames,
_this8 = this;
var className = (0, _classnames2['default'])('Select-input', this.props.inputProps.className);
var isOpen = !!this.state.isOpen;
var ariaOwns = (0, _classnames2['default'])((_classNames = {}, _defineProperty(_classNames, this._instancePrefix + '-list', isOpen), _defineProperty(_classNames, this._instancePrefix + '-backspace-remove-message', this.props.multi && !this.props.disabled && this.state.isFocused && !this.state.inputValue), _classNames));
// TODO: Check how this project includes Object.assign()
var inputProps = _extends({}, this.props.inputProps, {
role: 'combobox',
'aria-expanded': '' + isOpen,
'aria-owns': ariaOwns,
'aria-haspopup': '' + isOpen,
'aria-activedescendant': isOpen ? this._instancePrefix + '-option-' + focusedOptionIndex : this._instancePrefix + '-value',
'aria-describedby': this.props['aria-describedby'],
'aria-labelledby': this.props['aria-labelledby'],
'aria-label': this.props['aria-label'],
className: className,
tabIndex: this.props.tabIndex,
onBlur: this.handleInputBlur,
onChange: this.handleInputChange,
onFocus: this.handleInputFocus,
ref: function ref(_ref2) {
return _this8.input = _ref2;
},
required: this.state.required,
value: this.state.inputValue
});
if (this.props.inputRenderer) {
return this.props.inputRenderer(inputProps);
}
if (this.props.disabled || !this.props.searchable) {
var _props$inputProps = this.props.inputProps;
var inputClassName = _props$inputProps.inputClassName;
var divProps = _objectWithoutProperties(_props$inputProps, ['inputClassName']);
return _react2['default'].createElement('div', _extends({}, divProps, {
role: 'combobox',
'aria-expanded': isOpen,
'aria-owns': isOpen ? this._instancePrefix + '-list' : this._instancePrefix + '-value',
'aria-activedescendant': isOpen ? this._instancePrefix + '-option-' + focusedOptionIndex : this._instancePrefix + '-value',
className: className,
tabIndex: this.props.tabIndex || 0,
onBlur: this.handleInputBlur,
onFocus: this.handleInputFocus,
ref: function (ref) {
return _this8.input = ref;
},
'aria-readonly': '' + !!this.props.disabled,
style: { border: 0, width: 1, display: 'inline-block' } }));
}
if (this.props.autosize) {
return _react2['default'].createElement(_reactInputAutosize2['default'], _extends({}, inputProps, { minWidth: '5' }));
}
return _react2['default'].createElement(
'div',
{ className: className },
_react2['default'].createElement('input', inputProps)
);
},
renderClear: function renderClear() {
if (!this.props.clearable || !this.props.value || this.props.value === 0 || this.props.multi && !this.props.value.length || this.props.disabled || this.props.isLoading) return;
var clear = this.props.clearRenderer();
return _react2['default'].createElement(
'span',
{ className: 'Select-clear-zone', title: this.props.multi ? this.props.clearAllText : this.props.clearValueText,
'aria-label': this.props.multi ? this.props.clearAllText : this.props.clearValueText,
onMouseDown: this.clearValue,
onTouchStart: this.handleTouchStart,
onTouchMove: this.handleTouchMove,
onTouchEnd: this.handleTouchEndClearValue
},
clear
);
},
renderArrow: function renderArrow() {
var onMouseDown = this.handleMouseDownOnArrow;
var isOpen = this.state.isOpen;
var arrow = this.props.arrowRenderer({ onMouseDown: onMouseDown, isOpen: isOpen });
return _react2['default'].createElement(
'span',
{
className: 'Select-arrow-zone',
onMouseDown: onMouseDown
},
arrow
);
},
filterFlatOptions: function filterFlatOptions(excludeOptions) {
var filterValue = this.state.inputValue;
var flatOptions = this._flatOptions;
if (this.props.filterOptions) {
// Maintain backwards compatibility with boolean attribute
var filterOptions = typeof this.props.filterOptions === 'function' ? this.props.filterOptions : _utilsDefaultFilterOptions2['default'];
return filterOptions(flatOptions, filterValue, excludeOptions, {
filterOption: this.props.filterOption,
ignoreAccents: this.props.ignoreAccents,
ignoreCase: this.props.ignoreCase,
labelKey: this.props.labelKey,
matchPos: this.props.matchPos,
matchProp: this.props.matchProp,
valueKey: this.props.valueKey
});
} else {
return flatOptions;
}
},
flattenOptions: function flattenOptions(options, group) {
if (!options) return [];
var flatOptions = [];
for (var i = 0; i < options.length; i++) {
// We clone each option with a pointer to its parent group for efficient unflattening
var optionCopy = clone(options[i]);
optionCopy.isInTree = false;
if (group) {
optionCopy.group = group;
}
if (isGroup(optionCopy)) {
flatOptions = flatOptions.concat(this.flattenOptions(optionCopy.options, optionCopy));
optionCopy.options = [];
} else {
flatOptions.push(optionCopy);
}
}
return flatOptions;
},
unflattenOptions: function unflattenOptions(flatOptions) {
var groupedOptions = [];
var parent = undefined,
child = undefined;
// Remove all ancestor groups from the tree
flatOptions.forEach(function (option) {
option.isInTree = false;
parent = option.group;
while (parent) {
if (parent.isInTree) {
parent.options = [];
parent.isInTree = false;
}
parent = parent.group;
}
});
// Now reconstruct the options tree
flatOptions.forEach(function (option) {
child = option;
parent = child.group;
while (parent) {
if (!child.isInTree) {
parent.options.push(child);
child.isInTree = true;
}
child = parent;
parent = child.group;
}
if (!child.isInTree) {
groupedOptions.push(child);
child.isInTree = true;
}
});
return groupedOptions;
},
onOptionRef: function onOptionRef(ref, isFocused) {
if (isFocused) {
this.focused = ref;
}
},
renderMenu: function renderMenu(options, valueArray, focusedOption) {
if (options && options.length) {
return this.props.menuRenderer({
focusedOption: focusedOption,
focusOption: this.focusOption,
instancePrefix: this._instancePrefix,
labelKey: this.props.labelKey,
onFocus: this.focusOption,
onOptionRef: this.onOptionRef,
onSelect: this.selectValue,
optionClassName: this.props.optionClassName,
optionComponent: this.props.optionComponent,
optionGroupComponent: this.props.optionGroupComponent,
optionRenderer: this.props.optionRenderer || this.getOptionLabel,
options: options,
selectValue: this.selectValue,
valueArray: valueArray,
valueKey: this.props.valueKey
});
} else if (this.props.noResultsText) {
return _react2['default'].createElement(
'div',
{ className: 'Select-noresults' },
this.props.noResultsText
);
} else {
return null;
}
},
renderHiddenField: function renderHiddenField(valueArray) {
var _this9 = this;
if (!this.props.name) return;
if (this.props.joinValues) {
var value = valueArray.map(function (i) {
return stringifyValue(i[_this9.props.valueKey]);
}).join(this.props.delimiter);
return _react2['default'].createElement('input', {
type: 'hidden',
ref: function (ref) {
return _this9.value = ref;
},
name: this.props.name,
value: value,
disabled: this.props.disabled });
}
return valueArray.map(function (item, index) {
return _react2['default'].createElement('input', { key: 'hidden.' + index,
type: 'hidden',
ref: 'value' + index,
name: _this9.props.name,
value: stringifyValue(item[_this9.props.valueKey]),
disabled: _this9.props.disabled });
});
},
getFocusableOptionIndex: function getFocusableOptionIndex(selectedOption) {
var options = this._visibleOptions;
if (!options.length) return null;
var focusedOption = this.state.focusedOption || selectedOption;
if (focusedOption && !focusedOption.disabled) {
var focusedOptionIndex = -1;
options.some(function (option, index) {
var isOptionEqual = option.value === focusedOption.value;
if (isOptionEqual) {
focusedOptionIndex = index;
}
return isOptionEqual;
});
if (focusedOptionIndex !== -1) {
return focusedOptionIndex;
}
}
for (var i = 0; i < options.length; i++) {
if (!options[i].disabled) return i;
}
return null;
},
renderOuter: function renderOuter(options, valueArray, focusedOption) {
var _this10 = this;
var Dropdown = this.props.dropdownComponent;
var menu = this.renderMenu(options, valueArray, focusedOption);
if (!menu) {
return null;
}
return _react2['default'].createElement(
Dropdown,
null,
_react2['default'].createElement(
'div',
{ ref: function (ref) {
return _this10.menuContainer = ref;
}, className: 'Select-menu-outer', style: this.props.menuContainerStyle },
_react2['default'].createElement(
'div',
{ ref: function (ref) {
return _this10.menu = ref;
}, role: 'listbox', className: 'Select-menu', id: this._instancePrefix + '-list',
style: this.props.menuStyle,
onScroll: this.handleMenuScroll,
onMouseDown: this.handleMouseDownOnMenu },
menu
)
)
);
},
render: function render() {
var _this11 = this;
var valueArray = this.getValueArray(this.props.value);
this._visibleOptions = this.filterFlatOptions(this.props.multi ? valueArray : null);
var options = this.unflattenOptions(this._visibleOptions);
var isOpen = typeof this.props.isOpen === 'boolean' ? this.props.isOpen : this.state.isOpen;
var focusedOptionIndex = this.getFocusableOptionIndex(valueArray[0]);
var focusedOption = null;
if (focusedOptionIndex !== null) {
focusedOption = this._focusedOption = this._visibleOptions[focusedOptionIndex];
} else {
focusedOption = this._focusedOption = null;
}
var className = (0, _classnames2['default'])('Select', this.props.className, {
'Select--multi': this.props.multi,
'Select--single': !this.props.multi,
'is-disabled': this.props.disabled,
'is-focused': this.state.isFocused,
'is-loading': this.props.isLoading,
'is-open': isOpen,
'is-pseudo-focused': this.state.isPseudoFocused,
'is-searchable': this.props.searchable,
'has-value': valueArray.length
});
var removeMessage = null;
if (this.props.multi && !this.props.disabled && valueArray.length && !this.state.inputValue && this.state.isFocused && this.props.backspaceRemoves) {
removeMessage = _react2['default'].createElement(
'span',
{ id: this._instancePrefix + '-backspace-remove-message', className: 'Select-aria-only', 'aria-live': 'assertive' },
this.props.backspaceToRemoveMessage.replace('{label}', valueArray[valueArray.length - 1][this.props.labelKey])
);
}
return _react2['default'].createElement(
'div',
{ ref: function (ref) {
return _this11.wrapper = ref;
},
className: className,
style: this.props.wrapperStyle },
this.renderHiddenField(valueArray),
_react2['default'].createElement(
'div',
{ ref: function (ref) {
return _this11.control = ref;
},
className: 'Select-control',
style: this.props.style,
onKeyDown: this.handleKeyDown,
onMouseDown: this.handleMouseDown,
onTouchEnd: this.handleTouchEnd,
onTouchStart: this.handleTouchStart,
onTouchMove: this.handleTouchMove
},
_react2['default'].createElement(
'span',
{ className: 'Select-multi-value-wrapper', id: this._instancePrefix + '-value' },
this.renderValue(valueArray, isOpen),
this.renderInput(valueArray, focusedOptionIndex)
),
removeMessage,
this.renderLoading(),
this.renderClear(),
this.renderArrow()
),
isOpen ? this.renderOuter(options, !this.props.multi ? valueArray : null, focusedOption) : null
);
}
});
Select.stripDiacritics = _utilsStripDiacritics2['default'];
exports['default'] = Select;
module.exports = exports['default'];