import React, { PropsWithChildren, ReactElement } from 'react';
import { IconName } from '../../foundation';
import { ThrottleOptions } from '../../internal/utils/useThrottledPressHandler';
import { KeyboardStickyView } from 'react-native-keyboard-controller';
import { TwoButtonType } from './TwoButtonType';
import { KeyboardType, OneButtonFullType, OneButtonSolidType } from './OneButtonTypeComponents';
import { CustomButtonTypeContainer } from './CustomButtonTypeContainer';
import { View } from 'react-native';
import { useKeyboardWill } from '../../internal/utils/useKeyboardWill';
// 하나의 버튼으로 이루어짐
export interface OneButtonProp {
type: 'full' | 'solid';
label: string;
onPress: () => void;
disabled?: boolean;
loading?: boolean;
throttle?: ThrottleOptions['throttle'];
TopSlotComponent?: ReactElement;
absolute?: boolean;
disableKeyboardSticky?: boolean;
}
// 두 개의 버튼이 row 형태로 배치
export type TwoButtonProp = TwoButtonCommon &
(
| {
type: 'two_button_3to7' | 'two_button_5to5';
label: { primary: string; secondary: string };
}
| {
type: 'two_button_2to8';
label: { primary: string; secondary: IconName };
}
);
interface TwoButtonCommon {
onPress: { primary: () => void; secondary: () => void };
disabled?: { primary?: boolean; secondary?: boolean };
loading?: { primary?: boolean; secondary?: boolean };
throttle?: { primary?: ThrottleOptions['throttle']; secondary?: ThrottleOptions['throttle'] };
TopSlotComponent?: ReactElement;
absolute?: boolean;
}
// (참고) onPress, disable, loading, throttle 설정은 커스텀 컨텐츠 컴포넌트가 직접 구현해야 함
interface CustomButtonProp {
type: 'custom';
customContent: ReactElement;
TopSlotComponent?: ReactElement;
absolute?: boolean;
}
type Props = OneButtonProp | TwoButtonProp | CustomButtonProp;
/**
* ## Bottom Fixed Button 컴포넌트
*
* ## Props
*
* #### `type`
* - 버튼 한 개로 구성 : 'full' | 'solid'
* - 버튼 두 개로 구성 : 'two_button_3to7' | 'two_button_5to5' | 'two_button_2to8'
* - 버튼 영역 내용을 직접 구현 : 'custom'
* - 대부분의 prop을 지원하지 않으며, 필요한 기능은 커스텀 컴포넌트가 구현해야 합니다. 사용 방법은 예시 항목을 참고해주세요.
*
* #### `label`
* - 버튼 라벨
*
* #### `onPress`
* - 버튼 press 이벤트
*
* #### `disabled` (optional)
* - 버튼 비활성화
*
* #### `throttle` (optional)
* - 버튼 쓰로틀링 처리 여부
* - 기본값은 true (500ms 쓰로틀링 적용)
*
* #### `loading` (optional)
* - 버튼 로딩중 처리 (기본적으로는 비활성)
* - 로딩중에는 터치 비활성화.
*
* #### `customContent` (optional)
* - 'custom' 타입에서 사용. 예제 코드를 참고하세요.
*
* #### `TopSlotComponent` (optional)
* - 컴포넌트 상단에 렌더할 컴포넌트
* - 버튼 위에 붙어서 키보드 동작에 따라 밀접하게 움직여야 하는 뷰가 있으면 이 슬롯에 전달하세요.
* - 키보드 동작과 관련없이 버튼 위에 붙여 표시하고 싶은 경우라도 활용 가능합니다.
* - (주의) stateless 컴포넌트를 전달하세요. 키보드 올라오기 전/후 각기 다른 인스턴스를 그리기 떄문에 각자의 상태가 공유되지 않으며, 키보드 동작시 상태를 잃습니다.
*
* #### `disableKeyboardSticky` (optional)
* - 키보드가 올라올 때 버튼이 키보드 상단에 표시되지 않도록 설정할 수 있습니다.
* - 기본값 false
*
* #### `absolute` (optional)
* - 기본값 false
* - 컴포넌트 컨테이너를 absolute position 설정하고, 화면 하단에 배치합니다.
* - 함께 사용할 컨텐츠 컨테이너 컴포넌트(형제 컴포넌트)의 높이가 BFB 컴포넌트에 의해 **변경되지 않아야 할 때** 사용합니다.
*
*
* # 사용 예시
*
* @example
* ```tsx
* // 한 개의 버튼으로 구성된 타입들 //
*
*
*
*
*
*
* // 두 개의 버튼으로 구성된 타입들 //
*
*
*
*
*
*
* ```
*
*
* # 텍스트 인풋 + 키보드 핸들링 예제
*
* ### 주의
* - 컨텐츠 컨테이너로 `KeyboardAwareScrollView`를 사용해야 한다면 react-native-keyboard-controller 라이브러리에 포함된 것을 사용하세요.
* - `BottomFixedButton` 컴포넌트는 컨텐츠 컨테이너 바깥에 호출해야 합니다. (예제 코드 참고)
* - full/solid 타입이면 `bottomOffset` prop 설정 필요
* - `getBottomFixedButtonHeight('keyboard')` 호출하여 인풋창이 키보드 상단 버튼에 가려지지 않도록 합니다.
* - `disableKeyboardSticky` 옵션을 사용해 키보드 상단에 버튼이 올라오지 않으면 bottomOffset 설정이 필요치 않습니다.
* - `Screen` 등 부모 컴포넌트에서 RN `KeyboardAvoidingView` 사용시 정상 동작하지 않습니다.
*
* @example
* ```tsx
*
*
*
*
*
*
*
*
*
*
* // 키보드 올라올 때 키보드의 상단에 버튼이 붙지 않도록 하려면 //
*
* ```
*
*
* # 'custom' 타입 사용 예시
*
* ### BottomFixedButton 컴포넌트는 내용을 필요에 따라 변경할 수 있는 특수한 컴포넌트입니다.
*
* 피그마 시안에 따라 이러한 요구사항이 있을 수 있습니다.
* - SocialButton 같은 특수 버튼을 사용해야 할 때
* - 버튼 타입을 자유롭게 변경하여 사용해야 할 때
* - 두 버튼의 영역 비율을 조정해야 할 때
* - 등등
*
* @example
* ```tsx
*
*
*
*
*
*
* }
* />
* ```
*
*
* # `absolute` 속성 사용 예시
* - BottomFixedButton 컴포넌트로 인해 형제 컨텐츠 컴포넌트의 높이가 변하지 않아야 하는 경우 사용합니다.
*
* @example
* ```tsx
* import PagerView from 'react-native-pager-view';
*
*
*
*
*
*
* {isFirstPage ? : null}
*
* ```
*/
export function BottomFixedButton(props: Props) {
switch (props.type) {
case 'full':
case 'solid': {
const { TopSlotComponent, disableKeyboardSticky = false, ...oneButtonProps } = props;
return (
}
TopSlotComponent={TopSlotComponent}>
{oneButtonProps.type === 'full' ? (
) : (
)}
{TopSlotComponent}
);
}
case 'two_button_2to8':
case 'two_button_3to7':
case 'two_button_5to5': {
const { TopSlotComponent, ...twoButtonProps } = props;
return (
{TopSlotComponent}
);
}
case 'custom': {
return (
{props.customContent}
{props.TopSlotComponent}
);
}
}
}
/** 키보드 이벤트를 구독해 렌더링 분기 */
function KeyboardAware(
props: PropsWithChildren<{ keyboardType?: ReactElement } & Pick>,
): JSX.Element {
const { keyboardShown } = useKeyboardWill();
return (
<>
{props.children}
{/* 키보드가 올라오면 전달받은 keyboardType 컴포넌트를 표시하고, 키보드 내려오면 감춤 */}
{props.keyboardType ? (
{props.TopSlotComponent}
{props.keyboardType}
) : null}
>
);
}
function AbsoluteOrNot({ absolute = false, ...props }: PropsWithChildren<{ absolute?: boolean }>) {
if (absolute) {
return (
{props.children}
);
}
return <>{props.children}>;
}