2021-05-09 21:59:03 +02:00
|
|
|
import { StyleConstants } from '@utils/styles/constants'
|
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
2022-09-18 23:28:14 +02:00
|
|
|
import React, { forwardRef, RefObject } from 'react'
|
2022-05-07 00:52:32 +02:00
|
|
|
import { Platform, TextInput, TextInputProps, View } from 'react-native'
|
2021-05-09 21:59:03 +02:00
|
|
|
import Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated'
|
2022-12-28 23:41:36 +01:00
|
|
|
import { EmojisState } from './Emojis/Context'
|
2022-05-07 00:52:32 +02:00
|
|
|
import CustomText from './Text'
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-09-18 01:02:25 +02:00
|
|
|
export type Props = {
|
2021-05-09 21:59:03 +02:00
|
|
|
title?: string
|
|
|
|
multiline?: boolean
|
2023-01-26 00:57:48 +01:00
|
|
|
invalid?: boolean
|
|
|
|
} & Pick<NonNullable<EmojisState['inputProps'][0]>, 'value'> &
|
|
|
|
Pick<Partial<EmojisState['inputProps'][0]>, 'isFocused' | 'selection'> &
|
2022-09-18 23:28:14 +02:00
|
|
|
Omit<
|
|
|
|
TextInputProps,
|
|
|
|
| 'style'
|
|
|
|
| 'onChangeText'
|
|
|
|
| 'onSelectionChange'
|
|
|
|
| 'keyboardAppearance'
|
|
|
|
| 'textAlignVertical'
|
|
|
|
| 'multiline'
|
|
|
|
| 'selection'
|
|
|
|
| 'value'
|
|
|
|
>
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-09-18 01:02:25 +02:00
|
|
|
const ComponentInput = forwardRef(
|
|
|
|
(
|
2022-09-18 23:28:14 +02:00
|
|
|
{
|
|
|
|
title,
|
|
|
|
multiline = false,
|
2023-01-26 00:57:48 +01:00
|
|
|
invalid = false,
|
2022-09-18 23:28:14 +02:00
|
|
|
value: [value, setValue],
|
2023-01-26 00:57:48 +01:00
|
|
|
selection,
|
2022-09-18 23:28:14 +02:00
|
|
|
isFocused,
|
|
|
|
...props
|
|
|
|
}: Props,
|
2022-09-18 01:02:25 +02:00
|
|
|
ref: RefObject<TextInput>
|
|
|
|
) => {
|
|
|
|
const { colors, mode } = useTheme()
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-09-18 01:02:25 +02:00
|
|
|
const animateTitle = useAnimatedStyle(() => {
|
|
|
|
if (value) {
|
|
|
|
return {
|
|
|
|
fontSize: withTiming(StyleConstants.Font.Size.S),
|
|
|
|
paddingHorizontal: withTiming(StyleConstants.Spacing.XS),
|
|
|
|
left: withTiming(StyleConstants.Spacing.S),
|
|
|
|
top: withTiming(-(StyleConstants.Font.Size.S / 2) - 2),
|
2023-01-26 00:57:48 +01:00
|
|
|
backgroundColor: colors.backgroundDefault
|
2021-05-09 21:59:03 +02:00
|
|
|
}
|
2022-09-18 01:02:25 +02:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
fontSize: withTiming(StyleConstants.Font.Size.M),
|
|
|
|
paddingHorizontal: withTiming(0),
|
|
|
|
left: withTiming(StyleConstants.Spacing.S),
|
|
|
|
top: withTiming(StyleConstants.Spacing.S + 1),
|
|
|
|
backgroundColor: withTiming(colors.backgroundDefaultTransparent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [mode, value])
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-09-18 01:02:25 +02:00
|
|
|
return (
|
2021-05-10 23:41:48 +02:00
|
|
|
<View
|
2022-05-07 00:52:32 +02:00
|
|
|
style={{
|
|
|
|
borderWidth: 1,
|
|
|
|
marginVertical: StyleConstants.Spacing.S,
|
|
|
|
padding: StyleConstants.Spacing.S,
|
2023-01-26 00:57:48 +01:00
|
|
|
borderColor: invalid ? colors.red : colors.border,
|
2022-05-07 00:52:32 +02:00
|
|
|
flexDirection: multiline ? 'column' : 'row',
|
|
|
|
alignItems: 'stretch'
|
|
|
|
}}
|
2021-05-10 23:41:48 +02:00
|
|
|
>
|
2022-09-18 01:02:25 +02:00
|
|
|
<TextInput
|
|
|
|
ref={ref}
|
|
|
|
style={{
|
|
|
|
flex: 1,
|
|
|
|
fontSize: StyleConstants.Font.Size.M,
|
|
|
|
color: colors.primaryDefault,
|
|
|
|
minHeight:
|
|
|
|
Platform.OS === 'ios' && multiline ? StyleConstants.Font.LineHeight.M * 5 : undefined
|
|
|
|
}}
|
|
|
|
value={value}
|
2022-09-18 23:28:14 +02:00
|
|
|
onChangeText={setValue}
|
2023-01-26 00:57:48 +01:00
|
|
|
{...(isFocused !== undefined && {
|
|
|
|
onFocus: () => (isFocused.current = true),
|
|
|
|
onBlur: () => (isFocused.current = false)
|
|
|
|
})}
|
|
|
|
{...(selection !== undefined && {
|
|
|
|
onSelectionChange: ({ nativeEvent }) => selection[1](nativeEvent.selection)
|
|
|
|
})}
|
2022-09-18 01:02:25 +02:00
|
|
|
{...(multiline && {
|
|
|
|
multiline,
|
|
|
|
numberOfLines: Platform.OS === 'android' ? 5 : undefined
|
|
|
|
})}
|
|
|
|
textAlignVertical='top'
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
|
2021-05-09 21:59:03 +02:00
|
|
|
{title ? (
|
2022-09-18 01:02:25 +02:00
|
|
|
<Animated.Text style={[animateTitle, { position: 'absolute', color: colors.secondary }]}>
|
2021-05-09 21:59:03 +02:00
|
|
|
{title}
|
|
|
|
</Animated.Text>
|
|
|
|
) : null}
|
2022-09-18 01:02:25 +02:00
|
|
|
|
2021-05-23 22:53:06 +02:00
|
|
|
<View style={{ flexDirection: 'row', alignSelf: 'flex-end' }}>
|
2022-09-18 01:02:25 +02:00
|
|
|
{props?.maxLength && value?.length ? (
|
2022-05-07 00:52:32 +02:00
|
|
|
<CustomText
|
|
|
|
fontStyle='S'
|
|
|
|
style={{
|
|
|
|
paddingLeft: StyleConstants.Spacing.XS,
|
|
|
|
color: colors.secondary
|
|
|
|
}}
|
|
|
|
>
|
2022-09-18 01:02:25 +02:00
|
|
|
{value?.length} / {props.maxLength}
|
2022-05-07 00:52:32 +02:00
|
|
|
</CustomText>
|
2021-05-10 23:41:48 +02:00
|
|
|
) : null}
|
|
|
|
</View>
|
2021-05-09 21:59:03 +02:00
|
|
|
</View>
|
2022-09-18 01:02:25 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2021-05-09 21:59:03 +02:00
|
|
|
|
2022-09-18 01:02:25 +02:00
|
|
|
export default ComponentInput
|