-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathfactories.js
134 lines (113 loc) · 5.15 KB
/
factories.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import _ from 'lodash'
import cx from 'classnames'
import React, { cloneElement, isValidElement } from 'react'
// ============================================================
// Factories
// ============================================================
/**
* A more robust React.createElement. It can create elements from primitive values.
*
* @param {function|string} Component A ReactClass or string
* @param {function} mapValueToProps A function that maps a primitive value to the Component props
* @param {string|object|function} val The value to create a ReactElement from
* @param {Object} [options={}]
* @param {object} [options.defaultProps={}] Default props object
* @param {object|function} [options.overrideProps={}] Override props object or function (called with regular props)
* @returns {object|null}
*/
export function createShorthand(Component, mapValueToProps, val, options = {}) {
if (typeof Component !== 'function' && typeof Component !== 'string') {
throw new Error('createShorthandFactory() Component must be a string or function.')
}
// short circuit noop values
if (_.isNil(val) || _.isBoolean(val)) return null
const valIsString = _.isString(val)
const valIsNumber = _.isNumber(val)
const isReactElement = isValidElement(val)
const isPropsObject = _.isPlainObject(val)
const isPrimitiveValue = valIsString || valIsNumber || _.isArray(val)
// unhandled type return null
/* eslint-disable no-console */
if (!isReactElement && !isPropsObject && !isPrimitiveValue) {
if (process.env.NODE_ENV !== 'production') {
console.error([
'Shorthand value must be a string|number|array|object|ReactElement.',
' Use null|undefined|boolean for none',
` Received ${typeof val}.`,
].join(''))
}
return null
}
/* eslint-enable no-console */
// ----------------------------------------
// Build up props
// ----------------------------------------
const { defaultProps = {} } = options
// User's props
const usersProps = (isReactElement && val.props)
|| (isPropsObject && val)
|| (isPrimitiveValue && mapValueToProps(val))
// Override props
let { overrideProps = {} } = options
overrideProps = _.isFunction(overrideProps) ? overrideProps({ ...defaultProps, ...usersProps }) : overrideProps
// Merge props
/* eslint-disable react/prop-types */
const props = { ...defaultProps, ...usersProps, ...overrideProps }
// Merge className
if (defaultProps.className || overrideProps.className || usersProps.className) {
const mergedClassesNames = cx(defaultProps.className, overrideProps.className, usersProps.className)
props.className = _.uniq(mergedClassesNames.split(' ')).join(' ')
}
// Merge style
if (defaultProps.style || overrideProps.style || usersProps.style) {
props.style = { ...defaultProps.style, ...usersProps.style, ...overrideProps.style }
}
// ----------------------------------------
// Get key
// ----------------------------------------
// Use key, childKey, or generate key
if (_.isNil(props.key)) {
const { childKey } = props
if (!_.isNil(childKey)) {
// apply and consume the childKey
props.key = typeof childKey === 'function' ? childKey(props) : childKey
delete props.childKey
} else if (valIsString || valIsNumber) {
// use string/number shorthand values as the key
props.key = val
}
}
/* eslint-enable react/prop-types */
// ----------------------------------------
// Create Element
// ----------------------------------------
// Clone ReactElements
if (isReactElement) return cloneElement(val, props)
// Create ReactElements from built up props
if (isPrimitiveValue || isPropsObject) return <Component {...props} />
}
// ============================================================
// Factory Creators
// ============================================================
/**
* Creates a `createShorthand` function that is waiting for a value and options.
*
* @param {function|string} Component A ReactClass or string
* @param {function} mapValueToProps A function that maps a primitive value to the Component props
* @returns {function} A shorthand factory function waiting for `val` and `defaultProps`.
*/
export function createShorthandFactory(Component, mapValueToProps) {
if (typeof Component !== 'function' && typeof Component !== 'string') {
throw new Error('createShorthandFactory() Component must be a string or function.')
}
return (val, options) => createShorthand(Component, mapValueToProps, val, options)
}
// ============================================================
// HTML Factories
// ============================================================
export const createHTMLDivision = createShorthandFactory('div', val => ({ children: val }))
export const createHTMLIframe = createShorthandFactory('iframe', src => ({ src }))
export const createHTMLImage = createShorthandFactory('img', val => ({ src: val }))
export const createHTMLInput = createShorthandFactory('input', val => ({ type: val }))
export const createHTMLLabel = createShorthandFactory('label', val => ({ children: val }))
export const createHTMLParagraph = createShorthandFactory('p', val => ({ children: val }))