choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
199 lines (176 loc) • 6.04 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _typeof from "@babel/runtime/helpers/typeof";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import React, { Component } from 'react';
import classNames from 'classnames';
import omit from 'lodash/omit';
import { icons } from 'choerodon-ui-font';
import Icon from '../icon';
import Select from '../select';
import Pagination from '../pagination';
import Tooltip from '../tooltip';
import ConfigContext from '../config-provider/ConfigContext';
var Option = Select.Option;
var IconSelect = /*#__PURE__*/function (_Component) {
_inherits(IconSelect, _Component);
var _super = _createSuper(IconSelect);
function IconSelect(props, context) {
var _this;
_classCallCheck(this, IconSelect);
_this = _super.call(this, props, context);
_this.handleRender = function (label) {
if (typeof label === 'string' && label) {
return /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement(Icon, {
type: label
}), " ", label);
}
if (_typeof(label) === 'object' && label.props) {
var children = label.props.children;
return children ? /*#__PURE__*/React.createElement("span", null, children) : null;
}
return null;
};
_this.handlePageChange = function (current, pageSize) {
var filterValue = _this.state.filterValue;
_this.initIcon(current, pageSize, filterValue);
};
_this.handleFilter = function (value) {
_this.initIcon(1, 20, value);
};
_this.saveRef = function (node) {
if (node) {
_this.rcSelect = node;
}
};
_this.state = {
current: 1,
total: 0,
pageSize: 20,
filterValue: '',
data: []
};
return _this;
}
_createClass(IconSelect, [{
key: "componentDidMount",
value: function componentDidMount() {
this.initIcon();
}
}, {
key: "initIcon",
value: function initIcon() {
var current = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var pageSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 20;
var filterValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
var showAll = this.props.showAll;
var minIndex = (current - 1) * pageSize;
var maxIndex = current * pageSize;
var items;
if (showAll) {
items = icons["default"];
if (filterValue) {
items = icons.favorite.filter(function (name) {
return name.toLowerCase().indexOf(filterValue.toLowerCase()) !== -1;
});
}
} else {
items = icons.favorite;
if (filterValue) {
items = icons.favorite.filter(function (name) {
return name.toLowerCase().indexOf(filterValue.toLowerCase()) !== -1;
});
}
}
var total = items.length || 0;
var currentData = items.filter(function (name, index) {
return name && index >= minIndex && index < maxIndex;
});
this.setState({
data: currentData,
total: total,
pageSize: pageSize,
current: current,
filterValue: filterValue
});
}
}, {
key: "renderOption",
value: function renderOption() {
var data = this.state.data;
return data.map(function (icon) {
return /*#__PURE__*/React.createElement(Option, {
key: icon,
value: icon
}, /*#__PURE__*/React.createElement(Tooltip, {
placement: "bottomLeft",
title: icon
}, /*#__PURE__*/React.createElement(Icon, {
type: icon
}), /*#__PURE__*/React.createElement("span", {
className: "text"
}, icon)));
});
}
}, {
key: "renderFooter",
value: function renderFooter() {
var _this$state = this.state,
total = _this$state.total,
pageSize = _this$state.pageSize,
current = _this$state.current;
return /*#__PURE__*/React.createElement(Pagination, {
total: total,
onChange: this.handlePageChange,
pageSizeOptions: ['20', '40', '80'],
pageSize: pageSize,
onShowSizeChange: this.handlePageChange,
current: current
});
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
className = _this$props.className,
customizePrefixCls = _this$props.prefixCls,
dropdownClassName = _this$props.dropdownClassName;
var filterValue = this.state.filterValue;
var getPrefixCls = this.context.getPrefixCls;
var prefixCls = getPrefixCls('icon-select', customizePrefixCls);
var selectCls = classNames(className, prefixCls);
var dropDownCls = classNames(dropdownClassName, "".concat(prefixCls, "-dropdown"));
var selectProps = _objectSpread(_objectSpread({}, this.props), {}, {
className: selectCls,
dropdownClassName: dropDownCls
});
var otherProps = omit(selectProps, ['prefixCls']);
return /*#__PURE__*/React.createElement(Select, _extends({}, otherProps, {
footer: this.renderFooter(),
onFilterChange: this.handleFilter,
filterValue: filterValue,
choiceRender: this.handleRender,
filter: true,
ref: this.saveRef
}), this.renderOption());
}
}], [{
key: "contextType",
get: function get() {
return ConfigContext;
}
}]);
return IconSelect;
}(Component);
export { IconSelect as default };
IconSelect.displayName = 'IconSelect';
IconSelect.defaultProps = {
filter: true,
showArrow: false,
showCheckAll: false,
showAll: false
};
//# sourceMappingURL=IconSelect.js.map