Skip to content

Commit

Permalink
Remove random colors, apply props styles to parent and child.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmiecik committed Nov 9, 2021
1 parent 3906304 commit 83db2da
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from "react";
import { SafeAreaView, ScrollView, StyleSheet, View, ViewStyle } from "react-native";
import {SafeAreaView, ScrollView, StyleProp, StyleSheet, View, ViewStyle} from "react-native";

export type DataStructure = {
id: number;
Expand All @@ -11,7 +11,7 @@ type PillProps<T extends DataStructure> = {
level?: number;
selectedItemId: T["id"][] | null | undefined;
buttonComponent: (data: DataStructure, level: number) => React.ReactNode;
containerStyle?: (level: number) => React.CSSProperties | ViewStyle
containerStyle?: (level: number) => StyleProp<ViewStyle>
treeData: T[];
};

Expand All @@ -34,7 +34,10 @@ export default function Pill<T extends DataStructure>(props: PillProps<T>) {
return (
<React.Fragment key={`${level}-${item.id}`}>
<View
style={isSelected && hasChildren ? { marginBottom: height } : undefined}
style={[
isSelected && hasChildren ? { marginBottom: height } : undefined,
props.containerStyle ? props.containerStyle(level) : undefined
]}
onLayout={(event) => {
const { x, y, height, width } = event.nativeEvent.layout;

Expand Down Expand Up @@ -62,7 +65,7 @@ export default function Pill<T extends DataStructure>(props: PillProps<T>) {
(itemMeasure[item.id]?.y ?? 0),
}}
>
<View style={{ backgroundColor: getRandomColor(level) }}>
<View style={props.containerStyle ? props.containerStyle(level) : undefined}>
{item.children ? (
<Pill
level={props.level ?? level + 1}
Expand All @@ -83,18 +86,6 @@ export default function Pill<T extends DataStructure>(props: PillProps<T>) {
);
}

function getRandomColor(level: number) {
return (
"rgb(" +
Math.floor((level / 100) * 256) +
"," +
Math.floor((level / 10) * 256) +
"," +
Math.floor((level / 10) * 256) +
")"
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
Expand Down

0 comments on commit 83db2da

Please sign in to comment.