All files / apps/reallocation/src/screens/ChooseReason index.tsx

100% Statements 15/15
100% Branches 6/6
100% Functions 4/4
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                                            1x 7x 7x   7x                           7x   7x 7x     7x 3x 2x   2x     2x       7x                                                                                     7x                                      
import React, { memo, useCallback } from 'react';
import { KeyboardAwareScrollView } from 'react-native-keyboard-controller';
import { Pressable, TextInput, View } from 'react-native';
 
import { useNavigation } from '@react-navigation/native';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
 
import { QuickSelect } from '@repo/ui/components/QuickSelect';
import { MoreIcon } from '@repo/ui/icons/More';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
import { textStyles } from '@repo/ui/themes/typography';
 
import { useReallocationStateSelector } from '../Reallocation/context';
import {
  type ReallocationStackParamList,
  ReallocationStep,
} from '../Reallocation/types';
 
import { useChooseReason } from './hooks';
import { quickReasonOptions } from './utils';
 
const ChooseReason = () => {
  const styles = useStyles();
  const { theme } = useTheme();
  const navigation =
    useNavigation<NativeStackNavigationProp<ReallocationStackParamList>>();
  const {
    state: {
      isOpenReasonInput,
      quickReason,
      reasonInputValue,
      canSubmitReasonInput,
    },
    actions: {
      setReasonInputValue,
      openReasonInput,
      handleChangeQuickReason,
      handleSubmitTextInput,
    },
  } = useChooseReason();
 
  const isSameOffice = useReallocationStateSelector(
    state => state.isSameOffice,
  );
 
  const handleSubmitEditing = useCallback(() => {
    if (canSubmitReasonInput) {
      handleSubmitTextInput();
      const nextStep =
        isSameOffice === false
          ? ReallocationStep.MEAL_REGISTRATION
          : ReallocationStep.PREVIEW;
      navigation.navigate(nextStep);
    }
  }, [canSubmitReasonInput, handleSubmitTextInput, isSameOffice, navigation]);
 
  return (
    <>
      {isOpenReasonInput ? (
        <KeyboardAwareScrollView keyboardShouldPersistTaps="handled">
          <TextInput
            autoFocus
            style={styles.input}
            value={reasonInputValue}
            submitBehavior="submit"
            returnKeyType="done"
            enablesReturnKeyAutomatically
            onChangeText={setReasonInputValue}
            multiline
            testID="reason-input"
            onSubmitEditing={handleSubmitEditing}
            accessibilityLabel="Reason for relocation"
          />
        </KeyboardAwareScrollView>
      ) : (
        <View style={styles.container}>
          <QuickSelect
            options={quickReasonOptions}
            value={quickReason}
            onChange={handleChangeQuickReason}
            testID="choose-reason-quick-select"
          />
          <Pressable
            accessibilityRole="button"
            accessibilityLabel="Open reason input"
            testID="more-btn"
            style={styles.moreIcon}
            onPress={openReasonInput}
          >
            <MoreIcon color={theme.colors.icon.brand} />
          </Pressable>
        </View>
      )}
    </>
  );
};
 
export default memo(ChooseReason);
 
const useStyles = makeStyles(theme => ({
  container: {
    flex: 1,
    paddingHorizontal: theme.metrics.spacing[4],
    gap: theme.metrics.spacing[6],
    backgroundColor: theme.colors.background.pageMuted,
  },
  moreIcon: {
    borderRadius: theme.metrics.borderRadius.pill,
    width: theme.metrics.spacing[15],
    height: theme.metrics.spacing[15],
  },
  input: {
    padding: theme.metrics.spacing[4],
    paddingTop: -theme.metrics.spacing[4],
    ...textStyles.content.regular,
    color: theme.colors.text.onBehalf,
  },
}));