ant-design/components/grid/row.tsx

139 lines
3.7 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classNames from 'classnames';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
2019-08-05 18:38:10 +08:00
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import RowContext from './RowContext';
import { tuple } from '../_util/type';
import ResponsiveObserve, {
Breakpoint,
BreakpointMap,
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];
type?: 'flex';
align?: (typeof RowAligns)[number];
justify?: (typeof RowJustify)[number];
prefixCls?: string;
}
2017-11-21 20:34:06 +08:00
export interface RowState {
screens: BreakpointMap;
}
export default class Row extends React.Component<RowProps, RowState> {
static defaultProps = {
gutter: 0,
2016-07-13 11:14:24 +08:00
};
2017-11-21 20:34:06 +08:00
static propTypes = {
type: PropTypes.oneOf<'flex'>(['flex']),
align: PropTypes.oneOf(RowAligns),
justify: PropTypes.oneOf(RowJustify),
className: PropTypes.string,
children: PropTypes.node,
gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
prefixCls: PropTypes.string,
2016-07-13 11:14:24 +08:00
};
2017-11-21 20:34:06 +08:00
state: RowState = {
2017-11-01 20:31:26 +08:00
screens: {},
2017-11-01 18:33:41 +08:00
};
2019-08-05 18:38:10 +08:00
token: string;
2019-08-05 18:38:10 +08:00
2017-11-01 18:33:41 +08:00
componentDidMount() {
this.token = ResponsiveObserve.subscribe(screens => {
const { gutter } = this.props;
if (
typeof gutter === 'object' ||
(Array.isArray(gutter) && (typeof gutter[0] === 'object' || typeof gutter[1] === 'object'))
) {
this.setState({ screens });
}
});
2017-11-01 18:33:41 +08:00
}
2019-08-05 18:38:10 +08:00
2017-11-01 18:33:41 +08:00
componentWillUnmount() {
ResponsiveObserve.unsubscribe(this.token);
2017-11-01 18:33:41 +08:00
}
2019-08-05 18:38:10 +08:00
getGutter(): [number, number] {
2019-10-20 12:48:03 +08:00
const results: [number, number] = [0, 0];
const { gutter } = this.props;
const { screens } = this.state;
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;
}
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;
2017-11-01 18:33:41 +08:00
}
2019-08-05 18:38:10 +08:00
renderRow = ({ getPrefixCls }: ConfigConsumerProps) => {
2017-11-01 15:46:06 +08:00
const {
prefixCls: customizePrefixCls,
2018-12-07 20:02:01 +08:00
type,
justify,
align,
className,
style,
children,
...others
2017-11-01 15:46:06 +08:00
} = this.props;
const prefixCls = getPrefixCls('row', customizePrefixCls);
2017-11-01 18:33:41 +08:00
const gutter = this.getGutter();
2018-12-07 20:02:01 +08:00
const classes = classNames(
{
[prefixCls]: !type,
[`${prefixCls}-${type}`]: type,
[`${prefixCls}-${type}-${justify}`]: type && justify,
[`${prefixCls}-${type}-${align}`]: type && align,
},
className,
);
const rowStyle = {
...(gutter[0]! > 0
? {
marginLeft: gutter[0]! / -2,
marginRight: gutter[0]! / -2,
}
: {}),
...(gutter[1]! > 0
2018-12-07 20:02:01 +08:00
? {
marginTop: gutter[1]! / -2,
marginBottom: gutter[1]! / -2,
2018-12-07 20:02:01 +08:00
}
: {}),
...style,
};
2017-11-01 18:33:41 +08:00
const otherProps = { ...others };
delete otherProps.gutter;
return (
<RowContext.Provider value={{ gutter }}>
<div {...otherProps} className={classes} style={rowStyle}>
{children}
</div>
</RowContext.Provider>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderRow}</ConfigConsumer>;
}
}