Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/components/AlphabetList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { styles } from "./styles";
import { sizes } from "../../values/sizes";
import { DEFAULT_CHAR_INDEX } from "../../values/consts"

export const AlphabetList: React.FC<AlphabetListProps> = (props) => {
export const AlphabetList: React.FC<AlphabetListProps<any>> = (props) => {
const {
data,
index = DEFAULT_CHAR_INDEX,
Expand All @@ -30,7 +30,7 @@ export const AlphabetList: React.FC<AlphabetListProps> = (props) => {
} = props

const sectionListRef = useRef(null);
const [sectionData, setSectionData] = useState<ISectionData[]>([])
const [sectionData, setSectionData] = useState<ISectionData<any>[]>([])

useEffect(() => {
setSectionData(getSectionData(data, index, uncategorizedAtTop))
Expand All @@ -56,7 +56,7 @@ export const AlphabetList: React.FC<AlphabetListProps> = (props) => {
listHeaderHeight,
});

const onRenderSectionHeader = ({ section }: { section: SectionListData<IData> }) => {
const onRenderSectionHeader = ({ section }: { section: SectionListData<IData<any>> }) => {
if (renderCustomSectionHeader) return renderCustomSectionHeader(section);

return (
Expand All @@ -66,7 +66,7 @@ export const AlphabetList: React.FC<AlphabetListProps> = (props) => {
);
};

const onRenderItem = ({ item }: { item: IData }) => {
const onRenderItem = ({ item }: { item: IData<any> }) => {
if (renderCustomItem) return renderCustomItem(item);

return (
Expand All @@ -84,7 +84,7 @@ export const AlphabetList: React.FC<AlphabetListProps> = (props) => {
testID="sectionList"
ref={sectionListRef}
sections={sectionData}
keyExtractor={(item: IData) => item.key}
keyExtractor={(item: IData<any>) => item.key}
renderItem={onRenderItem}
renderSectionHeader={onRenderSectionHeader}
ListHeaderComponent={renderCustomListHeader}
Expand Down
22 changes: 11 additions & 11 deletions src/components/AlphabetList/types.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { SectionListData, SectionListProps, TextStyle, ViewStyle } from "react-native";

export interface IData {
value: string;
export interface IData<Type> {
value: Type;
key: string;
}

export interface ISectionData {
export interface ISectionData<Type> {
title: string;
data: IData[];
data: IData<Type>[];
index?: number;
}
export interface IIndexLetterProps {
item: ISectionData,
export interface IIndexLetterProps<Type> {
item: ISectionData<Type>,
index: number,
onPress: () => void;
}

export interface AlphabetListProps extends Partial<SectionListProps<IData>> {
data: IData[];
export interface AlphabetListProps<Type> extends Partial<SectionListProps<IData<Type>>> {
data: IData<Type>[];
index?: string[],
style?: ViewStyle;
indexLetterStyle?: TextStyle,
indexLetterContainerStyle?: ViewStyle,
indexContainerStyle?: ViewStyle,
letterListContainerStyle?: ViewStyle,
renderCustomItem?: (item: IData) => JSX.Element;
renderCustomSectionHeader?: (section: SectionListData<IData>) => JSX.Element;
renderCustomItem?: (item: IData<Type>) => JSX.Element;
renderCustomSectionHeader?: (section: SectionListData<IData<Type>>) => JSX.Element;
renderCustomListHeader?: () => JSX.Element;
renderCustomIndexLetter?: ({ item, index, onPress }: IIndexLetterProps) => JSX.Element;
renderCustomIndexLetter?: ({ item, index, onPress }: IIndexLetterProps<Type>) => JSX.Element;
getItemHeight?: (sectionIndex: number, rowIndex: number) => number;
sectionHeaderHeight?: number;
listHeaderHeight?: number;
Expand Down
4 changes: 2 additions & 2 deletions src/components/ListLetterIndex/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ListLetterIndexProps } from "./types"
import { styles } from "./styles";
import { ISectionData } from "../AlphabetList/types";

export const ListLetterIndex: React.FC<ListLetterIndexProps> = ({
export const ListLetterIndex: React.FC<ListLetterIndexProps<any>> = ({
sectionData,
onPressLetter,
indexContainerStyle,
Expand All @@ -13,7 +13,7 @@ export const ListLetterIndex: React.FC<ListLetterIndexProps> = ({
renderCustomIndexLetter,
letterListContainerStyle
}) => {
const onRenderCustomIndexLetter = ({ item, index }: { item: ISectionData, index: number }) => {
const onRenderCustomIndexLetter = ({ item, index }: { item: ISectionData<any>, index: number }) => {
const onPress = () => onPressLetter(index)

if (renderCustomIndexLetter) {
Expand Down
14 changes: 7 additions & 7 deletions src/components/ListLetterIndex/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AlphabetListProps, ISectionData } from "../AlphabetList/types"

export interface ListLetterIndexProps {
export interface ListLetterIndexProps<Type> {
onPressLetter: (sectionIndex: number) => void;
sectionData: ISectionData[];
indexContainerStyle?: AlphabetListProps["indexContainerStyle"],
indexLetterStyle?: AlphabetListProps["indexLetterStyle"],
indexLetterContainerStyle?: AlphabetListProps["indexLetterContainerStyle"],
renderCustomIndexLetter?: AlphabetListProps["renderCustomIndexLetter"],
letterListContainerStyle?: AlphabetListProps["letterListContainerStyle"]
sectionData: ISectionData<Type>[];
indexContainerStyle?: AlphabetListProps<Type>["indexContainerStyle"],
indexLetterStyle?: AlphabetListProps<Type>["indexLetterStyle"],
indexLetterContainerStyle?: AlphabetListProps<Type>["indexLetterContainerStyle"],
renderCustomIndexLetter?: AlphabetListProps<Type>["renderCustomIndexLetter"],
letterListContainerStyle?: AlphabetListProps<Type>["letterListContainerStyle"]
}
12 changes: 6 additions & 6 deletions src/utils/getSectionData.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { ISectionData, IData } from "../components/AlphabetList/types";

interface IAlphabetSet {
[key: string]: IData[];
[key: string]: IData<any>[];
}

interface IEntry {
title: string;
data: IData[];
data: IData<any>[];
}

interface ILetterMap {
[key: string]: number
}

export const getSectionData = (data: IData[], charIndex: string[], uncategorizedAtTop = false) => {
export const getSectionData = (data: IData<any>[], charIndex: string[], uncategorizedAtTop = false) => {
const validLettersMap = getValidLettersMap(charIndex)
const alphabetEntrySet: [string, IData[]][] = getAlphabetEntrySet(data, validLettersMap);
const alphabetEntrySet: [string, IData<any>[]][] = getAlphabetEntrySet(data, validLettersMap);

return alphabetEntrySet
.map(formatEntry)
.sort((a, b) => sortSectionsByCharIndex(a, b, validLettersMap, uncategorizedAtTop))
.map((section: ISectionData, index: number) => ({ ...section, index }));
.map((section: ISectionData<any>, index: number) => ({ ...section, index }));
};

const getValidLettersMap = (letterMap: string[]) => {
Expand All @@ -33,7 +33,7 @@ const getValidLettersMap = (letterMap: string[]) => {
return map
}

const getAlphabetEntrySet = (data: IData[], validLettersMap: ILetterMap) => {
const getAlphabetEntrySet = (data: IData<any>[], validLettersMap: ILetterMap) => {
const alphabetSet: IAlphabetSet = {}

data.forEach((item) => {
Expand Down