react-select-material-ui
Version:
A react SELECT component based on react-select and looking like a material-ui component
194 lines (193 loc) • 9.98 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var FormControl_1 = require("@material-ui/core/FormControl/FormControl");
var SelectDropdown_1 = require("./SelectDropdown");
var SelectHelperText_1 = require("./SelectHelperText");
var SelectLabel_1 = require("./SelectLabel");
var lodash_1 = require("lodash");
var ReactSelectMaterialUi = (function (_super) {
__extends(ReactSelectMaterialUi, _super);
function ReactSelectMaterialUi(props) {
var _this = _super.call(this, props) || this;
_this.getFinalValue = function (value, values, defaultValue, defaultValues) {
return values || value || defaultValues || defaultValue;
};
_this.getSelectedOption = function (options, value) {
return lodash_1.isNil(value) ? undefined : _this.getOneOrMoreSelectOptions(options, value);
};
_this.getOptionForValue = function (options) { return function (value) {
var option = lodash_1.find(options, _this.matchOptionValue(value));
if (lodash_1.isNil(option)) {
var subOptions = lodash_1.filter(options, _this.hasSubOptions);
if (lodash_1.isEmpty(subOptions)) {
return;
}
return _this.getOptionForValue(lodash_1.flatMap(subOptions, _this.getSubOption))(value);
}
return _this.getSelectOption(option);
}; };
_this.matchOptionValue = function (value) { return function (option) {
if (lodash_1.isString(option)) {
return value === option;
}
return lodash_1.isEqual(value, option.value);
}; };
_this.hasSubOptions = function (option) {
return lodash_1.isString(option) === false && lodash_1.isArray(option.options);
};
_this.getSubOption = function (option) { return option.options; };
_this.handleChangeSelect = function (newValue) {
var _a = _this.props, onChange = _a.onChange, value = _a.value, values = _a.values;
if (lodash_1.isEmpty(value) && lodash_1.isEmpty(values)) {
_this.setState({
filter: '',
selectedOption: newValue
});
}
if (lodash_1.isFunction(onChange)) {
onChange(_this.getValues(newValue), newValue === null ? undefined : newValue);
}
};
_this.handleGotFocus = function (event) {
_this.setState({
hasInputFocus: true
});
var onFocus = _this.props.onFocus;
if (lodash_1.isFunction(onFocus)) {
onFocus(event);
}
};
_this.handleLostFocus = function (event) {
_this.setState({
hasInputFocus: false
});
var onBlur = _this.props.onBlur;
if (lodash_1.isFunction(onBlur)) {
onBlur(event);
}
};
var defaultValue = props.defaultValue, defaultValues = props.defaultValues, value = props.value, values = props.values;
var finalValue = _this.getFinalValue(value, values, defaultValue, defaultValues);
_this.state = {
filter: '',
hasInputFocus: false,
selectedOption: _this.getSelectedOption(props.options, finalValue)
};
return _this;
}
ReactSelectMaterialUi.prototype.getOneOrMoreSelectOptions = function (options, value) {
if (lodash_1.isArray(value)) {
return lodash_1.reject(lodash_1.map(value, this.getOptionForValue(options)), lodash_1.isNil);
}
return this.getOptionForValue(options)(value);
};
ReactSelectMaterialUi.prototype.getOptions = function (options) {
return lodash_1.map(options, this.getSelectOption);
};
ReactSelectMaterialUi.prototype.getSelectOption = function (option) {
if (lodash_1.isString(option)) {
return {
label: option,
value: option
};
}
return option;
};
ReactSelectMaterialUi.prototype.render = function () {
var _a = this.props, autoComplete = _a.autoComplete, autoFocus = _a.autoFocus, children = _a.children, className = _a.className, defaultValue = _a.defaultValue, defaultValues = _a.defaultValues, disabled = _a.disabled, error = _a.error, FormHelperTextProps = _a.FormHelperTextProps, fullWidth = _a.fullWidth, helperText = _a.helperText, id = _a.id, InputLabelProps = _a.InputLabelProps, inputRef = _a.inputRef, label = _a.label, multiline = _a.multiline, name = _a.name, onBlur = _a.onBlur, onChange = _a.onChange, onFocus = _a.onFocus, placeholder = _a.placeholder, required = _a.required, rows = _a.rows, rowsMax = _a.rowsMax, select = _a.select, SelectProps = _a.SelectProps, type = _a.type, value = _a.value, values = _a.values, options = _a.options, variant = _a.variant, other = __rest(_a, ["autoComplete", "autoFocus", "children", "className", "defaultValue", "defaultValues", "disabled", "error", "FormHelperTextProps", "fullWidth", "helperText", "id", "InputLabelProps", "inputRef", "label", "multiline", "name", "onBlur", "onChange", "onFocus", "placeholder", "required", "rows", "rowsMax", "select", "SelectProps", "type", "value", "values", "options", "variant"]);
var helperTextId = id && helperText ? id + "-helper-text" : undefined;
var _b = this.state, hasInputFocus = _b.hasInputFocus, selectedOption = _b.selectedOption;
var dropdownOption = selectedOption;
if (value === null || values === null) {
dropdownOption = null;
}
else if (value || values) {
var finalValue = this.getFinalValue(value, values);
dropdownOption = this.getSelectedOption(options, finalValue);
}
var isClearable = !!SelectProps && SelectProps.isClearable === true && this.isClearable(dropdownOption);
var isDisabled = disabled || (!!SelectProps && SelectProps.isDisabled);
var selectPlaceholder = label ? '' : placeholder;
var shrink = this.isShrinked(dropdownOption);
return (React.createElement(FormControl_1.default, __assign({ "aria-describedby": helperTextId, className: className, error: error, fullWidth: fullWidth, required: required }, other),
React.createElement(SelectLabel_1.default, { id: id, label: label, shrink: shrink, hasInputFocus: hasInputFocus, inputLabelProps: InputLabelProps }),
React.createElement(SelectDropdown_1.default, { value: dropdownOption, placeholder: selectPlaceholder, options: this.getOptions(options), selectProps: __assign(__assign({}, SelectProps), { isClearable: isClearable,
isDisabled: isDisabled }), hasInputFocus: hasInputFocus, onChange: this.handleChangeSelect, onFocus: this.handleGotFocus, onBlur: this.handleLostFocus }),
React.createElement(SelectHelperText_1.default, { id: helperTextId, helperText: helperText, formHelperTextProps: FormHelperTextProps })));
};
ReactSelectMaterialUi.prototype.isShrinked = function (selectedOption) {
if (this.hasInputFocus() || this.hasFilter()) {
return true;
}
return lodash_1.isEmpty(selectedOption) === false;
};
ReactSelectMaterialUi.prototype.isClearable = function (dropdownOption) {
var disabled = this.props.disabled;
if (disabled) {
return false;
}
if (lodash_1.isEmpty(dropdownOption)) {
return false;
}
if (lodash_1.isArray(dropdownOption) && lodash_1.size(dropdownOption) < 2) {
return false;
}
return true;
};
ReactSelectMaterialUi.prototype.hasInputFocus = function () {
return this.state.hasInputFocus === true;
};
ReactSelectMaterialUi.prototype.hasFilter = function () {
return lodash_1.isEmpty(this.state.filter) === false;
};
ReactSelectMaterialUi.prototype.getValues = function (value) {
if (lodash_1.isNil(value)) {
return null;
}
if (lodash_1.isArray(value)) {
return lodash_1.map(value, this.getValue);
}
return this.getValue(value);
};
ReactSelectMaterialUi.prototype.getValue = function (option) {
return option.value;
};
return ReactSelectMaterialUi;
}(React.PureComponent));
exports.default = ReactSelectMaterialUi;