直播小程序源码,react-native自定义文本输入框
直播小程序源码,react-native自定义文本输入框
Examples from props:
...
_onChange = (label, value) => {
this.setState({ [label]: value });
};
render() {
return (
<View style={styles.container}>
<Text>
{this.state.result}
</Text>
<Input onChange={(value) => this._onChange('input1', value)} value={this.state.input1 || ''} />
<Input label={'姓名'} onChange={(value) => this._onChange('input2', value)} value={this.state.input2 || ''} />
<Input onChange={(value) => this._onChange('input3', value)} mode={'TextArea'} label={'详细地址'} value={this.state.input3 || ''} />
</View>
);
}
...
实现代码:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
TextInput,
View,
Text,
StyleSheet,
} from 'react-native';
const styles = StyleSheet.create({
center: {
justifyContent: 'center',
alignItems: 'center'
}
});
class Input extends Component {
static propTypes = {
inputStyle: TextInput.propTypes.style,
labelTextStyle: TextInput.propTypes.style,
placeholderTextColor: PropTypes.string,
isUpdate: PropTypes.bool,
readonly: PropTypes.bool,
textInputStyle: TextInput.propTypes.style,
autoCapitalize: PropTypes.oneOf(['characters', 'words', 'sentences', 'none']),
label: PropTypes.string,
placeholder: PropTypes.string,
};
static defaultProps = {
placeholderTextColor: '#ccc8c4',
autoCapitalize: 'none',
isUpdate: true,
readonly: false,
label: '文本输入框',
placeholder: '请输入'
};
constructor(props) {
super(props);
this.state = {
value: props.value || '',
textAlign: 'right'
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.state.value) {
this.setState({ value: nextProps.value });
}
}
_onChangeText = (val) => {
this.setState({ value: val });
this.props.onChange && this.props.onChange(val.replace(/(^\s*)|(\s*$)/g, ''));
};
_onBlur = (e) => {
const { onBlur } = this.props;
onBlur && onBlur(e);
this.setState({
textAlign: 'right'
});
};
_onFocus = (e) => {
const { onFocus } = this.props;
onFocus && onFocus(e);
this.setState({
textAlign: 'left'
});
};
_renderInputContent = () => {
const { textInputStyle, placeholderTextColor, autoCapitalize, isUpdate, suffix } = this.props;
return (
isUpdate ?
<View
style={[{ flexDirection: 'row', flex: 1, height: '100%' }, styles.center]}
>
<TextInput
{...this.props}
onChangeText={this._onChangeText}
onBlur={this._onBlur}
onFocus={this._onFocus}
style={[{ textAlign: (this.state.value === 0 || this.state.value) ?
this.state.textAlign : 'left', flex: 1, fontSize: 14, color: '#332f2b' }, textInputStyle]}
placeholderTextColor={placeholderTextColor}
value={String(this.state.value)}
autoCorrect={false}
autoCapitalize={autoCapitalize}
underlineColorAndroid="transparent"
/>
<Text style={{ marginRight: 10 }}>
{(suffix && suffix.length > 3) ? '' : suffix}
</Text>
</View>
:
<View
style={[{ flexDirection: 'row', flex: 1, height: '100%', backgroundColor: '#f7f6f5' }, styles.center]}
>
<Text
style={{ textAlign: this.state.value ? this.state.textAlign : 'left', flex: 1 }}
>
{this.state.value}
</Text>
<Text style={{ marginRight: 10 }}>
{(suffix && suffix.length > 3) ? '' : suffix}
</Text>
</View>
);
};
以上就是 直播小程序源码,react-native自定义文本输入框,更多内容欢迎关注之后的文章