155 lines
5.8 KiB
JavaScript
155 lines
5.8 KiB
JavaScript
|
"use strict";
|
||
|
"use client";
|
||
|
|
||
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
||
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = void 0;
|
||
|
var React = _interopRequireWildcard(require("react"));
|
||
|
var _SearchOutlined = _interopRequireDefault(require("@ant-design/icons/SearchOutlined"));
|
||
|
var _classnames = _interopRequireDefault(require("classnames"));
|
||
|
var _ref = require("rc-util/lib/ref");
|
||
|
var _reactNode = require("../_util/reactNode");
|
||
|
var _button = _interopRequireDefault(require("../button"));
|
||
|
var _configProvider = require("../config-provider");
|
||
|
var _useSize = _interopRequireDefault(require("../config-provider/hooks/useSize"));
|
||
|
var _Compact = require("../space/Compact");
|
||
|
var _Input = _interopRequireDefault(require("./Input"));
|
||
|
var __rest = void 0 && (void 0).__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;
|
||
|
};
|
||
|
const Search = /*#__PURE__*/React.forwardRef((props, ref) => {
|
||
|
const {
|
||
|
prefixCls: customizePrefixCls,
|
||
|
inputPrefixCls: customizeInputPrefixCls,
|
||
|
className,
|
||
|
size: customizeSize,
|
||
|
suffix,
|
||
|
enterButton = false,
|
||
|
addonAfter,
|
||
|
loading,
|
||
|
disabled,
|
||
|
onSearch: customOnSearch,
|
||
|
onChange: customOnChange,
|
||
|
onCompositionStart,
|
||
|
onCompositionEnd
|
||
|
} = props,
|
||
|
restProps = __rest(props, ["prefixCls", "inputPrefixCls", "className", "size", "suffix", "enterButton", "addonAfter", "loading", "disabled", "onSearch", "onChange", "onCompositionStart", "onCompositionEnd"]);
|
||
|
const {
|
||
|
getPrefixCls,
|
||
|
direction
|
||
|
} = React.useContext(_configProvider.ConfigContext);
|
||
|
const composedRef = React.useRef(false);
|
||
|
const prefixCls = getPrefixCls('input-search', customizePrefixCls);
|
||
|
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
|
||
|
const {
|
||
|
compactSize
|
||
|
} = (0, _Compact.useCompactItemContext)(prefixCls, direction);
|
||
|
const size = (0, _useSize.default)(ctx => {
|
||
|
var _a;
|
||
|
return (_a = customizeSize !== null && customizeSize !== void 0 ? customizeSize : compactSize) !== null && _a !== void 0 ? _a : ctx;
|
||
|
});
|
||
|
const inputRef = React.useRef(null);
|
||
|
const onChange = e => {
|
||
|
if ((e === null || e === void 0 ? void 0 : e.target) && e.type === 'click' && customOnSearch) {
|
||
|
customOnSearch(e.target.value, e, {
|
||
|
source: 'clear'
|
||
|
});
|
||
|
}
|
||
|
customOnChange === null || customOnChange === void 0 ? void 0 : customOnChange(e);
|
||
|
};
|
||
|
const onMouseDown = e => {
|
||
|
var _a;
|
||
|
if (document.activeElement === ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.input)) {
|
||
|
e.preventDefault();
|
||
|
}
|
||
|
};
|
||
|
const onSearch = e => {
|
||
|
var _a, _b;
|
||
|
if (customOnSearch) {
|
||
|
customOnSearch((_b = (_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.input) === null || _b === void 0 ? void 0 : _b.value, e, {
|
||
|
source: 'input'
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
const onPressEnter = e => {
|
||
|
if (composedRef.current || loading) {
|
||
|
return;
|
||
|
}
|
||
|
onSearch(e);
|
||
|
};
|
||
|
const searchIcon = typeof enterButton === 'boolean' ? /*#__PURE__*/React.createElement(_SearchOutlined.default, null) : null;
|
||
|
const btnClassName = `${prefixCls}-button`;
|
||
|
let button;
|
||
|
const enterButtonAsElement = enterButton || {};
|
||
|
const isAntdButton = enterButtonAsElement.type && enterButtonAsElement.type.__ANT_BUTTON === true;
|
||
|
if (isAntdButton || enterButtonAsElement.type === 'button') {
|
||
|
button = (0, _reactNode.cloneElement)(enterButtonAsElement, Object.assign({
|
||
|
onMouseDown,
|
||
|
onClick: e => {
|
||
|
var _a, _b;
|
||
|
(_b = (_a = enterButtonAsElement === null || enterButtonAsElement === void 0 ? void 0 : enterButtonAsElement.props) === null || _a === void 0 ? void 0 : _a.onClick) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
||
|
onSearch(e);
|
||
|
},
|
||
|
key: 'enterButton'
|
||
|
}, isAntdButton ? {
|
||
|
className: btnClassName,
|
||
|
size
|
||
|
} : {}));
|
||
|
} else {
|
||
|
button = /*#__PURE__*/React.createElement(_button.default, {
|
||
|
className: btnClassName,
|
||
|
type: enterButton ? 'primary' : undefined,
|
||
|
size: size,
|
||
|
disabled: disabled,
|
||
|
key: "enterButton",
|
||
|
onMouseDown: onMouseDown,
|
||
|
onClick: onSearch,
|
||
|
loading: loading,
|
||
|
icon: searchIcon
|
||
|
}, enterButton);
|
||
|
}
|
||
|
if (addonAfter) {
|
||
|
button = [button, (0, _reactNode.cloneElement)(addonAfter, {
|
||
|
key: 'addonAfter'
|
||
|
})];
|
||
|
}
|
||
|
const cls = (0, _classnames.default)(prefixCls, {
|
||
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
||
|
[`${prefixCls}-${size}`]: !!size,
|
||
|
[`${prefixCls}-with-button`]: !!enterButton
|
||
|
}, className);
|
||
|
const handleOnCompositionStart = e => {
|
||
|
composedRef.current = true;
|
||
|
onCompositionStart === null || onCompositionStart === void 0 ? void 0 : onCompositionStart(e);
|
||
|
};
|
||
|
const handleOnCompositionEnd = e => {
|
||
|
composedRef.current = false;
|
||
|
onCompositionEnd === null || onCompositionEnd === void 0 ? void 0 : onCompositionEnd(e);
|
||
|
};
|
||
|
return /*#__PURE__*/React.createElement(_Input.default, Object.assign({
|
||
|
ref: (0, _ref.composeRef)(inputRef, ref),
|
||
|
onPressEnter: onPressEnter
|
||
|
}, restProps, {
|
||
|
size: size,
|
||
|
onCompositionStart: handleOnCompositionStart,
|
||
|
onCompositionEnd: handleOnCompositionEnd,
|
||
|
prefixCls: inputPrefixCls,
|
||
|
addonAfter: button,
|
||
|
suffix: suffix,
|
||
|
onChange: onChange,
|
||
|
className: cls,
|
||
|
disabled: disabled
|
||
|
}));
|
||
|
});
|
||
|
if (process.env.NODE_ENV !== 'production') {
|
||
|
Search.displayName = 'Search';
|
||
|
}
|
||
|
var _default = exports.default = Search;
|