import React, { useCallback, useMemo } from "react"; import { FlipColors } from "@flip.id/ui-kit"; import type { IButtonProps, IFlexProps } from "native-base"; import type { IColors } from "native-base/lib/typescript/theme/base/colors"; import { AccessibilityProps, Pressable, View } from "react-native"; import KeyPad from "./components/KeyPad"; import { F2Text } from "../../Flip2/components"; import { StyleUtils } from "../../Flip2/utils"; interface KeyboardNumpadProps { onPressSubmit?: () => void; onPressDelete?: () => void; onPressKey?: (item: string) => void; submitColor?: string | IColors; isHideDelete?: boolean; isHideSubmit?: boolean; shadow?: IFlexProps["shadow"]; as?: React.ComponentType; accessibilityLabelKeypad?: string; disabled?: boolean; } interface KeyboardNumericProps { swapDeleteSubmit?: boolean; testID?: string; testIDKeypad?: string; keyboardShouldPersistTaps?: boolean; submitLabel?: string; submitButtonProps?: IButtonProps; hasThousandDigits?: boolean; SubmitComponent?: () => React.ReactElement; } const widthThird = Math.floor(StyleUtils.getScreenWidth() / 3) - 16; export const KeyboardNumeric: React.FC< KeyboardNumericProps & KeyboardNumpadProps & AccessibilityProps > = (props) => { const { swapDeleteSubmit, testID, testIDKeypad, onPressKey, onPressSubmit, onPressDelete, isHideDelete, isHideSubmit, as, shadow, accessibilityLabelKeypad, submitLabel, submitButtonProps, hasThousandDigits, disabled, SubmitComponent, ...rest } = props; const defaultHeight = StyleUtils.getScreenHeight() > StyleUtils.IPHONE_7_HEIGHT ? 52 : 46; const data = useMemo( () => [ "1", "2", "3", "4", "5", "6", "7", "8", "9", swapDeleteSubmit ? "submit" : "delete", "0", swapDeleteSubmit ? "delete" : "submit", ], [swapDeleteSubmit] ); const onPress = useCallback( (item: string) => { if (item === "delete") { return onPressDelete?.(); } if (item === "submit") { return onPressSubmit?.(); } return onPressKey?.(item); }, [onPressDelete, onPressSubmit, onPressKey] ); return ( {data.map((item) => { if (isHideDelete && item === "delete") { return ( ); } if ((isHideSubmit || submitLabel) && item === "submit") { if (hasThousandDigits) { item = "000"; } else { return ( ); } } return ( ); })} {submitLabel ? ( SubmitComponent ? ( ) : ( [ { width: "100%", marginTop: 4, borderWidth: 1.5, borderColor: pressed ? FlipColors.flipOrange.Plus2 : FlipColors.flipOrange.Main, borderRadius: 26, height: defaultHeight, alignItems: "center", justifyContent: "center", }, ]} onPress={onPressSubmit} {...submitButtonProps} > {({ pressed }) => ( {submitLabel} )} ) ) : null} ); }; export default React.memo(KeyboardNumeric);