Files
super-app/components/widgets/styled-text/StyledText.tsx
Rohitaksh Choudhary 3f471498ed NTP-8070 | Rohitaksh | RN integration iOS (#14407)
Signed-off-by: kishan kumar <kishan.kumar@navi.com>
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Chirayu <chirayu.mor@navi.com>
Co-authored-by: Prakhar Saxena <prakhar.saxena@navi.com>
Co-authored-by: Shivam Goyal <shivam.goyal@navi.com>
Co-authored-by: vedant aggarwal <vedant.aggarwal@navi.com>
Co-authored-by: A Shrihari Raju <shrihari.raju@navi.com>
Co-authored-by: Prajjaval Verma <prajjaval.verma@navi.com>
Co-authored-by: Raaj Gopal <raaj.gopal@navi.com>
Co-authored-by: Aman S <aman.s@navi.com>
Co-authored-by: Aman <amankasyapp@gmail.com>
Co-authored-by: Sanjay P <sanjay.p@navi.com>
Co-authored-by: Varun Jain <varun.jain@navi.com>
Co-authored-by: Shiv Natani <shiv.natani@navi.com>
Co-authored-by: Hardik Chaudhary <hardik.chaudhary@navi.com>
Co-authored-by: Kishan Kumar <kishan.kumar@navi.com>
Co-authored-by: Balrambhai Sharma <sharma.balrambhai@navi.com>
Co-authored-by: Ujjwal Kumar <ujjwal.kumar@navi.com>
Co-authored-by: Aditya Narayan Malik <aditya.narayan@navi.com>
Co-authored-by: Ayushman Sharma <ayushman.sharma@navi.com>
Co-authored-by: Anmol Agrawal <anmol.agrawal@navi.com>
Co-authored-by: Soumya Ranjan Patra <soumya.ranjan@navi.com>
Co-authored-by: Sohan Reddy Atukula <sohan.reddy@navi.com>
Co-authored-by: Sayed Owais Ali <sayed.owais@navi.com>
Co-authored-by: Ankit Yadav <ankit.yadav@navi.com>
Co-authored-by: Shaurya Rehan <shaurya.rehan@navi.com>
Co-authored-by: saksham-mahajan_navi <saksham.mahajan@navi.com>
Co-authored-by: shankar yadav <shankar.yadav@navi.com>
Co-authored-by: Mehul Garg <mehul.garg@navi.com>
Co-authored-by: Somarapu Vamshi <somarapu.vamshi@navi.com>
Co-authored-by: Kshitij Pramod Ghongadi <kshitij.pramod@navi.com>
Co-authored-by: Sandeep Kumar <sandeep.ku@navi.com>
Co-authored-by: Aparna Vadlamani <aparna.vadlamani@navi.com>
Co-authored-by: Siddiboina Susai <siddiboina.susai@navi.com>
Co-authored-by: Kamalesh Garnayak <kamalesh.garnayak@navi.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Divyesh Shinde <divyesh.shinde@navi.com>
Co-authored-by: Mohit Rajput <mohit.rajput@navi.com>
Co-authored-by: Akshita Singh <akshita.singh@navi.com>
Co-authored-by: shreyansu raj <shreyansu.raj@navi.com>
Co-authored-by: Venkat Praneeth Reddy <venkat.praneeth@navi.com>
2025-01-13 13:30:56 +00:00

213 lines
6.3 KiB
TypeScript

import React from "react";
import { Platform, TouchableOpacity, View } from "react-native";
import Animated from "react-native-reanimated";
import { GenericActionPayload } from "../../../App/common/actions/GenericAction";
import { CtaData } from "../../../App/common/interface";
import {
ImageFieldData,
LottieFieldData,
SubstringStyle,
TextFieldData,
} from "../../../App/common/interface/widgets/widgetData/TitleWidgetData";
import { StyledImage } from "../../StyledImage";
import { StyledLottie } from "../styled-lottie/StyledLottie";
import styles from "./StyledTextComponentStyle";
import { OsTypeConstants } from "../../../App/common/constants";
import { getMappedFontForIos } from "../../../App/common/utilities/MiscUtils";
export const StyledText = ({
textFieldData,
handleClick,
handleActions,
}: {
textFieldData: TextFieldData;
handleClick?: (ctaData: CtaData) => void;
handleActions?: (
value: any | undefined | null,
actionPayloadList: GenericActionPayload | undefined,
) => void;
}) => {
if (Platform.OS === OsTypeConstants.IOS) {
textFieldData.textStyle = {
...textFieldData.textStyle,
fontFamily: getMappedFontForIos(textFieldData.textStyle?.fontFamily),
};
}
let text: string = textFieldData.text;
let topImage: ImageFieldData | undefined =
textFieldData?.textDrawableData?.top;
let bottomImage: ImageFieldData | undefined =
textFieldData?.textDrawableData?.bottom;
let leftImage: ImageFieldData | undefined =
textFieldData?.textDrawableData?.left;
let rightImage: ImageFieldData | undefined =
textFieldData?.textDrawableData?.right;
let topLottie: LottieFieldData | undefined =
textFieldData?.textDrawableData?.topLottie;
let bottomLottie: LottieFieldData | undefined =
textFieldData?.textDrawableData?.bottomLottie;
let leftLottie: LottieFieldData | undefined =
textFieldData?.textDrawableData?.leftLottie;
let rightLottie: LottieFieldData | undefined =
textFieldData?.textDrawableData?.rightLottie;
return (
<View style={[styles.columnContainer, textFieldData.viewStyle]}>
{topImage && (
<StyledImage
imageFieldData={topImage}
handleClick={handleClick}
handleActions={handleActions}
/>
)}
{topLottie && (
<StyledLottie
lottieFieldData={topLottie}
handleClick={handleClick}
handleActions={handleActions}
/>
)}
<View style={styles.rowContainer}>
{leftImage && (
<StyledImage
imageFieldData={leftImage}
handleClick={handleClick}
handleActions={handleActions}
/>
)}
{leftLottie && (
<StyledLottie
lottieFieldData={leftLottie}
handleClick={handleClick}
handleActions={handleActions}
/>
)}
{text && (
<StyledTextComponent
textFieldData={textFieldData}
handleClick={handleClick}
handleActions={handleActions}
/>
)}
{rightImage && (
<StyledImage
imageFieldData={rightImage}
handleClick={handleClick}
handleActions={handleActions}
/>
)}
{rightLottie && (
<StyledLottie
lottieFieldData={rightLottie}
handleClick={handleClick}
handleActions={handleActions}
/>
)}
</View>
{bottomImage && (
<StyledImage
imageFieldData={bottomImage}
handleClick={handleClick}
handleActions={handleActions}
/>
)}
{bottomLottie && (
<StyledLottie
lottieFieldData={bottomLottie}
handleClick={handleClick}
handleActions={handleActions}
/>
)}
</View>
);
};
const StyledTextComponent = ({
textFieldData,
handleClick,
handleActions,
}: {
textFieldData: TextFieldData;
handleClick?: (ctaData: CtaData) => void;
handleActions?: (
value: any | undefined | null,
actionPayloadList: GenericActionPayload | undefined,
) => void;
}) => {
const { cta, actions } = textFieldData;
const handleTextClick = () => {
if (actions && handleActions) {
handleActions(null, actions);
} else if (cta && handleClick) {
handleClick(cta);
}
};
const isInteractive = (cta && handleClick) || (actions && handleActions);
return isInteractive ? (
<TouchableOpacity onPress={handleTextClick} activeOpacity={1}>
<TextComponent textFieldData={textFieldData} />
</TouchableOpacity>
) : (
<TextComponent textFieldData={textFieldData} />
);
};
const TextComponent = ({ textFieldData }: { textFieldData: TextFieldData }) => {
let substringStyleMap: SubstringStyle[] = [];
let textFieldString: string = textFieldData.text || "";
textFieldData.substringStyles?.forEach(({ substring, textStyle }, index) => {
textFieldString = substringStyleMap?.pop()?.substring || textFieldData.text;
if (Platform.OS === OsTypeConstants.IOS) {
textStyle = {
...textStyle,
fontFamily: getMappedFontForIos(textStyle?.fontFamily),
};
}
if (substring) {
const splitArray = textFieldString.split(substring);
const part1: SubstringStyle = {
substring: splitArray[0] || "",
};
const part2: SubstringStyle = {
substring: substring,
textStyle: textStyle,
};
const part3: SubstringStyle = {
substring: splitArray[1] || "",
};
substringStyleMap = [...substringStyleMap, part1, part2, part3];
} else {
const part1: SubstringStyle = {
substring: textFieldString,
};
substringStyleMap = [...substringStyleMap, part1];
}
});
return (
<Animated.Text
ellipsizeMode={textFieldData.ellipsizeMode}
numberOfLines={textFieldData.numberOfLines}
style={textFieldData.textStyle || undefined}
>
{substringStyleMap.length !== 0
? substringStyleMap.map((substringStyle, index) => {
return (
<Animated.Text
style={substringStyle.textStyle || undefined}
key={index}
>
{substringStyle.substring}
</Animated.Text>
);
})
: textFieldData.text}
</Animated.Text>
);
};