UNPKG

antd-mobile-taro-ui

Version:

以antd-mobile为设计标准,基于taro框架的微信小程序组件库

106 lines (91 loc) 3.18 kB
import React, { forwardRef, useImperativeHandle, useRef } from 'react'; import { usePropsValue } from 'antd-mobile/es/utils/use-props-value'; import { RoundCloseFillIcon } from 'antd-mobile-taro-icons'; import { withNativeProps } from 'antd-mobile/es/utils/native-props'; import { mergeProps } from 'antd-mobile/es/utils/with-default-props'; import classNames from 'classnames'; import { bound } from 'antd-mobile/es/utils/bound'; import { isIOS } from 'antd-mobile/es/utils/validate'; import { View, Input as InputTaro } from '@tarojs/components'; const classPrefix = `adm-input`; const defaultProps = { defaultValue: '' }; export const Input = forwardRef((p, ref) => { const props = mergeProps(defaultProps, p); const [value, setValue] = usePropsValue(props); const compositionStartRef = useRef(false); const nativeInputRef = useRef(null); useImperativeHandle(ref, () => ({ clear: () => { setValue(''); }, focus: () => { var _a; (_a = nativeInputRef.current) === null || _a === void 0 ? void 0 : _a.focus(); }, blur: () => { var _a; (_a = nativeInputRef.current) === null || _a === void 0 ? void 0 : _a.blur(); }, get nativeElement() { return nativeInputRef.current; } })); const handleKeydown = e => { var _a; (_a = props.onEnterPress) === null || _a === void 0 ? void 0 : _a.call(props, e); }; function checkValue() { let nextValue = value; if (props.type === 'number') { nextValue = nextValue && bound(parseFloat(nextValue), props.min, props.max).toString(); } if (nextValue !== value) { setValue(nextValue); } } const shouldShowClear = (() => { if (!props.clearable || !value || props.readOnly) return false; return true; })(); return withNativeProps(props, React.createElement(View, { className: classNames(`${classPrefix}`, props.disabled && `${classPrefix}-disabled`) }, React.createElement(InputTaro, { ref: nativeInputRef, className: `${classPrefix}-element`, value: value, onInput: e => { setValue(e.detail.value); }, onFocus: e => { var _a; (_a = props.onFocus) === null || _a === void 0 ? void 0 : _a.call(props, e); }, onBlur: e => { var _a; checkValue(); (_a = props.onBlur) === null || _a === void 0 ? void 0 : _a.call(props, e); }, id: props.id, placeholder: props.placeholder, disabled: props.disabled || props.readOnly, onConfirm: handleKeydown, maxlength: props.maxlength, focus: props.focus, password: props.type === 'password', type: props.type === 'password' ? 'text' : props.type, name: props.name }), shouldShowClear && React.createElement(View, { className: `${classPrefix}-clear`, onClick: () => { var _a, _b; setValue(''); (_a = props.onClear) === null || _a === void 0 ? void 0 : _a.call(props); if (isIOS() && compositionStartRef.current) { compositionStartRef.current = false; (_b = nativeInputRef.current) === null || _b === void 0 ? void 0 : _b.blur(); } } }, React.createElement(RoundCloseFillIcon, null)))); });