@txdfe/at
Version:
一个设计体系组件库
607 lines (489 loc) • 22.4 kB
JavaScript
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _input = _interopRequireDefault(require("../input"));
var _select = _interopRequireDefault(require("../select"));
var _button = _interopRequireDefault(require("../button"));
var _icon = _interopRequireDefault(require("../icon"));
var _util = require("../util");
var _zhCn = _interopRequireDefault(require("../locale/zh-cn"));
var _this2 = void 0;
var _excluded = ["shape", "autoFocus", "filter", "hasIcon", "disabled", "placeholder", "type", "className", "style", "prefix", "searchText", "dataSource", "filterProps", "buttonProps", "popupContent", "followTrigger", "hasClear", "visible", "locale", "rtl"],
_excluded2 = ["onInputFocus", "overlayVisible", "combox"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _extends() { _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; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 Group = _input["default"].Group;
var AutoComplete = _select["default"].AutoComplete;
/**
* Search
* @description 输入框部分继承 Select.AutoComplete 的能力,可以直接用AutoComplete 的 api
*/
var Search = /*#__PURE__*/function (_React$Component) {
_inherits(Search, _React$Component);
var _super = _createSuper(Search);
function Search(props) {
var _this;
_classCallCheck(this, Search);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "inputRef", /*#__PURE__*/_react["default"].createRef());
_defineProperty(_assertThisInitialized(_this), "autoCompleteRef", /*#__PURE__*/_react["default"].createRef());
_defineProperty(_assertThisInitialized(_this), "onChange", function (value) {
var _this$props;
if (!('value' in _this.props)) {
_this.setState({
value: value
});
}
for (var _len = arguments.length, argv = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
argv[_key - 1] = arguments[_key];
}
(_this$props = _this.props).onChange.apply(_this$props, [value].concat(argv));
});
_defineProperty(_assertThisInitialized(_this), "onSearch", function () {
if (_this.props.disabled) {
return;
}
_this.props.onSearch(_this.state.value, _this.state.filterValue);
});
_defineProperty(_assertThisInitialized(_this), "onFilterChange", function (filterValue) {
if (!('filterValue' in _this.props)) {
_this.setState({
filterValue: filterValue
});
}
_this.props.onFilterChange(filterValue);
});
_defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
if (_this.props.disabled) {
return;
}
if (e.keyCode !== _util.KEYCODE.ENTER) {
return;
}
_this.onSearch();
});
_defineProperty(_assertThisInitialized(_this), "focus", function () {
if (_this.props.disabled) {
return;
}
_this.inputRef.current && _this.inputRef.current.focus();
if (_this.autoCompleteRef.current && _this.autoCompleteRef.current.getInstance) {
var ins = _this.autoCompleteRef.current.getInstance();
ins && ins.focusInput && ins.focusInput();
}
_this.setState({
searchFocus: true
});
});
_defineProperty(_assertThisInitialized(_this), "onClickMask", function () {
_this.focus();
});
_defineProperty(_assertThisInitialized(_this), "onInputFocus", function () {
_this.setState({
searchFocus: true
});
if (typeof _this.props.onFocus === 'function') {
_this.props.onFocus();
}
});
_defineProperty(_assertThisInitialized(_this), "onInputBlur", function () {
_this.setState({
searchFocus: false
});
if (typeof _this.props.onBlur === 'function') {
_this.props.onBlur();
}
});
_defineProperty(_assertThisInitialized(_this), "renderAddonAfter", function () {
var _this$props2 = _this.props,
prefix = _this$props2.prefix,
type = _this$props2.type,
addonAfter = _this$props2.addonAfter;
if (type !== 'normal' || !addonAfter) {
return null;
}
return /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefix, "search-addon-after")
}, addonAfter);
});
var _value = 'value' in props ? props.value : props.defaultValue;
var _filterValue = 'filterValue' in props ? props.filterValue : props.defaultFilterValue;
_this.state = {
value: typeof _value === 'undefined' ? '' : _value,
filterValue: _filterValue,
searchFocus: props.autoFocus && !props.disabled ? true : -1 // -1 or boolean
};
return _this;
}
_createClass(Search, [{
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
var state = {};
if ('value' in nextProps) {
state.value = typeof nextProps.value === 'undefined' ? '' : nextProps.value;
}
if ('filterValue' in nextProps) {
state.filterValue = typeof nextProps.filterValue === 'undefined' ? '' : nextProps.filterValue;
}
this.setState(state);
}
}, {
key: "renderInputMask",
value: function renderInputMask() {
var _this$props3 = this.props,
prefix = _this$props3.prefix,
disabled = _this$props3.disabled,
placeholder = _this$props3.placeholder;
return /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefix, "search-mask"),
onClick: this.onClickMask
}, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
type: "search",
"aria-disabled": disabled
}), /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefix, "search-mask-text")
}, placeholder));
}
}, {
key: "renderInput",
value: function renderInput() {
var _this$props4 = this.props,
hasClear = _this$props4.hasClear,
disabled = _this$props4.disabled,
placeholder = _this$props4.placeholder,
autoFocus = _this$props4.autoFocus;
return /*#__PURE__*/_react["default"].createElement(_input["default"], {
ref: this.inputRef,
autoFocus: autoFocus,
hasClear: hasClear,
disabled: disabled,
placeholder: this.state.searchFocus === true ? placeholder : undefined,
value: this.state.value,
onChange: this.onChange,
onFocus: this.onInputFocus,
onBlur: this.onInputBlur
});
}
}, {
key: "renderSearchButton",
value: function renderSearchButton() {
var _classNames;
var _this$props5 = this.props,
buttonProps = _this$props5.buttonProps,
disabled = _this$props5.disabled,
locale = _this$props5.locale,
hasIcon = _this$props5.hasIcon,
prefix = _this$props5.prefix,
searchText = _this$props5.searchText;
var buttonCls = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "search-btn"), true), _defineProperty(_classNames, buttonProps.className, !!buttonProps.className), _classNames));
return /*#__PURE__*/_react["default"].createElement(_button["default"], _extends({
tabIndex: "0",
"aria-disabled": disabled,
"aria-label": locale.buttonText,
className: buttonCls,
disabled: disabled
}, buttonProps, {
onClick: this.onSearch,
onKeyDown: this.onKeyDown
}), hasIcon ? /*#__PURE__*/_react["default"].createElement(_icon["default"], {
type: "search"
}) : null, searchText ? /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefix, "search-btn-text")
}, searchText) : null);
}
}, {
key: "renderSearchIcon",
value: function renderSearchIcon() {
var _classNames2;
var _this$props6 = this.props,
buttonProps = _this$props6.buttonProps,
disabled = _this$props6.disabled,
locale = _this$props6.locale,
hasIcon = _this$props6.hasIcon,
prefix = _this$props6.prefix;
var iconCls = (0, _classnames["default"])((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefix, "search-icon"), true), _defineProperty(_classNames2, buttonProps.className, !!buttonProps.className), _classNames2));
return hasIcon && /*#__PURE__*/_react["default"].createElement(_icon["default"], _extends({
type: "search",
tabIndex: "0",
role: "button",
"aria-disabled": disabled,
"aria-label": locale.buttonText
}, buttonProps, {
className: iconCls,
onClick: this.onSearch,
onKeyDown: this.onKeyDown
}));
}
}, {
key: "render",
value: function render() {
var _classNames3;
var _this$props7 = this.props,
shape = _this$props7.shape,
autoFocus = _this$props7.autoFocus,
filter = _this$props7.filter,
hasIcon = _this$props7.hasIcon,
disabled = _this$props7.disabled,
placeholder = _this$props7.placeholder,
type = _this$props7.type,
className = _this$props7.className,
style = _this$props7.style,
prefix = _this$props7.prefix,
searchText = _this$props7.searchText,
dataSource = _this$props7.dataSource,
filterProps = _this$props7.filterProps,
buttonProps = _this$props7.buttonProps,
popupContent = _this$props7.popupContent,
followTrigger = _this$props7.followTrigger,
hasClear = _this$props7.hasClear,
visible = _this$props7.visible,
locale = _this$props7.locale,
rtl = _this$props7.rtl,
others = _objectWithoutProperties(_this$props7, _excluded);
var cls = (0, _classnames["default"])((_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefix, "search"), true), _defineProperty(_classNames3, "".concat(prefix, "search-disabled"), disabled), _defineProperty(_classNames3, "".concat(prefix, "search-").concat(shape), true), _defineProperty(_classNames3, "".concat(prefix).concat(type), type), _defineProperty(_classNames3, "".concat(prefix, "medium"), true), _defineProperty(_classNames3, "".concat(prefix, "search-focus"), this.state.searchFocus === true), _defineProperty(_classNames3, className, !!className), _classNames3));
var searchIcon = null;
var searchBtn = null;
var filterSelect = null;
if (shape === 'simple') {
searchIcon = this.renderSearchIcon();
} else {
searchBtn = this.renderSearchButton();
}
if (filter.length > 0) {
filterSelect = /*#__PURE__*/_react["default"].createElement(_select["default"], _extends({}, filterProps, {
followTrigger: followTrigger,
hasBorder: false,
dataSource: filter,
disabled: disabled,
value: this.state.filterValue,
onChange: this.onFilterChange
}));
}
var othersAttributes = _util.obj.pickOthers(Search.propTypes, others);
if (visible !== undefined) {
// 受控属性 visible 不能直接写在组件上
othersAttributes.visible = Boolean(visible);
}
var dataAttr = _util.obj.pickAttrsWith(others, 'data-');
var left = /*#__PURE__*/_react["default"].createElement(Group, {
addonBefore: filterSelect,
className: "".concat(prefix, "search-left"),
addonBeforeClassName: "".concat(prefix, "search-left-addon"),
addonAfter: this.renderAddonAfter()
}, /*#__PURE__*/_react["default"].createElement(AutoComplete, _extends({
"aria-label": locale.buttonText,
showEmpty: false
}, othersAttributes, {
autoFocus: autoFocus,
followTrigger: followTrigger,
role: "searchbox",
hasClear: hasClear,
className: "".concat(prefix, "search-input"),
placeholder: placeholder,
dataSource: dataSource,
innerBefore: searchIcon,
onPressEnter: this.onSearch,
value: this.state.value,
onChange: this.onChange,
onFocus: this.onInputFocus,
onBlur: this.onInputBlur,
popupContent: popupContent,
disabled: disabled,
ref: this.autoCompleteRef
})));
if (type === 'nav-search') {
var _classNames4;
var wrapCls = (0, _classnames["default"])((_classNames4 = {}, _defineProperty(_classNames4, "".concat(prefix, "search"), true), _defineProperty(_classNames4, "".concat(prefix).concat(type), type), _defineProperty(_classNames4, "".concat(prefix, "search-disabled"), disabled), _defineProperty(_classNames4, "".concat(prefix, "search-init"), this.state.searchFocus === -1), _defineProperty(_classNames4, "".concat(prefix, "search-focus"), this.state.searchFocus === true), _defineProperty(_classNames4, "".concat(prefix, "search-has-query"), !!this.state.value), _defineProperty(_classNames4, className, !!className), _classNames4));
return /*#__PURE__*/_react["default"].createElement("span", _extends({
className: wrapCls,
style: style,
dir: rtl ? 'rtl' : undefined
}, dataAttr), this.renderInputMask(), this.renderInput());
}
return /*#__PURE__*/_react["default"].createElement("span", _extends({
className: cls,
style: style
}, dataAttr, {
dir: rtl ? 'rtl' : undefined
}), searchBtn ? /*#__PURE__*/_react["default"].createElement(Group, {
addonAfter: searchBtn
}, left) : left);
}
}]);
return Search;
}(_react["default"].Component);
_defineProperty(Search, "displayName", 'Search');
_defineProperty(Search, "propTypes", {
/**
* 样式前缀
*/
prefix: _propTypes["default"].string,
/**
* 样式前缀
*/
autoFocus: _propTypes["default"].bool,
/**
* 形状
*/
shape: _propTypes["default"].oneOf(['normal', 'simple']),
/**
* 类型 shape=normal: primary/secondary; shape=simple: normal/dark/nav-search;
*/
type: _propTypes["default"].oneOf(['primary', 'secondary', 'normal', 'dark', 'nav-search']),
/**
* 后缀,仅在type为normal时有效
*/
addonAfter: _propTypes["default"].node,
/**
* 搜索框默认值
*/
defaultValue: _propTypes["default"].string,
/**
* 搜索框数值
*/
value: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),
/**
* 输入关键字时的回掉
* @param {Object} value 输入值
*/
onChange: _propTypes["default"].func,
/**
* 点击搜索按钮触发的回调
* @param {Object} value 输入值
*/
onSearch: _propTypes["default"].func,
/**
* 选择器默认值
*/
defaultFilterValue: _propTypes["default"].string,
/**
* 选择器
*/
filter: _propTypes["default"].array,
/**
* 选择器值
*/
filterValue: _propTypes["default"].string,
/**
* 选择器发生变化时回调
* @param {Object} filter value
*/
onFilterChange: _propTypes["default"].func,
/**
* 搜索框下拉联想列表
*/
dataSource: _propTypes["default"].array,
/**
* 默认提示
*/
placeholder: _propTypes["default"].string,
/**
* button 的内容
*/
searchText: _propTypes["default"].node,
/**
* 自定义样式
*/
style: _propTypes["default"].object,
/**
* 样式名称
*/
className: _propTypes["default"].string,
/**
* 选择器的props
*/
filterProps: _propTypes["default"].object,
/**
* 按钮的额外属性
*/
buttonProps: _propTypes["default"].object,
/**
* 自定义渲染的的下拉框
*/
popupContent: _propTypes["default"].node,
/**
* 是否跟随滚动
*/
followTrigger: _propTypes["default"].bool,
/**
* 自定义渲染的的下拉框
*/
visible: _propTypes["default"].bool,
/**
* 是否显示清除按钮
*/
hasClear: _propTypes["default"].bool,
/**
* 是否显示搜索按钮
*/
hasIcon: _propTypes["default"].bool,
/**
* 是否禁用
*/
disabled: _propTypes["default"].bool,
locale: _propTypes["default"].object,
rtl: _propTypes["default"].bool,
onFocus: _propTypes["default"].func,
onBlur: _propTypes["default"].func
});
_defineProperty(Search, "defaultProps", {
prefix: 'next-',
shape: 'simple',
type: 'normal',
hasIcon: true,
filter: [],
locale: _zhCn["default"].Search,
buttonProps: {},
onChange: _util.func.noop,
onSearch: _util.func.noop,
onFilterChange: _util.func.noop,
autoFocus: false,
hasClear: true,
disabled: false
});
var _default = _configProvider["default"].config(Search, {
transfrom:
/* istanbul ignore next */
function transfrom(props, deprecated) {
var _this2$props = _this2.props,
onInputFocus = _this2$props.onInputFocus,
overlayVisible = _this2$props.overlayVisible,
combox = _this2$props.combox,
others = _objectWithoutProperties(_this2$props, _excluded2);
var newprops = others;
if (onInputFocus) {
deprecated('onInputFocus', 'onFocus', 'Search');
newprops.onFocus = onInputFocus;
}
if ('overlayVisible' in _this2.props) {
deprecated('overlayVisible', 'visible', 'Search');
newprops.visible = overlayVisible;
}
if (combox) {
deprecated('combox', 'popupContent', 'Search');
newprops.popupContent = combox;
}
return newprops;
}
});
exports["default"] = _default;