antd-mobile
Version:
<div align="center">
160 lines (159 loc) • 6.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Input = void 0;
var _react = _interopRequireWildcard(require("react"));
var _usePropsValue = require("../../utils/use-props-value");
var _antdMobileIcons = require("antd-mobile-icons");
var _nativeProps = require("../../utils/native-props");
var _withDefaultProps = require("../../utils/with-default-props");
var _classnames = _interopRequireDefault(require("classnames"));
var _ahooks = require("ahooks");
var _bound = require("../../utils/bound");
var _validate = require("../../utils/validate");
var _configProvider = require("../config-provider");
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: '',
onlyShowClearWhenFocus: true
};
const Input = (0, _react.forwardRef)((p, ref) => {
const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
const [value, setValue] = (0, _usePropsValue.usePropsValue)(props);
const [hasFocus, setHasFocus] = (0, _react.useState)(false);
const compositionStartRef = (0, _react.useRef)(false);
const nativeInputRef = (0, _react.useRef)(null);
const {
locale
} = (0, _configProvider.useConfig)();
(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;
if (props.onEnterPress && (e.code === 'Enter' || e.keyCode === 13)) {
props.onEnterPress(e);
}
(_a = props.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(props, e);
};
(0, _ahooks.useIsomorphicLayoutEffect)(() => {
var _a;
if (!props.enterKeyHint) return;
(_a = nativeInputRef.current) === null || _a === void 0 ? void 0 : _a.setAttribute('enterkeyhint', props.enterKeyHint);
return () => {
var _a;
(_a = nativeInputRef.current) === null || _a === void 0 ? void 0 : _a.removeAttribute('enterkeyhint');
};
}, [props.enterKeyHint]);
function checkValue() {
let nextValue = value;
if (props.type === 'number') {
const boundValue = nextValue && (0, _bound.bound)(parseFloat(nextValue), props.min, props.max).toString();
// fix the display issue of numbers starting with 0
if (Number(nextValue) !== Number(boundValue)) {
nextValue = boundValue;
}
}
if (nextValue !== value) {
setValue(nextValue);
}
}
const shouldShowClear = (() => {
if (!props.clearable || !value || props.readOnly) return false;
if (props.onlyShowClearWhenFocus) {
return hasFocus;
} else {
return true;
}
})();
return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", {
className: (0, _classnames.default)(`${classPrefix}`, props.disabled && `${classPrefix}-disabled`)
}, _react.default.createElement("input", {
ref: nativeInputRef,
className: `${classPrefix}-element`,
value: value,
onChange: e => {
setValue(e.target.value);
},
onFocus: e => {
var _a;
setHasFocus(true);
(_a = props.onFocus) === null || _a === void 0 ? void 0 : _a.call(props, e);
},
onBlur: e => {
var _a;
setHasFocus(false);
checkValue();
(_a = props.onBlur) === null || _a === void 0 ? void 0 : _a.call(props, e);
},
id: props.id,
placeholder: props.placeholder,
disabled: props.disabled,
readOnly: props.readOnly,
maxLength: props.maxLength,
minLength: props.minLength,
max: props.max,
min: props.min,
autoComplete: props.autoComplete,
autoFocus: props.autoFocus,
pattern: props.pattern,
inputMode: props.inputMode,
type: props.type,
name: props.name,
autoCapitalize: props.autoCapitalize,
autoCorrect: props.autoCorrect,
onKeyDown: handleKeydown,
onKeyUp: props.onKeyUp,
onCompositionStart: e => {
var _a;
compositionStartRef.current = true;
(_a = props.onCompositionStart) === null || _a === void 0 ? void 0 : _a.call(props, e);
},
onCompositionEnd: e => {
var _a;
compositionStartRef.current = false;
(_a = props.onCompositionEnd) === null || _a === void 0 ? void 0 : _a.call(props, e);
},
onClick: props.onClick,
step: props.step,
role: props.role,
"aria-valuenow": props['aria-valuenow'],
"aria-valuemax": props['aria-valuemax'],
"aria-valuemin": props['aria-valuemin'],
"aria-label": props['aria-label']
}), shouldShowClear && _react.default.createElement("div", {
className: `${classPrefix}-clear`,
onMouseDown: e => {
e.preventDefault();
},
onClick: () => {
var _a, _b;
setValue('');
(_a = props.onClear) === null || _a === void 0 ? void 0 : _a.call(props);
// https://github.com/ant-design/ant-design-mobile/issues/5212
if ((0, _validate.isIOS)() && compositionStartRef.current) {
compositionStartRef.current = false;
(_b = nativeInputRef.current) === null || _b === void 0 ? void 0 : _b.blur();
}
},
"aria-label": locale.Input.clear
}, _react.default.createElement(_antdMobileIcons.CloseCircleFill, null))));
});
exports.Input = Input;
;