@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
180 lines (179 loc) • 7.8 kB
JavaScript
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { forwardRef, useCallback, useImperativeHandle, useRef, useState } from "react";
import { MaskClose } from "@nutui/icons-react";
import { formatNumber } from "./utils";
import { useConfig, useRtl } from "../configprovider";
import { ComponentDefaults } from "../../utils/typings";
import { usePropsValue } from "../../hooks/use-props-value";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
type: 'text',
name: '',
placeholder: undefined,
confirmType: 'done',
align: 'left',
required: false,
disabled: false,
readOnly: false,
maxLength: 9999,
clearable: false,
clearIcon: null,
formatTrigger: 'onChange',
autoFocus: false,
plain: false
});
export var Input = /*#__PURE__*/ forwardRef(function(props, ref) {
var rtl = useRtl();
var locale = useConfig().locale;
var _$_object_spread = _object_spread({}, defaultProps, props), type = _$_object_spread.type, name = _$_object_spread.name, placeholder = _$_object_spread.placeholder, align = _$_object_spread.align, disabled = _$_object_spread.disabled, readOnly = _$_object_spread.readOnly, maxLength = _$_object_spread.maxLength, clearable = _$_object_spread.clearable, clearIcon = _$_object_spread.clearIcon, formatTrigger = _$_object_spread.formatTrigger, autoFocus = _$_object_spread.autoFocus, style = _$_object_spread.style, className = _$_object_spread.className, inputStyle = _$_object_spread.inputStyle, onChange = _$_object_spread.onChange, onFocus = _$_object_spread.onFocus, onClear = _$_object_spread.onClear, formatter = _$_object_spread.formatter, onClick = _$_object_spread.onClick, confirmType = _$_object_spread.confirmType, plain = _$_object_spread.plain, defaultValue = _$_object_spread.defaultValue, _value = _$_object_spread.value, onCompositionStart = _$_object_spread.onCompositionStart, onCompositionEnd = _$_object_spread.onCompositionEnd, rest = _object_without_properties(_$_object_spread, [
"type",
"name",
"placeholder",
"align",
"disabled",
"readOnly",
"maxLength",
"clearable",
"clearIcon",
"formatTrigger",
"autoFocus",
"style",
"className",
"inputStyle",
"onChange",
"onFocus",
"onClear",
"formatter",
"onClick",
"confirmType",
"plain",
"defaultValue",
"value",
"onCompositionStart",
"onCompositionEnd"
]);
var _usePropsValue = _sliced_to_array(usePropsValue({
value: _value,
defaultValue: defaultValue,
finalValue: '',
onChange: onChange
}), 2), value = _usePropsValue[0], setValue = _usePropsValue[1];
var inputRef = useRef(null);
var composingRef = useRef(false);
var _useState = _sliced_to_array(useState(false), 2), active = _useState[0], setActive = _useState[1];
useImperativeHandle(ref, function() {
return {
clear: function clear() {
return setValue('');
},
focus: function focus() {
var _inputRef_current;
return (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : _inputRef_current.focus();
},
blur: function blur() {
var _inputRef_current;
return (_inputRef_current = inputRef.current) === null || _inputRef_current === void 0 ? void 0 : _inputRef_current.blur();
},
get nativeElement () {
return inputRef.current;
}
};
});
var getInputClass = useCallback(function() {
var classPrefix = 'nut-input';
return [
classPrefix,
"".concat(disabled ? "".concat(classPrefix, "-disabled") : ''),
readOnly ? "".concat(classPrefix, "-readonly") : '',
"".concat(plain ? "".concat(classPrefix, "-plain") : "".concat(classPrefix, "-container"))
].filter(Boolean).join(' ');
}, [
disabled,
readOnly,
plain
]);
var handleValueUpdate = function handleValueUpdate(inputValue, trigger) {
var updatedValue = inputValue;
if (type === 'number') updatedValue = formatNumber(updatedValue, false, true);
if (type === 'digit') updatedValue = formatNumber(updatedValue, true, true);
if (formatter && trigger === formatTrigger) updatedValue = formatter(updatedValue);
setValue(updatedValue);
if (trigger !== 'onChange') {
var eventHandler = props[trigger];
eventHandler === null || eventHandler === void 0 ? void 0 : eventHandler(updatedValue);
}
};
var handleFocus = function handleFocus(event) {
onFocus === null || onFocus === void 0 ? void 0 : onFocus(event.target.value);
setActive(true);
};
var handleBlur = function handleBlur(event) {
handleValueUpdate(event.target.value, 'onBlur');
setTimeout(function() {
return setActive(false);
}, 200);
};
var handleInputChange = function handleInputChange(event) {
handleValueUpdate(event.target.value, 'onChange');
};
var getInputType = function getInputType(inputType) {
if (inputType === 'digit') return 'text';
if (inputType === 'number') return 'tel';
return inputType;
};
var getTextAlign = function getTextAlign() {
if (rtl) {
if (align === 'right') return 'left';
if (align === 'left') return 'right';
}
return align;
};
return /*#__PURE__*/ React.createElement("div", {
className: "".concat(getInputClass(), " ").concat(className || ''),
style: style,
onClick: onClick
}, /*#__PURE__*/ React.createElement("input", _object_spread_props(_object_spread({}, rest), {
ref: inputRef,
name: name,
className: "nut-input-native",
style: _object_spread({}, {
textAlign: getTextAlign()
}, inputStyle),
type: getInputType(type),
maxLength: maxLength,
placeholder: placeholder === undefined ? locale.placeholder : placeholder,
disabled: disabled,
readOnly: readOnly,
value: value,
autoFocus: autoFocus,
enterKeyHint: confirmType,
onFocus: handleFocus,
onBlur: handleBlur,
onChange: handleInputChange,
onCompositionStart: function onCompositionStart1(e) {
composingRef.current = true;
onCompositionStart === null || onCompositionStart === void 0 ? void 0 : onCompositionStart(e);
},
onCompositionEnd: function onCompositionEnd1(e) {
composingRef.current = false;
onCompositionEnd === null || onCompositionEnd === void 0 ? void 0 : onCompositionEnd(e);
}
})), clearable && !readOnly && active && value.length > 0 && /*#__PURE__*/ React.createElement("span", {
style: {
display: 'flex',
alignItems: 'center',
cursor: 'pointer'
},
onClick: function onClick() {
if (!disabled) {
setValue('');
onClear === null || onClear === void 0 ? void 0 : onClear('');
}
}
}, clearIcon || /*#__PURE__*/ React.createElement(MaskClose, {
className: "nut-input-clear"
})));
});
Input.displayName = 'NutInput';