-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.tsx
More file actions
97 lines (79 loc) · 2.28 KB
/
Copy pathStack.tsx
File metadata and controls
97 lines (79 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React, { useCallback, useMemo } from 'react'
import { StyleSheet, View, ViewProps } from 'react-native'
import randomColor from 'randomcolor'
import { useStack } from '../hooks'
import { preparePaddings } from '../helpers/preparePaddings'
import { BaseProps } from '../types/BaseProps'
interface Props extends ViewProps {}
// TODO
export const Stack = ({ children, style, spaces,
padding, ...props }: Props) => {
const globalConfig = useStack()
// TODO refactor
const stackSpaces = useMemo(
() => spaces ?? globalConfig.spaces,
[globalConfig.spaces, spaces]
)
const stackPadding = useMemo(
() => padding ?? globalConfig.padding,
[globalConfig.padding, padding]
)
const stackOmitNull = useMemo(
() => omitNull ?? globalConfig.omitNull,
[globalConfig.omitNull, omitNull]
)
const isSpacer = useMemo(
() => typeof stackSpaces === 'number' || typeof stackSpaces === 'string',
[stackSpaces]
)
const divider = useCallback(
(): React.ReactElement => (
<View
style={StyleSheet.flatten([
{
minWidth: stackSpaces,
minHeight: stackSpaces,
},
// TODO refactor
globalConfig.debug && {
backgroundColor: globalConfig.debugColor || randomColor(),
},
])}
/>
),
[globalConfig.debug, globalConfig.debugColor, stackSpaces]
)
const addSpaces = useCallback(
(index: number) => {
if (!isSpacer) return []
return React.cloneElement(divider, {
key: `stack-divider-${index}`,
})
},
[isSpacer, divider]
)
const stack = useMemo(() => {
let elements = Array.isArray(children) ? children : [children]
elements = elements.filter((child) => {
return Array.isArray(child) || React.isValidElement(child)
})
return elements.reduce((children: React.ReactNodeArray, child, index) => {
if (children.length === 0) {
return [child]
}
// TODO refactor
if (
(stackOmitNull && child === null) ||
(child as any)?.type === React.Fragment
) {
return [...children, child]
}
return [...children, addSpaces(index), child]
}, [])
}, [addSpaces, children, stackOmitNull])
return (
<View>
{stack}
</View>
)
}