forked from flexport/__latitude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeprecatedHorizontalGroup.jsx
130 lines (123 loc) · 3.79 KB
/
DeprecatedHorizontalGroup.jsx
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
/**
* TEAM: frontend_infra
* @deprecated prefer using latitude/Flex
* @flow strict
*/
/* eslint-disable no-restricted-imports*/
/* eslint-disable react/prefer-stateless-function */
import * as React from "react";
import classnames from "classnames";
import {compact, flatten} from "lodash";
import {SPACING_SIZES} from "./constants/styles";
const CROSS_ALIGN_CLASS_MAP = {
baseline: "flexBaseline",
center: "flexCenter",
start: "flexStart",
end: "flexEnd",
stretch: "flexStretch",
default: "",
};
const MAIN_ALIGN_CLASS_MAP = {
start: "flexContentStart",
end: "flexContentEnd",
center: "flexContentCenter",
between: "flexSpaceBetween",
around: "flexSpaceAround",
default: "",
};
type Props = {
/** The percentage of the parent that each child should occupy. */
+basis?: number,
/** Children can be any React.Node and will be auto laid out based on Group's props. */
+children?: React.Node,
/** The flexbox align-items property which generally adjusts the vertical flex layout of the items. */
+crossAlign: $Keys<typeof CROSS_ALIGN_CLASS_MAP>,
/** Whether the elements should grow to fill the width of their container. */
+fill: boolean,
/** The flexbox justify-content property which generally adjusts the horizontal layout of the items. */
+mainAlign: $Keys<typeof MAIN_ALIGN_CLASS_MAP>,
/** The minimum width of each child. */
+minWidth?: number,
/** The gap between elements. */
+spacing: $Keys<typeof SPACING_SIZES>,
/** Whether the elements should wrap. */
+wrap: boolean,
};
/**
* @short Horizontally distribute elements using standard whitespace and simple flexbox rules
* @brandStatus V2
* @status Deprecated
* @category Layout
* @extends React.Component */
export default class DeprecatedHorizontalGroup extends React.PureComponent<Props> {
static defaultProps = {
mainAlign: "default",
crossAlign: "baseline",
spacing: "s",
fill: false,
wrap: true,
};
render() {
const {
basis,
children,
crossAlign,
mainAlign,
minWidth,
spacing,
fill,
wrap,
} = this.props;
const wrapperClasses = classnames(
"flex",
CROSS_ALIGN_CLASS_MAP[crossAlign],
MAIN_ALIGN_CLASS_MAP[mainAlign]
);
const paddingClass = spacing === "none" ? "" : `pr${spacing}`;
const elementClasses = classnames(paddingClass, {
flexfill: fill,
flexStatic: !fill,
});
const margin = SPACING_SIZES[spacing];
// When an element wraps, it needs a top margin, but all the elements on the
// first row do not, so the negative margin here cancels it out.
const wrapStyle = {
flexFlow: `row ${wrap ? "" : "no"}wrap`,
marginTop: `-${margin}px`,
};
const elementWrapStyle = {marginTop: `${margin}px`};
const elementWidth = minWidth != null ? {minWidth} : {};
const elementStyles = {...elementWrapStyle, ...elementWidth};
if (basis != null) {
// $FlowUpgradeFixMe(0.69.0 -> 0.70.0)
elementStyles.flex = `1 0 ${basis}%`;
}
const elements = Array.isArray(children)
? compact(flatten(children))
: [children];
return Array.isArray(children) ? (
<div className={wrapperClasses} style={wrapStyle}>
{elements.map((e, i) =>
i === elements.length - 1 ? (
<div
// eslint-disable-next-line react/no-array-index-key
key={i}
className={fill ? "flexfill" : "flexStatic"}
style={elementStyles}
>
{e}
</div>
) : (
// eslint-disable-next-line react/no-array-index-key
<div key={i} className={elementClasses} style={elementStyles}>
{e}
</div>
)
)}
</div>
) : (
// $FlowUpgradeFixMe(0.81.0 -> 0.82.0)
children
);
}
}