ant-design/components/grid/row.tsx
MadCcc 1ac0bcaa60
Some checks are pending
Publish Any Commit / build (push) Waiting to run
✅ test v6 / lint (push) Waiting to run
✅ test v6 / test-react-legacy (18, 1/2) (push) Waiting to run
✅ test v6 / test-react-legacy (18, 2/2) (push) Waiting to run
✅ test v6 / test-node (push) Waiting to run
✅ test v6 / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test v6 / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test v6 / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test v6 / test-coverage (push) Blocked by required conditions
✅ test v6 / build (push) Waiting to run
✅ test v6 / test lib/es module (es, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (es, 2/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
feat: use cssVar by default (#52671)
* feat: use cssVar by default

* chore: update deps

* chore: update snapshot

* chore: update snapshot

* chore: update snapshot

* chore: test

* chore: update

* chore: update scripts

* chore: UPDATE TEST

* feat: add root

* chore: update snapshot

* chore: fix test case

* chore: fix cycle deps

* feat: rm legacy css variables configuration

* chore: update test case

* chore: update

* chore: fix test

* chore: update overrides

* chore: bump cssinjs

* chore: add test case
2025-02-24 15:35:29 +08:00

146 lines
3.9 KiB
TypeScript

import * as React from 'react';
import classNames from 'classnames';
import type { Breakpoint, ScreenMap } from '../_util/responsiveObserver';
import { responsiveArray } from '../_util/responsiveObserver';
import { ConfigContext } from '../config-provider';
import useBreakpoint from './hooks/useBreakpoint';
import useGutter from './hooks/useGutter';
import RowContext from './RowContext';
import type { RowContextState } from './RowContext';
import { useRowStyle } from './style';
const _RowAligns = ['top', 'middle', 'bottom', 'stretch'] as const;
const _RowJustify = [
'start',
'end',
'center',
'space-around',
'space-between',
'space-evenly',
] as const;
type Responsive = 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs';
type ResponsiveLike<T> = {
[key in Responsive]?: T;
};
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;
}
function useMergedPropByScreen(
oriProp: RowProps['align'] | RowProps['justify'],
screen: ScreenMap | null,
) {
const [prop, setProp] = React.useState(typeof oriProp === 'string' ? oriProp : '');
const calcMergedAlignOrJustify = () => {
if (typeof oriProp === 'string') {
setProp(oriProp);
}
if (typeof oriProp !== 'object') {
return;
}
for (let i = 0; i < responsiveArray.length; i++) {
const breakpoint: Breakpoint = responsiveArray[i];
// if do not match, do nothing
if (!screen || !screen[breakpoint]) {
continue;
}
const curVal = oriProp[breakpoint];
if (curVal !== undefined) {
setProp(curVal);
return;
}
}
};
React.useEffect(() => {
calcMergedAlignOrJustify();
}, [JSON.stringify(oriProp), screen]);
return prop;
}
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 = useBreakpoint(true, null);
const mergedAlign = useMergedPropByScreen(align, screens);
const mergedJustify = useMergedPropByScreen(justify, screens);
const prefixCls = getPrefixCls('row', customizePrefixCls);
const [hashId, cssVarCls] = useRowStyle(prefixCls);
const gutters = useGutter(gutter, screens);
const classes = classNames(
prefixCls,
{
[`${prefixCls}-no-wrap`]: wrap === false,
[`${prefixCls}-${mergedJustify}`]: mergedJustify,
[`${prefixCls}-${mergedAlign}`]: mergedAlign,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
hashId,
cssVarCls,
);
// Add gutter related style
const rowStyle: React.CSSProperties = {};
const horizontalGutter = gutters[0] != null && gutters[0] > 0 ? gutters[0] / -2 : undefined;
if (horizontalGutter) {
rowStyle.marginInline = horizontalGutter;
}
// "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;
rowStyle.rowGap = gutterV;
const rowContext = React.useMemo<RowContextState>(
() => ({ gutter: [gutterH, gutterV] as [number, number], wrap }),
[gutterH, gutterV, wrap],
);
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;