antd-mobile-taro-ui
Version:
以antd-mobile为设计标准,基于taro框架的微信小程序组件库
129 lines (100 loc) • 4.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Input = void 0;
var _react = _interopRequireWildcard(require("react"));
var _usePropsValue = require("antd-mobile/es/utils/use-props-value");
var _antdMobileTaroIcons = require("antd-mobile-taro-icons");
var _nativeProps = require("antd-mobile/es/utils/native-props");
var _withDefaultProps = require("antd-mobile/es/utils/with-default-props");
var _classnames = _interopRequireDefault(require("classnames"));
var _bound = require("antd-mobile/es/utils/bound");
var _validate = require("antd-mobile/es/utils/validate");
var _components = require("@tarojs/components");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const classPrefix = `adm-input`;
const defaultProps = {
defaultValue: ''
};
const Input = (0, _react.forwardRef)((p, ref) => {
const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
const [value, setValue] = (0, _usePropsValue.usePropsValue)(props);
const compositionStartRef = (0, _react.useRef)(false);
const nativeInputRef = (0, _react.useRef)(null);
(0, _react.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 && (0, _bound.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 (0, _nativeProps.withNativeProps)(props, _react.default.createElement(_components.View, {
className: (0, _classnames.default)(`${classPrefix}`, props.disabled && `${classPrefix}-disabled`)
}, _react.default.createElement(_components.Input, {
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.default.createElement(_components.View, {
className: `${classPrefix}-clear`,
onClick: () => {
var _a, _b;
setValue('');
(_a = props.onClear) === null || _a === void 0 ? void 0 : _a.call(props);
if ((0, _validate.isIOS)() && compositionStartRef.current) {
compositionStartRef.current = false;
(_b = nativeInputRef.current) === null || _b === void 0 ? void 0 : _b.blur();
}
}
}, _react.default.createElement(_antdMobileTaroIcons.RoundCloseFillIcon, null))));
});
exports.Input = Input;