Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keyboard avoiding input field #43

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 36 additions & 30 deletions components/qna/input.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useRef, useState } from 'react';
import { TextInput, View } from 'react-native';
import { KeyboardAvoidingView, TextInput, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';

import { extendedColors } from '../../theme/extendedColors';
import { useKeyboardOffset } from '../../utils/keyboard.utils';
import { StyledButton } from '../common/styled-button';

interface InputProps {
Expand All @@ -15,43 +15,49 @@ const MIN_CHARACTERS_TO_SEND = 5;

export function Input({ placeholder, onSubmit, disabled = false }: InputProps) {
const ref = useRef<TextInput>(null);
const insets = useSafeAreaInsets();
const [value, setValue] = useState('');
const keyboardOffset = useKeyboardOffset();

const onSend = () => {
if (value.length < MIN_CHARACTERS_TO_SEND) return;
ref.current?.clear();
onSubmit(value);
setValue('');
};

const isDisabled = value.length < MIN_CHARACTERS_TO_SEND || disabled;

return (
<View
style={{
bottom: Math.max(130, keyboardOffset + 16),
}}
className='absolute left-0 right-0 mx-5 flex-row space-x-3 rounded-xl bg-white dark:bg-slate-800 px-3 py-2 shadow-md max-h-60'
<KeyboardAvoidingView
keyboardVerticalOffset={0}
behavior='position'
style={{ paddingBottom: insets.bottom + 100 }}
className='justify-end absolute w-full h-full'
enabled
>
<TextInput
ref={ref}
returnKeyType='send'
placeholderTextColor={extendedColors.slate['500'] + '80'}
autoCapitalize='sentences'
textAlignVertical='center'
blurOnSubmit
multiline
className='flex-1 text-slate-900 dark:text-white font-raleway-regular self-center'
placeholder={placeholder}
value={value}
onChange={(e) => setValue(e.nativeEvent.text)}
onSubmitEditing={onSend}
editable={!disabled}
/>
<StyledButton
disabled={isDisabled}
className='rounded-full p-1 h-8 w-8 self-end'
leftIcon='arrow-up'
onPress={onSend}
/>
</View>
<View className='mx-5 flex-row space-x-3 rounded-xl bg-white dark:bg-slate-800 px-3 py-2 shadow-md max-h-60'>
<TextInput
ref={ref}
returnKeyType='send'
placeholderTextColor={extendedColors.slate['500'] + '80'}
autoCapitalize='sentences'
textAlignVertical='center'
blurOnSubmit
multiline
className='flex-1 text-slate-900 dark:text-white font-raleway-regular self-center'
placeholder={placeholder}
value={value}
onChange={(e) => setValue(e.nativeEvent.text)}
onSubmitEditing={onSend}
editable={!disabled}
/>
<StyledButton
disabled={isDisabled}
className='rounded-full p-1 h-8 w-8 self-end'
leftIcon='arrow-up'
onPress={onSend}
/>
</View>
</KeyboardAvoidingView>
);
}
Loading