UNPKG

@ray-js/components

Version:

Ray basic components

109 lines 3.45 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["name", "id", "className", "style", "disabled", "type", "value", "focus", "maxLength", "onBlur", "onConfirm", "onFocus", "onInput", "password", "placeholder", "placeholderStyle"]; import * as React from 'react'; import clsx from 'clsx'; import { Input as RemaxInput } from '@ray-core/wechat'; import { inlineStyle } from '@ray-js/framework-shared'; import { defaultInputProps } from './props'; import { nextFrame } from '../core'; import styles from './index.module.less'; const Input = /*#__PURE__*/React.forwardRef((props, ref) => { const { name, id, className, style, disabled, type = 'text', value, focus, maxLength, onBlur, onConfirm, onFocus, onInput, password, placeholder, placeholderStyle } = props, restProps = _objectWithoutProperties(props, _excluded); const [internalValue, setInternalValue] = React.useState(value); const bufferValue = React.useRef(value); React.useEffect(() => { if (value !== bufferValue.current) { // HACK: 避免重复设置相同 value 不渲染的问题 setInternalValue(bufferValue.current); nextFrame(1).then(() => { bufferValue.current = value; setInternalValue(value); }); } }, [value]); const selection = focus ? { selectionStart: (internalValue === null || internalValue === void 0 ? void 0 : internalValue.length) || -1, selectionEnd: (internalValue === null || internalValue === void 0 ? void 0 : internalValue.length) || -1 } : undefined; return /*#__PURE__*/React.createElement(RemaxInput, _extends({ name: name //@ts-ignore , ref: ref, id: id, style: inlineStyle(style), className: clsx('ray-input', styles.input, className), disabled: disabled, type: type, focus: focus, password: password, placeholder: placeholder, placeholderStyle: inlineStyle(placeholderStyle), maxlength: maxLength, onInput: function (event) { const { value } = event.detail; // 内部 value 管控 bufferValue.current = value; onInput === null || onInput === void 0 || onInput(_objectSpread(_objectSpread({}, event), {}, { type: 'input', value, origin: event })); }, onBlur: function (event) { const { value = '' } = event.detail; onBlur === null || onBlur === void 0 || onBlur(_objectSpread(_objectSpread({}, event), {}, { type: 'blur', value, origin: event })); }, onConfirm: function (event) { const { value = '' } = event.detail; setInternalValue(value); onConfirm === null || onConfirm === void 0 || onConfirm(event); }, onFocus: function (event) { const { value = '', height = 0 } = event.detail; setInternalValue(value); onFocus === null || onFocus === void 0 || onFocus(_objectSpread(_objectSpread({}, event), {}, { type: 'focus', value, height, origin: event })); }, value: internalValue }, selection, restProps)); }); Input.defaultProps = defaultInputProps; export default Input;