UNPKG

linkmore-design

Version:

🌈 πŸš€lmη»„δ»ΆεΊ“γ€‚πŸš€

135 lines β€’ 6.05 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _excluded = ["prefixCls", "inputPrefixCls", "className", "size", "suffix", "enterButton", "addonAfter", "loading", "disabled", "onSearch", "onChange", "onCompositionStart", "onCompositionEnd"]; import classNames from 'classnames'; import { composeRef } from 'rc-util/lib/ref'; import * as React from 'react'; import Button from "../button"; import { ConfigContext } from "../config-provider"; import SizeContext from "../config-provider/SizeContext"; import { useCompactItemContext } from "../space/Compact"; import { cloneElement } from "../_util/reactNode"; import IconFont from "../icon-font"; import Input from "./Input"; var Search = /*#__PURE__*/React.forwardRef(function (props, ref) { var _classNames; var customizePrefixCls = props.prefixCls, customizeInputPrefixCls = props.inputPrefixCls, className = props.className, customizeSize = props.size, suffix = props.suffix, _props$enterButton = props.enterButton, enterButton = _props$enterButton === void 0 ? false : _props$enterButton, addonAfter = props.addonAfter, loading = props.loading, disabled = props.disabled, customOnSearch = props.onSearch, customOnChange = props.onChange, onCompositionStart = props.onCompositionStart, onCompositionEnd = props.onCompositionEnd, restProps = _objectWithoutProperties(props, _excluded); var _React$useContext = React.useContext(ConfigContext), getPrefixCls = _React$useContext.getPrefixCls, direction = _React$useContext.direction; var contextSize = React.useContext(SizeContext); var composedRef = React.useRef(false); var prefixCls = getPrefixCls('input-search', customizePrefixCls); var inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls); var _useCompactItemContex = useCompactItemContext(prefixCls, direction), compactSize = _useCompactItemContex.compactSize; var size = compactSize || customizeSize || contextSize; var inputRef = React.useRef(null); var onChange = function onChange(e) { if (e && e.target && e.type === 'click' && customOnSearch) { customOnSearch(e.target.value, e); } if (customOnChange) { customOnChange(e); } }; var onMouseDown = function onMouseDown(e) { var _inputRef$current; if (document.activeElement === ((_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.input)) { e.preventDefault(); } }; var onSearch = function onSearch(e) { if (customOnSearch) { var _inputRef$current2, _inputRef$current2$in; customOnSearch((_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : (_inputRef$current2$in = _inputRef$current2.input) === null || _inputRef$current2$in === void 0 ? void 0 : _inputRef$current2$in.value, e); } }; var onPressEnter = function onPressEnter(e) { if (composedRef.current || loading) { return; } onSearch(e); }; var searchIcon = typeof enterButton === 'boolean' ? /*#__PURE__*/React.createElement(IconFont, { type: "icon-sousuo" }) : null; var btnClassName = "".concat(prefixCls, "-button"); var button; var enterButtonAsElement = enterButton || {}; var isAntdButton = enterButtonAsElement.type && enterButtonAsElement.type.__ANT_BUTTON === true; if (isAntdButton || enterButtonAsElement.type === 'button') { button = cloneElement(enterButtonAsElement, _objectSpread({ onMouseDown: onMouseDown, onClick: function onClick(e) { var _enterButtonAsElement, _enterButtonAsElement2; enterButtonAsElement === null || enterButtonAsElement === void 0 ? void 0 : (_enterButtonAsElement = enterButtonAsElement.props) === null || _enterButtonAsElement === void 0 ? void 0 : (_enterButtonAsElement2 = _enterButtonAsElement.onClick) === null || _enterButtonAsElement2 === void 0 ? void 0 : _enterButtonAsElement2.call(_enterButtonAsElement, e); onSearch(e); }, key: 'enterButton' }, isAntdButton ? { className: btnClassName, size: size } : {})); } else { button = /*#__PURE__*/React.createElement(Button, { 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, cloneElement(addonAfter, { key: 'addonAfter' })]; } var cls = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), !!size), _defineProperty(_classNames, "".concat(prefixCls, "-with-button"), !!enterButton), _classNames), className); var handleOnCompositionStart = function handleOnCompositionStart(e) { composedRef.current = true; onCompositionStart === null || onCompositionStart === void 0 ? void 0 : onCompositionStart(e); }; var handleOnCompositionEnd = function handleOnCompositionEnd(e) { composedRef.current = false; onCompositionEnd === null || onCompositionEnd === void 0 ? void 0 : onCompositionEnd(e); }; return /*#__PURE__*/React.createElement(Input, _extends({ 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'; } export default Search;