import type { ReactNode } from "react";
import React from "react";
import type {
LayoutChangeEvent,
PressableStateCallbackType,
} from "react-native";
import { Pressable, View } from "react-native";
import type { IconNames } from "@jobber/design";
import type { XOR } from "ts-xor";
import { useStyles } from "./Card.style";
import { InternalCardHeader } from "./components/InternalCardHeader";
import { ErrorMessageWrapper } from "../ErrorMessageWrapper";
import { ActionLabel } from "../ActionLabel";
import { Typography } from "../Typography";
import { Icon } from "../Icon";
interface CardProps {
/**
* @deprecated Use with the title and onPress properties instead
*/
readonly header?: HeaderProps;
readonly footer?: FooterProps;
readonly children?: ReactNode;
readonly reportCardHeight?: (height: number) => void;
readonly testID?: string;
readonly error?: string;
readonly elevation?: elevationProp;
}
type elevationProp = "none" | "low" | "base" | "high";
export type HeaderProps = HeaderCommonProps & HeaderActionProps;
interface FooterProps {
readonly onPress: () => void;
readonly title: string;
}
interface HeaderCommonProps {
readonly title: string;
}
type HeaderActionProps =
| {
readonly onPress?: never;
readonly actionItem?: never;
}
| {
readonly onPress: () => void;
readonly actionItem: CardAction;
};
interface IconAction {
readonly iconName: IconNames;
}
interface ButtonAction {
readonly label: string;
}
export type CardAction = XOR;
function getElevationStyle(
elevation: elevationProp,
styles: ReturnType,
) {
if (elevation === "none") {
return undefined;
}
return styles[`${elevation}Elevation`];
}
export function Card({
header,
footer,
children,
reportCardHeight: onCardHeightChange,
testID = "card",
error,
elevation = "none",
}: CardProps) {
const styles = useStyles();
return (
{header && (
{header.title}
{!!header.actionItem?.label && (
{header.actionItem.label}
)}
{header.actionItem?.iconName && (
)}
)}
{children}
{footer && (
[
styles.footer,
pressed && styles.pressed,
]}
accessibilityRole={"button"}
>
{footer.title}
)}
);
function handleLayoutChange(event: LayoutChangeEvent) {
const { height } = event.nativeEvent.layout;
onCardHeightChange?.(height);
}
}