【发布时间】:2017-01-16 14:24:28
【问题描述】:
是否有任何用于 react-native 的内置文本区域组件?我已经尝试实现这些:
https://github.com/buildo/react-autosize-textarea
https://github.com/andreypopp/react-textarea-autosize
但得到一个错误“预期组件类得到对象对象”。
【问题讨论】:
标签: react-native
是否有任何用于 react-native 的内置文本区域组件?我已经尝试实现这些:
https://github.com/buildo/react-autosize-textarea
https://github.com/andreypopp/react-textarea-autosize
但得到一个错误“预期组件类得到对象对象”。
【问题讨论】:
标签: react-native
是的,有。叫TextInput,普通的TextInput组件支持多行。
只需将以下属性分配给您的 TextInput 组件
multiline = {true}
numberOfLines = {4}
最后你应该有这个:
<TextInput
multiline={true}
numberOfLines={4}
onChangeText={(text) => this.setState({text})}
value={this.state.text}/>
【讨论】:
如果你想看到你的TextInput 组件就像一个文本区域,你需要添加这个
<TextInput
multiline={true}
numberOfLines={10}
style={{ height:200, textAlignVertical: 'top',}}/>
【讨论】:
textAlignVertical="top" 作为道具,但目前仅支持android
我通过以下方式将 TextInput 组件包装到 View 中,从而在 react-native 中构建文本区域:
<View style={styles.textAreaContainer} >
<TextInput
style={styles.textArea}
underlineColorAndroid="transparent"
placeholder="Type something"
placeholderTextColor="grey"
numberOfLines={10}
multiline={true}
/>
</View>
...
const styles = StyleSheet.create({
textAreaContainer: {
borderColor: COLORS.grey20,
borderWidth: 1,
padding: 5
},
textArea: {
height: 150,
justifyContent: "flex-start"
}
})
【讨论】:
我正在使用这个组件: https://www.npmjs.com/package/react-native-autogrow-textinput
它会自动扩展文本增长。我创建了自己的可重用组件,其中包含 autogrow-textinput,组件内部如下所示:
<AutoGrowingTextInput
minHeight={40}
maxHeight={maxHeight} // this is a flexible value that I set in my
component, where I use this reusable component, same below, unless specified the other
onChangeText={onChangeText}
placeholder={placeholder}
placeholderTextColor='#C7C7CD'
style={inputStyle}
value={value}
/>
【讨论】:
如果您只使用 react-native 组件,您的选择是 TextInput
正如“funkysoul”所解释的:
只需将以下属性分配给您的 TextInput 组件
multiline = {true}numberOfLines = {4}
如果您想将此组件视为经典的textarea(大于内联文本输入),通常需要添加height 样式属性。请参阅以下示例:
<TextInput
multiline={true}
numberOfLines={10}
style={{ height:200, backgroundColor:'red'}}
/>
我添加了 backgroundColor 以便更好地理解 height 角色。请不要在您的项目中使用它;)
【讨论】: