2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2020-10-14 16:35:29 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2018-09-21 19:30:39 +08:00
|
|
|
import RowContext from './RowContext';
|
2018-12-22 21:21:42 +08:00
|
|
|
import { tuple } from '../_util/type';
|
2019-05-22 23:22:09 +08:00
|
|
|
import ResponsiveObserve, {
|
|
|
|
Breakpoint,
|
2020-01-08 17:14:29 +08:00
|
|
|
ScreenMap,
|
2019-05-22 23:22:09 +08:00
|
|
|
responsiveArray,
|
|
|
|
} from '../_util/responsiveObserve';
|
2021-03-02 20:29:00 +08:00
|
|
|
import useFlexGapSupport from './hooks/useFlexGapSupport';
|
2016-08-19 17:11:06 +08:00
|
|
|
|
2019-09-23 11:58:03 +08:00
|
|
|
const RowAligns = tuple('top', 'middle', 'bottom', 'stretch');
|
2018-12-22 21:21:42 +08:00
|
|
|
const RowJustify = tuple('start', 'end', 'center', 'space-around', 'space-between');
|
2019-09-24 21:44:20 +08:00
|
|
|
|
|
|
|
export type Gutter = number | Partial<Record<Breakpoint, number>>;
|
2018-03-06 15:51:06 +08:00
|
|
|
export interface RowProps extends React.HTMLAttributes<HTMLDivElement> {
|
2019-09-24 21:44:20 +08:00
|
|
|
gutter?: Gutter | [Gutter, Gutter];
|
2019-11-20 11:13:24 +08:00
|
|
|
align?: typeof RowAligns[number];
|
|
|
|
justify?: typeof RowJustify[number];
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
2020-10-30 22:13:23 +08:00
|
|
|
wrap?: boolean;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2020-07-10 17:38:28 +08:00
|
|
|
const Row = React.forwardRef<HTMLDivElement, RowProps>((props, ref) => {
|
2020-10-14 16:35:29 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
justify,
|
|
|
|
align,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
children,
|
|
|
|
gutter = 0,
|
2020-10-30 22:13:23 +08:00
|
|
|
wrap,
|
2020-10-14 16:35:29 +08:00
|
|
|
...others
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
|
2020-07-10 17:38:28 +08:00
|
|
|
const [screens, setScreens] = React.useState<ScreenMap>({
|
|
|
|
xs: true,
|
|
|
|
sm: true,
|
|
|
|
md: true,
|
|
|
|
lg: true,
|
|
|
|
xl: true,
|
|
|
|
xxl: true,
|
|
|
|
});
|
2020-10-14 16:35:29 +08:00
|
|
|
|
2021-03-02 20:29:00 +08:00
|
|
|
const supportFlexGap = useFlexGapSupport();
|
|
|
|
|
2020-10-14 16:35:29 +08:00
|
|
|
const gutterRef = React.useRef<Gutter | [Gutter, Gutter]>(gutter);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2021-03-02 20:29:00 +08:00
|
|
|
// ================================== Effect ==================================
|
2020-07-10 17:38:28 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
const token = ResponsiveObserve.subscribe(screen => {
|
|
|
|
const currentGutter = gutterRef.current || 0;
|
2020-01-03 15:20:28 +08:00
|
|
|
if (
|
2020-07-10 17:38:28 +08:00
|
|
|
(!Array.isArray(currentGutter) && typeof currentGutter === 'object') ||
|
|
|
|
(Array.isArray(currentGutter) &&
|
|
|
|
(typeof currentGutter[0] === 'object' || typeof currentGutter[1] === 'object'))
|
2020-01-03 15:20:28 +08:00
|
|
|
) {
|
2020-07-10 17:38:28 +08:00
|
|
|
setScreens(screen);
|
2020-01-03 15:20:28 +08:00
|
|
|
}
|
2019-05-22 23:22:09 +08:00
|
|
|
});
|
2020-10-14 16:35:29 +08:00
|
|
|
return () => ResponsiveObserve.unsubscribe(token);
|
2020-07-10 17:38:28 +08:00
|
|
|
}, []);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2021-03-02 20:29:00 +08:00
|
|
|
// ================================== Render ==================================
|
2020-07-10 17:38:28 +08:00
|
|
|
const getGutter = (): [number, number] => {
|
2019-10-20 12:48:03 +08:00
|
|
|
const results: [number, number] = [0, 0];
|
|
|
|
const normalizedGutter = Array.isArray(gutter) ? gutter : [gutter, 0];
|
|
|
|
normalizedGutter.forEach((g, index) => {
|
2019-09-24 21:44:20 +08:00
|
|
|
if (typeof g === 'object') {
|
|
|
|
for (let i = 0; i < responsiveArray.length; i++) {
|
|
|
|
const breakpoint: Breakpoint = responsiveArray[i];
|
2019-10-20 12:48:03 +08:00
|
|
|
if (screens[breakpoint] && g[breakpoint] !== undefined) {
|
|
|
|
results[index] = g[breakpoint] as number;
|
2019-10-20 12:48:39 +08:00
|
|
|
break;
|
2019-09-24 21:44:20 +08:00
|
|
|
}
|
2017-11-01 20:31:26 +08:00
|
|
|
}
|
2019-09-24 21:44:20 +08:00
|
|
|
} else {
|
2019-10-20 12:48:03 +08:00
|
|
|
results[index] = g || 0;
|
2017-11-01 20:31:26 +08:00
|
|
|
}
|
2019-09-24 21:44:20 +08:00
|
|
|
});
|
2019-10-20 12:48:03 +08:00
|
|
|
return results;
|
2020-07-10 17:38:28 +08:00
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-10-14 16:35:29 +08:00
|
|
|
const prefixCls = getPrefixCls('row', customizePrefixCls);
|
|
|
|
const gutters = getGutter();
|
|
|
|
const classes = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
2020-10-30 22:13:23 +08:00
|
|
|
[`${prefixCls}-no-wrap`]: wrap === false,
|
2020-10-14 16:35:29 +08:00
|
|
|
[`${prefixCls}-${justify}`]: justify,
|
|
|
|
[`${prefixCls}-${align}`]: align,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2021-02-01 10:15:39 +08:00
|
|
|
|
|
|
|
// Add gutter related style
|
2021-02-03 18:06:51 +08:00
|
|
|
const rowStyle: React.CSSProperties = {};
|
|
|
|
const horizontalGutter = gutters[0] > 0 ? gutters[0] / -2 : undefined;
|
|
|
|
const verticalGutter = gutters[1] > 0 ? gutters[1] / -2 : undefined;
|
2021-02-01 10:15:39 +08:00
|
|
|
|
2021-02-03 18:06:51 +08:00
|
|
|
rowStyle.marginLeft = horizontalGutter;
|
|
|
|
rowStyle.marginRight = horizontalGutter;
|
2021-02-01 10:15:39 +08:00
|
|
|
|
2021-03-02 20:29:00 +08:00
|
|
|
if (supportFlexGap) {
|
2021-02-03 18:06:51 +08:00
|
|
|
// Set gap direct if flex gap support
|
|
|
|
[, rowStyle.rowGap] = gutters;
|
2021-02-01 10:15:39 +08:00
|
|
|
} else {
|
2021-02-03 18:06:51 +08:00
|
|
|
rowStyle.marginTop = verticalGutter;
|
|
|
|
rowStyle.marginBottom = verticalGutter;
|
2021-02-01 10:15:39 +08:00
|
|
|
}
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2021-03-02 20:29:00 +08:00
|
|
|
const rowContext = React.useMemo(() => ({ gutter: gutters, wrap, supportFlexGap }), [
|
|
|
|
gutters,
|
|
|
|
wrap,
|
|
|
|
supportFlexGap,
|
|
|
|
]);
|
|
|
|
|
2020-10-14 16:35:29 +08:00
|
|
|
return (
|
2021-03-02 20:29:00 +08:00
|
|
|
<RowContext.Provider value={rowContext}>
|
2021-02-01 10:15:39 +08:00
|
|
|
<div {...others} className={classes} style={{ ...rowStyle, ...style }} ref={ref}>
|
2020-10-14 16:35:29 +08:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</RowContext.Provider>
|
|
|
|
);
|
2020-07-10 17:38:28 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
Row.displayName = 'Row';
|
|
|
|
|
|
|
|
export default Row;
|