2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-09-05 10:17:56 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2022-12-22 10:11:08 +08:00
|
|
|
import type { Breakpoint, ScreenMap } from '../_util/responsiveObserver';
|
|
|
|
import useResponsiveObserver, { responsiveArray } from '../_util/responsiveObserver';
|
2023-09-05 10:17:56 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-21 22:14:15 +08:00
|
|
|
import RowContext from './RowContext';
|
2023-09-05 10:17:56 +08:00
|
|
|
import type { RowContextState } from './RowContext';
|
2022-03-31 12:45:41 +08:00
|
|
|
import { useRowStyle } from './style';
|
2016-08-19 17:11:06 +08:00
|
|
|
|
2024-09-19 03:30:19 +08:00
|
|
|
const _RowAligns = ['top', 'middle', 'bottom', 'stretch'] as const;
|
|
|
|
const _RowJustify = [
|
2022-12-13 14:57:40 +08:00
|
|
|
'start',
|
|
|
|
'end',
|
|
|
|
'center',
|
|
|
|
'space-around',
|
|
|
|
'space-between',
|
|
|
|
'space-evenly',
|
|
|
|
] as const;
|
2019-09-24 21:44:20 +08:00
|
|
|
|
2022-10-12 23:52:29 +08:00
|
|
|
type Responsive = 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs';
|
|
|
|
type ResponsiveLike<T> = {
|
|
|
|
[key in Responsive]?: T;
|
|
|
|
};
|
|
|
|
|
2022-05-07 11:44:48 +08:00
|
|
|
type Gap = number | undefined;
|
|
|
|
export type Gutter = number | undefined | Partial<Record<Breakpoint, number>>;
|
2022-10-12 23:52:29 +08:00
|
|
|
|
2024-09-19 03:30:19 +08:00
|
|
|
type ResponsiveAligns = ResponsiveLike<(typeof _RowAligns)[number]>;
|
|
|
|
type ResponsiveJustify = ResponsiveLike<(typeof _RowJustify)[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];
|
2024-09-19 03:30:19 +08:00
|
|
|
align?: (typeof _RowAligns)[number] | ResponsiveAligns;
|
|
|
|
justify?: (typeof _RowJustify)[number] | ResponsiveJustify;
|
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
|
|
|
}
|
|
|
|
|
2024-03-24 10:56:28 +08:00
|
|
|
function useMergedPropByScreen(
|
|
|
|
oriProp: RowProps['align'] | RowProps['justify'],
|
|
|
|
screen: ScreenMap,
|
|
|
|
) {
|
2022-10-12 23:52:29 +08:00
|
|
|
const [prop, setProp] = React.useState(typeof oriProp === 'string' ? oriProp : '');
|
|
|
|
|
2024-03-24 10:56:28 +08:00
|
|
|
const calcMergedAlignOrJustify = () => {
|
2022-12-21 14:16:41 +08:00
|
|
|
if (typeof oriProp === 'string') {
|
|
|
|
setProp(oriProp);
|
|
|
|
}
|
2022-10-12 23:52:29 +08:00
|
|
|
if (typeof oriProp !== 'object') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < responsiveArray.length; i++) {
|
|
|
|
const breakpoint: Breakpoint = responsiveArray[i];
|
|
|
|
// if do not match, do nothing
|
2023-09-05 10:17:56 +08:00
|
|
|
if (!screen[breakpoint]) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-12 23:52:29 +08:00
|
|
|
const curVal = oriProp[breakpoint];
|
|
|
|
if (curVal !== undefined) {
|
|
|
|
setProp(curVal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2024-03-24 10:56:28 +08:00
|
|
|
calcMergedAlignOrJustify();
|
2022-10-12 23:52:29 +08:00
|
|
|
}, [JSON.stringify(oriProp), screen]);
|
|
|
|
|
|
|
|
return prop;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
});
|
2022-10-12 23:52:29 +08:00
|
|
|
// 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,
|
|
|
|
});
|
|
|
|
|
2023-02-23 21:56:43 +08:00
|
|
|
// ================================== calc responsive data ==================================
|
2024-03-24 10:56:28 +08:00
|
|
|
const mergedAlign = useMergedPropByScreen(align, curScreens);
|
2022-10-12 23:52:29 +08:00
|
|
|
|
2024-03-24 10:56:28 +08:00
|
|
|
const mergedJustify = useMergedPropByScreen(justify, curScreens);
|
2020-10-14 16:35:29 +08:00
|
|
|
|
|
|
|
const gutterRef = React.useRef<Gutter | [Gutter, Gutter]>(gutter);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2022-12-22 10:11:08 +08:00
|
|
|
const responsiveObserver = useResponsiveObserver();
|
2022-12-15 14:14:07 +08:00
|
|
|
|
2021-03-02 20:29:00 +08:00
|
|
|
// ================================== Effect ==================================
|
2020-07-10 17:38:28 +08:00
|
|
|
React.useEffect(() => {
|
2022-12-22 10:11:08 +08:00
|
|
|
const token = responsiveObserver.subscribe((screen) => {
|
2022-10-12 23:52:29 +08:00
|
|
|
setCurScreens(screen);
|
2020-07-10 17:38:28 +08:00
|
|
|
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
|
|
|
});
|
2022-12-22 10:11:08 +08:00
|
|
|
return () => responsiveObserver.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);
|
2023-11-17 16:25:47 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useRowStyle(prefixCls);
|
2023-11-17 16:25:47 +08:00
|
|
|
|
2020-10-14 16:35:29 +08:00
|
|
|
const gutters = getGutter();
|
|
|
|
const classes = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
2020-10-30 22:13:23 +08:00
|
|
|
[`${prefixCls}-no-wrap`]: wrap === false,
|
2024-03-24 10:56:28 +08:00
|
|
|
[`${prefixCls}-${mergedJustify}`]: mergedJustify,
|
|
|
|
[`${prefixCls}-${mergedAlign}`]: mergedAlign,
|
2020-10-14 16:35:29 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
2022-03-31 12:45:41 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2020-10-14 16:35:29 +08:00
|
|
|
);
|
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;
|
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
|
|
|
|
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;
|
2023-09-05 10:17:56 +08:00
|
|
|
|
2024-02-17 19:56:41 +08:00
|
|
|
rowStyle.rowGap = gutterV;
|
|
|
|
|
2023-09-05 10:17:56 +08:00
|
|
|
const rowContext = React.useMemo<RowContextState>(
|
|
|
|
() => ({ gutter: [gutterH, gutterV] as [number, number], wrap }),
|
|
|
|
[gutterH, gutterV, wrap],
|
2022-01-21 18:39:47 +08:00
|
|
|
);
|
2021-03-02 20:29:00 +08:00
|
|
|
|
2023-11-17 16:25:47 +08:00
|
|
|
return wrapCSSVar(
|
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>
|
2022-03-31 12:45:41 +08:00
|
|
|
</RowContext.Provider>,
|
2020-10-14 16:35:29 +08:00
|
|
|
);
|
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;
|