forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrapUtils.js
More file actions
183 lines (145 loc) · 4.17 KB
/
Copy pathbootstrapUtils.js
File metadata and controls
183 lines (145 loc) · 4.17 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
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
// TODO: The publicly exposed parts of this should be in lib/BootstrapUtils.
import invariant from 'invariant';
import { PropTypes } from 'react';
import { SIZE_MAP } from './StyleConfig';
function curry(fn) {
return (...args) => {
let last = args[args.length - 1];
if (typeof last === 'function') {
return fn(...args);
}
return Component => fn(...args, Component);
};
}
export function prefix(props, variant) {
invariant(
props.bsClass != null,
'A `bsClass` prop is required for this component'
);
return props.bsClass + (variant ? `-${variant}` : '');
}
export const bsClass = curry((defaultClass, Component) => {
let propTypes = Component.propTypes || (Component.propTypes = {});
let defaultProps = Component.defaultProps || (Component.defaultProps = {});
propTypes.bsClass = PropTypes.string;
defaultProps.bsClass = defaultClass;
return Component;
});
export const bsStyles = curry((styles, defaultStyle, Component) => {
if (typeof defaultStyle !== 'string') {
Component = defaultStyle;
defaultStyle = undefined;
}
let existing = Component.STYLES || [];
let propTypes = Component.propTypes || {};
styles.forEach(style => {
if (existing.indexOf(style) === -1) {
existing.push(style);
}
});
let propType = PropTypes.oneOf(existing);
// expose the values on the propType function for documentation
Component.STYLES = propType._values = existing;
Component.propTypes = {
...propTypes,
bsStyle: propType
};
if (defaultStyle !== undefined) {
let defaultProps = Component.defaultProps || (Component.defaultProps = {});
defaultProps.bsStyle = defaultStyle;
}
return Component;
});
export const bsSizes = curry((sizes, defaultSize, Component) => {
if (typeof defaultSize !== 'string') {
Component = defaultSize;
defaultSize = undefined;
}
let existing = Component.SIZES || [];
let propTypes = Component.propTypes || {};
sizes.forEach(size => {
if (existing.indexOf(size) === -1) {
existing.push(size);
}
});
const values = [];
existing.forEach(size => {
const mappedSize = SIZE_MAP[size];
if (mappedSize && mappedSize !== size) {
values.push(mappedSize);
}
values.push(size);
});
const propType = PropTypes.oneOf(values);
propType._values = values;
// expose the values on the propType function for documentation
Component.SIZES = existing;
Component.propTypes = {
...propTypes,
bsSize: propType
};
if (defaultSize !== undefined) {
if (!Component.defaultProps) {
Component.defaultProps = {};
}
Component.defaultProps.bsSize = defaultSize;
}
return Component;
});
export function getClassSet(props) {
const classes = {
[prefix(props)]: true,
};
if (props.bsSize) {
const bsSize = SIZE_MAP[props.bsSize] || props.bsSize;
classes[prefix(props, bsSize)] = true;
}
if (props.bsStyle) {
classes[prefix(props, props.bsStyle)] = true;
}
return classes;
}
function getBsProps(props) {
return {
bsClass: props.bsClass,
bsSize: props.bsSize,
bsStyle: props.bsStyle,
bsRole: props.bsRole,
};
}
function isBsProp(propName) {
return (
propName === 'bsClass' ||
propName === 'bsSize' ||
propName === 'bsStyle' ||
propName === 'bsRole'
);
}
export function splitBsProps(props) {
const elementProps = {};
Object.entries(props).forEach(([propName, propValue]) => {
if (!isBsProp(propName)) {
elementProps[propName] = propValue;
}
});
return [getBsProps(props), elementProps];
}
export function splitBsPropsAndOmit(props, omittedPropNames) {
const isOmittedProp = {};
omittedPropNames.forEach(propName => { isOmittedProp[propName] = true; });
const elementProps = {};
Object.entries(props).forEach(([propName, propValue]) => {
if (!isBsProp(propName) && !isOmittedProp[propName]) {
elementProps[propName] = propValue;
}
});
return [getBsProps(props), elementProps];
}
/**
* Add a style variant to a Component. Mutates the propTypes of the component
* in order to validate the new variant.
*/
export function addStyle(Component, ...styleVariant) {
bsStyles(styleVariant, Component);
}
export const _curry = curry;