vantui-edit
Version:
一套适用于Taro3及React的vantui组件库
155 lines (146 loc) • 6.81 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["value", "defaultValue", "label", "focus", "error", "disabled", "readonly", "inputAlign", "showAction", "leftIcon", "rightIcon", "placeholder", "placeholderStyle", "actionText", "background", "maxlength", "shape", "clearable", "clearTrigger", "clearIcon", "renderLabel", "renderLeftIcon", "renderRightIcon", "renderAction", "onFocus", "onBlur", "onChange", "onClear", "onClickInput", "onSearch", "onCancel", "style", "className"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import { useState, useEffect, useMemo } from 'react';
import { View } from '@tarojs/components';
import * as utils from '../wxs/utils';
import { Field } from '../field';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export function Search(props) {
var value = props.value,
_props$defaultValue = props.defaultValue,
defaultValue = _props$defaultValue === void 0 ? '' : _props$defaultValue,
label = props.label,
focus = props.focus,
error = props.error,
disabled = props.disabled,
readonly = props.readonly,
inputAlign = props.inputAlign,
showAction = props.showAction,
_props$leftIcon = props.leftIcon,
leftIcon = _props$leftIcon === void 0 ? 'search' : _props$leftIcon,
rightIcon = props.rightIcon,
placeholder = props.placeholder,
placeholderStyle = props.placeholderStyle,
_props$actionText = props.actionText,
actionText = _props$actionText === void 0 ? '取消' : _props$actionText,
_props$background = props.background,
background = _props$background === void 0 ? '#ffffff' : _props$background,
_props$maxlength = props.maxlength,
maxlength = _props$maxlength === void 0 ? -1 : _props$maxlength,
_props$shape = props.shape,
shape = _props$shape === void 0 ? 'square' : _props$shape,
_props$clearable = props.clearable,
clearable = _props$clearable === void 0 ? true : _props$clearable,
_props$clearTrigger = props.clearTrigger,
clearTrigger = _props$clearTrigger === void 0 ? 'focus' : _props$clearTrigger,
_props$clearIcon = props.clearIcon,
clearIcon = _props$clearIcon === void 0 ? 'clear' : _props$clearIcon,
renderLabel = props.renderLabel,
renderLeftIcon = props.renderLeftIcon,
renderRightIcon = props.renderRightIcon,
renderAction = props.renderAction,
onFocus = props.onFocus,
onBlur = props.onBlur,
onChange = props.onChange,
onClear = props.onClear,
onClickInput = props.onClickInput,
onSearch = props.onSearch,
onCancel = props.onCancel,
style = props.style,
className = props.className,
others = _objectWithoutProperties(props, _excluded);
var noControlled = useMemo(function () {
return typeof value === 'undefined';
}, [value]);
var _useState = useState(noControlled ? defaultValue : value),
_useState2 = _slicedToArray(_useState, 2),
innerValue = _useState2[0],
setInnerValue = _useState2[1];
var _change = function _change(event) {
if (noControlled) {
setInnerValue(event.detail);
}
onChange === null || onChange === void 0 ? void 0 : onChange(event);
};
var _cancel = function _cancel(e) {
/**
* 修复修改输入框值时,输入框失焦和赋值同时触发,赋值失效
* https://github.com/youzan/@vant/weapp/issues/1768
*/
setTimeout(function () {
setInnerValue('');
onCancel === null || onCancel === void 0 ? void 0 : onCancel();
Object.defineProperty(e, 'detail', {
value: ''
});
onChange === null || onChange === void 0 ? void 0 : onChange(e);
}, 200);
};
useEffect(function () {
if (!noControlled) {
setInnerValue(value);
}
}, [noControlled, value]);
var searchValue = noControlled ? innerValue : value;
return /*#__PURE__*/_jsxs(View, _objectSpread(_objectSpread({
className: "".concat(utils.bem('search', {
withaction: showAction || !!renderAction
}), " ").concat(className),
style: utils.style([{
background: background
}, style])
}, others), {}, {
children: [/*#__PURE__*/_jsxs(View, {
className: utils.bem('search__content', [shape]),
children: [label ? /*#__PURE__*/_jsx(View, {
className: "van-search__label",
children: label
}) : renderLabel, /*#__PURE__*/_jsx(Field, {
type: "text",
leftIcon: !renderLeftIcon ? leftIcon : '',
rightIcon: !renderRightIcon ? rightIcon : '',
focus: focus,
error: error,
border: false,
confirmType: "search",
className: "van-search__field field-class",
value: searchValue,
disabled: disabled,
readonly: readonly,
clearable: clearable,
clearTrigger: clearTrigger,
clearIcon: clearIcon,
maxlength: maxlength,
inputAlign: inputAlign,
placeholder: placeholder,
"placeholder-style": placeholderStyle,
renderLeftIcon: renderLeftIcon,
renderRightIcon: renderRightIcon,
style: "padding: 5px 10px 5px 0; background-color: transparent;",
onBlur: onBlur,
onFocus: onFocus,
onChange: _change,
onConfirm: onSearch // @ts-ignore
,
onClear: onClear // @ts-ignore
,
onClickInput: onClickInput
})]
}), (showAction || renderAction) && /*#__PURE__*/_jsx(View, {
className: "van-search__action",
hoverClass: "van-search__action--hover",
hoverStayTime: 70,
children: renderAction || /*#__PURE__*/_jsx(View, {
onClick: _cancel,
className: "cancel-class",
children: actionText
})
})]
}));
}
export default Search;