Skip to content

Commit

Permalink
fix:修复input输入框ref值
Browse files Browse the repository at this point in the history
  • Loading branch information
SunLxy committed May 31, 2023
1 parent d293e1c commit f9e6144
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions packages/core/src/Input/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, PropsWithoutRef } from 'react';
import {
TextInput,
TextInputProps,
Expand Down Expand Up @@ -47,15 +47,15 @@ export interface InputProps extends TextInputProps {
/** 容器样式 */
containerStyle?: StyleProp<ViewStyle>;
/** 输入框 ref */
inputRef?: React.RefObject<TextInput>;
inputRef?: React.ForwardedRef<TextInput>;
}

interface InputState {
value?: string;
control: 'props' | 'state';
}

const Input = (props: InputProps) => {
const Input = React.forwardRef<TextInput, PropsWithoutRef<InputProps>>((props, ref) => {
const {
wrongfulHandle,
rule,
Expand Down Expand Up @@ -148,18 +148,30 @@ const Input = (props: InputProps) => {
style={[styles.container, { flex: 1, borderColor: borderColor }, border ? styles[border] : {}]}
testID="RNE__Input__view"
>
{typeof extraStart === 'string' ? <Text color="primary_text" style={{ fontSize }}>{extraStart}</Text> : extraStart}
{typeof extraStart === 'string' ? (
<Text color="primary_text" style={{ fontSize }}>
{extraStart}
</Text>
) : (
extraStart
)}
<TextInput
testID="RNE__Input__input"
{...others}
ref={inputRef}
ref={ref || inputRef}
editable={!disabled}
value={defaultValue}
onChangeText={onInputChange}
onFocus={onInputFocus}
style={[{ fontSize, color: theme.colors.text || '#000' }, styles.input, style]}
/>
{typeof extraEnd === 'string' ? <Text color="primary_text" style={{ fontSize }}>{extraEnd}</Text> : extraEnd}
{typeof extraEnd === 'string' ? (
<Text color="primary_text" style={{ fontSize }}>
{extraEnd}
</Text>
) : (
extraEnd
)}
{error && (renderError || <Icon name="circle-close" color="#dc3545" />)}
</View>
{clear && (
Expand All @@ -170,12 +182,16 @@ const Input = (props: InputProps) => {
onInputChange?.('');
}}
>
{renderClear || <Text color="primary_text" style={[{ fontSize }, clearStyle]}>清除</Text>}
{renderClear || (
<Text color="primary_text" style={[{ fontSize }, clearStyle]}>
清除
</Text>
)}
</TouchableOpacity>
)}
</View>
);
};
});
export default Input;
type CreStyle = {
bgColor: string;
Expand Down Expand Up @@ -218,4 +234,4 @@ function createStyles({ bgColor }: CreStyle) {
color: '#f50',
},
});
}
}

0 comments on commit f9e6144

Please sign in to comment.