import React, { useEffect, useRef } from 'react';
import { Animated, Easing, Pressable, View } from 'react-native';
import { Icon, IconName, SemanticColor, Typo } from '../../foundation';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { NotificationCount } from '../notificationCount/NotificationCount';
import { NotificationDot } from '../notificationDot/NotificationDot';
/**
* key에는 ScreenKeyEnum을 사용
*/
export type BottomTabDescriptor = {
[key: string]: BottomNavigationDescriptor;
};
/**
* BottomNavigationBar 컴포넌트에 전달할 탭 정보를 담은 배열 타입
* Screen의 route 정보와 필요한 아이콘,탭 정보를 조합
*/
type BottomTabList = Array<
{
routeKey: string;
routeName: string;
} & BottomNavigationDescriptor
>;
/**
* 탭 라벨과 아이콘, 알림 카운트, 알림 뱃지(닷)를 정의하기 위한 타입
*/
type BottomNavigationDescriptor = {
label: string;
count?: number;
showDot?: boolean;
defaultIconName: IconName;
focusedIconName: IconName;
};
type BottomNavigationBarProps = {
/**
* 탭 정보를 담은 배열
*/
bottomTabList: BottomTabList;
/**
* 탭 선택시 이벤트
*/
onPress: (args: { routeKey: string; routeName: string; isFocused: boolean }) => void;
/**
* 현재 선택된 탭의 index
*/
focusedIndex: number;
};
/**
* 바텀 네비게이션 바
*
* 실제 사용 예시는 BottomNavigationBarStory.tsx 파일 하단에 주석 내용을 참고해 주세요.
* BottomNavigation 사용 예시:
* 1. '@react-navigation/bottom-tabs' 설치합니다.
* 2. 'createBottomTabNavigator'를 사용하여 ${FooTab} 탭 네비게이션을 생성합니다.
* 3. 내부에 을 추가합니다.
* 4. 내부에 tabBar 속성을 추가하고, BottomNavigationBar 컴포넌트를 호출합니다.
* 5. tabBar 속성 반환 받는 props state 이용하여 BottomNavigationBar에 필요한 정보를 전달합니다.
* - 5-1. bottomTabList: props.state.routes 배열을 순회하면서 필요한 라벨,아이콘 정보를 담은 배열을 생성하여 props로 전달합니다.
* - 5-2. onPress: 탭 선택시 이벤트를 처리할 함수를 정의합니다.
* - 5-3. focusedIndex: props.state.index를 사용해 현재 선택된 탭의 index를 전달합니다.
*/
export function BottomNavigationBar({ bottomTabList, onPress, focusedIndex }: BottomNavigationBarProps) {
const { bottom } = useSafeAreaInsets();
return (
{bottomTabList.map((tab, index) => {
const isFocused = focusedIndex === index;
return (
onPress({ routeKey: tab.routeKey, routeName: tab.routeName, isFocused: isFocused })
}
/>
);
})}
);
}
type BottomNavigationTabProps = {
/**
* 알림 개수
*/
count?: number;
/**
* 알림 뱃지 표시 여부
*/
showDot?: boolean;
/**
* 탭 라벨 텍스트
*/
label: string;
/**
* 기본 아이콘 이름
*/
defaultIconName: IconName;
/**
* 선택시 보여질 아이콘 이름
*/
focusedIconName: IconName;
/**
* 탭 선택 여부
*/
isFocused: boolean;
/**
* 탭 선택시 동작
*/
onPress: () => void;
};
/**
* BottomNavigation 정의시 사용하는 작은 단위의 tab 요소 컴포넌트입니다.
*/
function BottomNavigationTab({
count = 0,
showDot = false,
label,
isFocused,
onPress,
defaultIconName,
focusedIconName,
}: BottomNavigationTabProps) {
return (
{label}
);
}
/**
* BottomNavigationTab 컴포넌트 내부에서 사용하는 아이콘 컴포넌트입니다.
*/
function BottomNavigationIcon({
isFocused,
defaultIconName,
selectedIconName,
count,
showDot,
}: {
isFocused: boolean;
defaultIconName: IconName;
selectedIconName: IconName;
count: number;
showDot: boolean;
}) {
const scaleAnimationRef = useRef(new Animated.Value(0));
useEffect(() => {
Animated.timing(scaleAnimationRef.current, {
toValue: isFocused ? 2 : 0,
duration: 200,
easing: Easing.linear,
useNativeDriver: true,
}).start(() => {
scaleAnimationRef.current.setValue(0);
});
}, [isFocused]);
const color = isFocused ? 'text_primary' : 'text_secondary';
const iconName = isFocused ? selectedIconName : defaultIconName;
return (
{showDot && }
);
}