ant-design/components/grid/row.tsx

120 lines
3.2 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider';
import RowContext from './RowContext';
import { tuple } from '../_util/type';
import ResponsiveObserve, {
Breakpoint,
ScreenMap,
responsiveArray,
} from '../_util/responsiveObserve';
2019-09-23 11:58:03 +08:00
const RowAligns = tuple('top', 'middle', 'bottom', 'stretch');
const RowJustify = tuple('start', 'end', 'center', 'space-around', 'space-between');
export type Gutter = number | Partial<Record<Breakpoint, number>>;
export interface RowProps extends React.HTMLAttributes<HTMLDivElement> {
gutter?: Gutter | [Gutter, Gutter];
align?: typeof RowAligns[number];
justify?: typeof RowJustify[number];
prefixCls?: string;
}
const Row = React.forwardRef<HTMLDivElement, RowProps>((props, ref) => {
const {
prefixCls: customizePrefixCls,
justify,
align,
className,
style,
children,
gutter = 0,
...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,
});
const gutterRef = React.useRef<Gutter | [Gutter, Gutter]>(gutter);
2019-08-05 18:38:10 +08:00
React.useEffect(() => {
const token = ResponsiveObserve.subscribe(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);
}, []);
2019-08-05 18:38:10 +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) => {
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;
break;
}
2017-11-01 20:31:26 +08:00
}
} else {
2019-10-20 12:48:03 +08:00
results[index] = g || 0;
2017-11-01 20:31:26 +08:00
}
});
2019-10-20 12:48:03 +08:00
return results;
};
2019-08-05 18:38:10 +08:00
const prefixCls = getPrefixCls('row', customizePrefixCls);
const gutters = getGutter();
const classes = classNames(
prefixCls,
{
[`${prefixCls}-${justify}`]: justify,
[`${prefixCls}-${align}`]: align,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
const rowStyle = {
...(gutters[0]! > 0
? {
marginLeft: gutters[0]! / -2,
marginRight: gutters[0]! / -2,
}
: {}),
...(gutters[1]! > 0
? {
marginTop: gutters[1]! / -2,
marginBottom: gutters[1]! / 2,
}
: {}),
...style,
2018-12-07 20:02:01 +08:00
};
return (
<RowContext.Provider value={{ gutter: gutters }}>
<div {...others} className={classes} style={rowStyle} ref={ref}>
{children}
</div>
</RowContext.Provider>
);
});
Row.displayName = 'Row';
export default Row;