@txdfe/at
Version:
一个设计体系组件库
484 lines (482 loc) • 20.7 kB
JavaScript
"use strict";
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(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var Group = _input["default"].Group;
var AutoComplete = _select["default"].AutoComplete;
/**
* Search
* @description 输入框部分继承 Select.AutoComplete 的能力,可以直接用AutoComplete 的 api
*/
var Search = /*#__PURE__*/function (_React$Component) {
function Search(props) {
var _this;
_classCallCheck(this, Search);
_this = _callSuper(this, Search, [props]);
_defineProperty(_this, "inputRef", /*#__PURE__*/_react["default"].createRef());
_defineProperty(_this, "autoCompleteRef", /*#__PURE__*/_react["default"].createRef());
_defineProperty(_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(_this, "onSearch", function () {
if (_this.props.disabled) {
return;
}
_this.props.onSearch(_this.state.value, _this.state.filterValue);
});
_defineProperty(_this, "onFilterChange", function (filterValue) {
if (!('filterValue' in _this.props)) {
_this.setState({
filterValue: filterValue
});
}
_this.props.onFilterChange(filterValue);
});
_defineProperty(_this, "onKeyDown", function (e) {
if (_this.props.disabled) {
return;
}
if (e.keyCode !== _util.KEYCODE.ENTER) {
return;
}
_this.onSearch();
});
_defineProperty(_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(_this, "onClickMask", function () {
_this.focus();
});
_defineProperty(_this, "onInputFocus", function () {
_this.setState({
searchFocus: true
});
if (typeof _this.props.onFocus === 'function') {
_this.props.onFocus();
}
});
_defineProperty(_this, "onInputBlur", function () {
_this.setState({
searchFocus: false
});
if (typeof _this.props.onBlur === 'function') {
_this.props.onBlur();
}
});
_defineProperty(_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;
}
_inherits(Search, _React$Component);
return _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 _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"])(_defineProperty(_defineProperty({}, "".concat(prefix, "search-btn"), true), buttonProps.className, !!buttonProps.className));
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 _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"])(_defineProperty(_defineProperty({}, "".concat(prefix, "search-icon"), true), buttonProps.className, !!buttonProps.className));
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 _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"])(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "search"), true), "".concat(prefix, "search-disabled"), disabled), "".concat(prefix, "search-").concat(shape), true), "".concat(prefix).concat(type), type), "".concat(prefix, "medium"), true), "".concat(prefix, "search-focus"), this.state.searchFocus === true), className, !!className));
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 wrapCls = (0, _classnames["default"])(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "search"), true), "".concat(prefix).concat(type), type), "".concat(prefix, "search-disabled"), disabled), "".concat(prefix, "search-init"), this.state.searchFocus === -1), "".concat(prefix, "search-focus"), this.state.searchFocus === true), "".concat(prefix, "search-has-query"), !!this.state.value), className, !!className));
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);
}
}]);
}(_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 = exports["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;
}
});