tooot/src/screens/Shared/Compose/TextInput.tsx

79 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-12-04 01:17:10 +01:00
import React, { Dispatch, RefObject } from 'react'
import { StyleSheet, Text, TextInput } from 'react-native'
import { StyleConstants } from 'src/utils/styles/constants'
import { useTheme } from 'src/utils/styles/ThemeManager'
2020-12-07 12:31:40 +01:00
import { PostAction, ComposeState } from '../Compose'
2020-12-04 01:17:10 +01:00
import formatText from './formatText'
export interface Props {
2020-12-07 12:31:40 +01:00
composeState: ComposeState
composeDispatch: Dispatch<PostAction>
2020-12-04 01:17:10 +01:00
textInputRef: RefObject<TextInput>
}
const ComposeTextInput: React.FC<Props> = ({
2020-12-07 12:31:40 +01:00
composeState,
composeDispatch,
2020-12-04 01:17:10 +01:00
textInputRef
}) => {
const { theme } = useTheme()
return (
<TextInput
style={[
styles.textInput,
{
2020-12-06 21:42:19 +01:00
color: theme.primary,
borderBottomColor: theme.border
2020-12-04 01:17:10 +01:00
}
]}
autoCapitalize='none'
autoCorrect={false}
autoFocus
enablesReturnKeyAutomatically
multiline
placeholder='想说点什么'
placeholderTextColor={theme.secondary}
onChangeText={content =>
formatText({
2020-12-06 23:51:13 +01:00
origin: 'text',
2020-12-07 12:31:40 +01:00
composeDispatch,
2020-12-04 01:17:10 +01:00
content
})
}
onSelectionChange={({
nativeEvent: {
selection: { start, end }
}
}) => {
2020-12-10 19:19:56 +01:00
composeDispatch({
type: 'text',
payload: { selection: { start, end } }
})
2020-12-04 01:17:10 +01:00
}}
ref={textInputRef}
scrollEnabled
>
2020-12-07 12:31:40 +01:00
<Text>{composeState.text.formatted}</Text>
2020-12-04 01:17:10 +01:00
</TextInput>
)
}
const styles = StyleSheet.create({
textInput: {
fontSize: StyleConstants.Font.Size.M,
marginTop: StyleConstants.Spacing.S,
2020-12-06 21:42:19 +01:00
paddingBottom: StyleConstants.Spacing.M,
marginLeft: StyleConstants.Spacing.Global.PagePadding,
2020-12-06 23:51:13 +01:00
marginRight: StyleConstants.Spacing.Global.PagePadding
2020-12-06 22:32:36 +01:00
// borderBottomWidth: 0.5
2020-12-04 01:17:10 +01:00
}
})
export default React.memo(
ComposeTextInput,
(prev, next) =>
2020-12-10 19:19:56 +01:00
prev.composeState.text.raw === next.composeState.text.raw &&
2020-12-07 12:31:40 +01:00
prev.composeState.text.formatted === next.composeState.text.formatted
2020-12-04 01:17:10 +01:00
)