-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathfactories.js
186 lines (160 loc) · 6.37 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import cx from 'clsx'
import _ from 'lodash'
import * as React from 'react'
import * as ReactIs from 'react-is'
const DEPRECATED_CALLS = {}
// ============================================================
// 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)
* @param {boolean} [options.autoGenerateKey=true] Whether or not automatic key generation is allowed
* @returns {object|null}
*/
export function createShorthand(Component, mapValueToProps, val, options = {}) {
if (!ReactIs.isValidElementType(Component)) {
throw new Error('createShorthand(): Component should be a valid element type.')
}
// short circuit noop values
if (_.isNil(val) || _.isBoolean(val)) {
return null
}
const valIsString = _.isString(val)
const valIsNumber = _.isNumber(val)
const valIsFunction = _.isFunction(val)
const valIsReactElement = React.isValidElement(val)
const valIsPropsObject = _.isPlainObject(val)
const valIsPrimitiveValue = valIsString || valIsNumber || _.isArray(val)
// unhandled type return null
/* eslint-disable no-console */
if (!valIsFunction && !valIsReactElement && !valIsPropsObject && !valIsPrimitiveValue) {
if (process.env.NODE_ENV !== 'production') {
console.error(
[
'Shorthand value must be a string|number|array|object|ReactElement|function.',
' 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 =
(valIsReactElement && val.props) ||
(valIsPropsObject && val) ||
(valIsPrimitiveValue && 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
const { autoGenerateKey = true } = options
if (!_.isNil(childKey)) {
// apply and consume the childKey
props.key = typeof childKey === 'function' ? childKey(props) : childKey
delete props.childKey
} else if (autoGenerateKey && (valIsString || valIsNumber)) {
// use string/number shorthand values as the key
props.key = val
}
}
// ----------------------------------------
// Create Element
// ----------------------------------------
// Clone ReactElements
if (valIsReactElement) {
return React.cloneElement(val, props)
}
if (typeof props.children === 'function') {
return props.children(Component, { ...props, children: undefined })
}
// Create ReactElements from built up props
if (valIsPrimitiveValue || valIsPropsObject) {
return React.createElement(Component, props)
}
// Call functions with args similar to createElement()
// TODO: V3 remove the implementation
if (valIsFunction) {
if (process.env.NODE_ENV !== 'production') {
if (!DEPRECATED_CALLS[Component]) {
DEPRECATED_CALLS[Component] = true
// eslint-disable-next-line no-console
console.warn(
`Warning: There is a deprecated shorthand function usage for "${Component}". It is deprecated and will be removed in v3 release. Please follow our upgrade guide: https://github.com/Semantic-Org/Semantic-UI-React/pull/4029`,
)
}
}
return val(Component, props, props.children)
}
/* eslint-enable react/prop-types */
}
// ============================================================
// 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 (!ReactIs.isValidElementType(Component)) {
throw new Error('createShorthandFactory(): Component should be a valid element type.')
}
return (val, options) => createShorthand(Component, mapValueToProps, val, options)
}
// ============================================================
// HTML Factories
// ============================================================
export const createHTMLDivision = /* #__PURE__ */ createShorthandFactory('div', (val) => ({
children: val,
}))
export const createHTMLIframe = /* #__PURE__ */ createShorthandFactory('iframe', (src) => ({ src }))
export const createHTMLImage = /* #__PURE__ */ createShorthandFactory('img', (val) => ({
src: val,
}))
export const createHTMLInput = /* #__PURE__ */ createShorthandFactory('input', (val) => ({
type: val,
}))
export const createHTMLLabel = /* #__PURE__ */ createShorthandFactory('label', (val) => ({
children: val,
}))
export const createHTMLParagraph = /* #__PURE__ */ createShorthandFactory('p', (val) => ({
children: val,
}))