-
-
Notifications
You must be signed in to change notification settings - Fork 535
Expand file tree
/
Copy pathFlex.jsx
More file actions
50 lines (46 loc) · 1.26 KB
/
Flex.jsx
File metadata and controls
50 lines (46 loc) · 1.26 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
import React, { forwardRef } from 'react';
import PropTypes from 'prop-types';
function toPercent(num) {
return `${num}%`;
}
const Flex = forwardRef(
({ children, className, count, direction, offset, style, wrap, ...otherProps }, ref) => {
return (
<div
className={className}
ref={ref}
style={{
display: 'flex',
flexDirection: direction,
flexWrap: wrap ? 'wrap' : 'nowrap',
...style,
}}
{...otherProps}
>
{React.Children.map(children, (child, index) =>
React.cloneElement(child, {
...child.props,
style: {
flexBasis: toPercent(100 / count),
flexShrink: 0,
flexGrow: 0,
overflow: 'hidden',
marginLeft: offset && index === 0 ? toPercent((100 * offset) / count) : null,
},
}),
)}
</div>
);
},
);
Flex.displayName = 'Flex';
Flex.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
count: PropTypes.number.isRequired,
direction: PropTypes.string,
offset: PropTypes.number,
style: PropTypes.objectOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
wrap: PropTypes.bool,
};
export default Flex;