forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
row.tsx
187 lines (165 loc) · 6.14 KB
/
row.tsx
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
187
import classNames from 'classnames';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import useFlexGapSupport from '../_util/hooks/useFlexGapSupport';
import type { Breakpoint, ScreenMap } from '../_util/responsiveObserve';
import ResponsiveObserve, { responsiveArray } from '../_util/responsiveObserve';
import { tuple } from '../_util/type';
import RowContext from './RowContext';
const RowAligns = tuple('top', 'middle', 'bottom', 'stretch');
const RowJustify = tuple('start', 'end', 'center', 'space-around', 'space-between', 'space-evenly');
type Responsive = 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs' | 'other';
type ResponsiveLike<T> = {
[key in Responsive]?: T;
};
type Gap = number | undefined;
export type Gutter = number | undefined | Partial<Record<Breakpoint, number>>;
type ResponsiveAligns = ResponsiveLike<typeof RowAligns[number]>;
type ResponsiveJustify = ResponsiveLike<typeof RowJustify[number]>;
export interface RowProps extends React.HTMLAttributes<HTMLDivElement> {
gutter?: Gutter | [Gutter, Gutter];
align?: typeof RowAligns[number] | ResponsiveAligns;
justify?: typeof RowJustify[number] | ResponsiveJustify;
prefixCls?: string;
wrap?: boolean;
}
const Row = React.forwardRef<HTMLDivElement, RowProps>((props, ref) => {
const {
prefixCls: customizePrefixCls,
justify,
align,
className,
style,
children,
gutter = 0,
wrap,
...others
} = props;
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const [screens, setScreens] = React.useState<ScreenMap>({
xs: true,
sm: true,
md: true,
lg: true,
xl: true,
xxl: true,
});
// to save screens info when responsiveObserve callback had been call
const [curScreens, setCurScreens] = React.useState<ScreenMap>({
xs: false,
sm: false,
md: false,
lg: false,
xl: false,
xxl: false,
});
const [mergeAlign, setMergeAlign] = React.useState(typeof align === 'string' ? align : '');
const [mergeJustify, setMergeJustify] = React.useState(
typeof justify === 'string' ? justify : '',
);
const supportFlexGap = useFlexGapSupport();
const gutterRef = React.useRef<Gutter | [Gutter, Gutter]>(gutter);
// ================================== Effect ==================================
React.useEffect(() => {
const token = ResponsiveObserve.subscribe(screen => {
setCurScreens(screen);
const currentGutter = gutterRef.current || 0;
if (
(!Array.isArray(currentGutter) && typeof currentGutter === 'object') ||
(Array.isArray(currentGutter) &&
(typeof currentGutter[0] === 'object' || typeof currentGutter[1] === 'object'))
) {
setScreens(screen);
}
});
return () => ResponsiveObserve.unsubscribe(token);
}, []);
// ================================== Render ==================================
const getGutter = (): [Gap, Gap] => {
const results: [Gap, Gap] = [undefined, undefined];
const normalizedGutter = Array.isArray(gutter) ? gutter : [gutter, undefined];
normalizedGutter.forEach((g, index) => {
if (typeof g === 'object') {
for (let i = 0; i < responsiveArray.length; i++) {
const breakpoint: Breakpoint = responsiveArray[i];
if (screens[breakpoint] && g[breakpoint] !== undefined) {
results[index] = g[breakpoint] as number;
break;
}
}
} else {
results[index] = g;
}
});
return results;
};
// ================================== calc reponsive data ==================================
const clacMergeAlignOrJustify = (propName: 'align' | 'justify') => {
if (typeof props[propName] !== 'object') {
return;
}
const prop = props[propName] as ResponsiveLike<any>;
const updator = propName === 'align' ? setMergeAlign : setMergeJustify;
for (let i = 0; i < responsiveArray.length; i++) {
const breakpoint: Breakpoint = responsiveArray[i];
// When 'align' sets the 'other' attribute,
// we need to set the value of the response attribute not explicitly set in 'align' to the value of 'other'
const curVal = prop[breakpoint];
if (prop.other && !curScreens[breakpoint]) {
if (!curVal) {
updator(prop.other);
}
} else if (curScreens[breakpoint] && curVal !== undefined) {
updator(curVal);
}
}
};
const prefixCls = getPrefixCls('row', customizePrefixCls);
const gutters = getGutter();
const classes = classNames(
prefixCls,
{
[`${prefixCls}-no-wrap`]: wrap === false,
[`${prefixCls}-${mergeJustify}`]: mergeJustify,
[`${prefixCls}-${mergeAlign}`]: mergeAlign,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
// Add gutter related style
const rowStyle: React.CSSProperties = {};
const horizontalGutter = gutters[0] != null && gutters[0] > 0 ? gutters[0] / -2 : undefined;
const verticalGutter = gutters[1] != null && gutters[1] > 0 ? gutters[1] / -2 : undefined;
if (horizontalGutter) {
rowStyle.marginLeft = horizontalGutter;
rowStyle.marginRight = horizontalGutter;
}
if (supportFlexGap) {
// Set gap direct if flex gap support
[, rowStyle.rowGap] = gutters;
} else if (verticalGutter) {
rowStyle.marginTop = verticalGutter;
rowStyle.marginBottom = verticalGutter;
}
// "gutters" is a new array in each rendering phase, it'll make 'React.useMemo' effectless.
// So we deconstruct "gutters" variable here.
const [gutterH, gutterV] = gutters;
const rowContext = React.useMemo(
() => ({ gutter: [gutterH, gutterV] as [number, number], wrap, supportFlexGap }),
[gutterH, gutterV, wrap, supportFlexGap],
);
// re-calc responsive data
React.useEffect(() => clacMergeAlignOrJustify('align'), [align, curScreens]);
React.useEffect(() => clacMergeAlignOrJustify('justify'), [justify, curScreens]);
return (
<RowContext.Provider value={rowContext}>
<div {...others} className={classes} style={{ ...rowStyle, ...style }} ref={ref}>
{children}
</div>
</RowContext.Provider>
);
});
if (process.env.NODE_ENV !== 'production') {
Row.displayName = 'Row';
}
export default Row;