2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2022-05-23 14:37:16 +08:00
|
|
|
import * as React from 'react';
|
2020-10-14 16:35:29 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-23 14:37:16 +08:00
|
|
|
import useFlexGapSupport from '../_util/hooks/useFlexGapSupport';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { Breakpoint, ScreenMap } from '../_util/responsiveObserve';
|
|
|
|
import ResponsiveObserve, { responsiveArray } from '../_util/responsiveObserve';
|
2022-05-23 14:37:16 +08:00
|
|
|
import { tuple } from '../_util/type';
|
|
|
|
import RowContext from './RowContext';
|
2016-08-19 17:11:06 +08:00
|
|
|
|
2019-09-23 11:58:03 +08:00
|
|
|
const RowAligns = tuple('top', 'middle', 'bottom', 'stretch');
|
2022-03-21 09:49:21 +08:00
|
|
|
const RowJustify = tuple('start', 'end', 'center', 'space-around', 'space-between', 'space-evenly');
|
2019-09-24 21:44:20 +08:00
|
|
|
|
2022-05-07 11:44:48 +08:00
|
|
|
type Gap = number | undefined;
|
|
|
|
export type Gutter = number | undefined | 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 ==================================
|
2022-05-07 11:44:48 +08:00
|
|
|
const getGutter = (): [Gap, Gap] => {
|
|
|
|
const results: [Gap, Gap] = [undefined, undefined];
|
|
|
|
const normalizedGutter = Array.isArray(gutter) ? gutter : [gutter, undefined];
|
2019-10-20 12:48:03 +08:00
|
|
|
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 {
|
2022-05-07 11:44:48 +08:00
|
|
|
results[index] = g;
|
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 = {};
|
2022-05-07 11:44:48 +08:00
|
|
|
const horizontalGutter = gutters[0] != null && gutters[0] > 0 ? gutters[0] / -2 : undefined;
|
|
|
|
const verticalGutter = gutters[1] != null && gutters[1] > 0 ? gutters[1] / -2 : undefined;
|
2021-02-01 10:15:39 +08:00
|
|
|
|
2021-03-11 10:44:18 +08:00
|
|
|
if (horizontalGutter) {
|
|
|
|
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-03-11 10:44:18 +08:00
|
|
|
} else if (verticalGutter) {
|
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
|
|
|
|
2022-01-21 18:39:47 +08:00
|
|
|
// "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],
|
|
|
|
);
|
2021-03-02 20:29:00 +08:00
|
|
|
|
2020-10-14 16:35:29 +08:00
|
|
|
return (
|
2021-03-02 20:29:00 +08:00
|
|
|
<RowContext.Provider value={rowContext}>
|
2022-05-19 10:05:40 +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
|
|
|
});
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Row.displayName = 'Row';
|
|
|
|
}
|
2020-07-10 17:38:28 +08:00
|
|
|
|
|
|
|
export default Row;
|