-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIf.js
51 lines (45 loc) · 1.28 KB
/
If.js
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
import React, { Children } from 'react'
import PropTypes from 'prop-types'
import bind from 'ramda/src/bind'
import ifElse from 'ramda/src/ifElse'
import propSatisfies from 'ramda/src/propSatisfies'
import converge from 'ramda/src/converge'
import prop from 'ramda/src/prop'
import pipe from 'ramda/src/pipe'
import is from 'ramda/src/is'
import equals from 'ramda/src/equals'
import always from 'ramda/src/always'
import call from 'ramda/src/call'
import { View } from 'react-native'
const count = bind(Children.count, Children)
const If = ifElse(
// TODO: Evaluate predicate function with props
/*
If predicate is function, evaluate it, otherwise, return as is (will be either truthy ot falsy).
*/
ifElse(
propSatisfies(is(Function), 'predicate'),
converge(call, [prop('predicate')]),
prop('predicate')
),
/*
If predicate is truthy, evaulate this block with props.
If single child, return as is, otherwise wrap in a view.
*/
ifElse(
propSatisfies(pipe(count, equals(1)), 'children'),
prop('children'),
pipe(prop('children'), children => <View>{children}</View>)
),
/*
If predicate is falsy, always return null
*/
always(null)
)
If.propTypes = {
predicate: PropTypes.any
}
If.defaultProps = {
predicate: false
}
export default If