ant-design/components/grid/row.tsx

113 lines
3.0 KiB
TypeScript
Raw Normal View History

import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import * as React from 'react';
import classNames from 'classnames';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
import RowContext from './RowContext';
import { tuple } from '../_util/type';
import ResponsiveObserve, {
Breakpoint,
BreakpointMap,
responsiveArray,
} from '../_util/responsiveObserve';
const RowAligns = tuple('top', 'middle', 'bottom');
const RowJustify = tuple('start', 'end', 'center', 'space-around', 'space-between');
export interface RowProps extends React.HTMLAttributes<HTMLDivElement> {
2018-05-17 21:41:25 +08:00
gutter?: number | Partial<Record<Breakpoint, number>>;
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,
2017-11-01 21:40:47 +08:00
gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
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
};
token: string;
2017-11-01 18:33:41 +08:00
componentDidMount() {
this.token = ResponsiveObserve.subscribe(screens => {
if (typeof this.props.gutter === 'object') {
this.setState({ screens });
}
});
2017-11-01 18:33:41 +08:00
}
componentWillUnmount() {
ResponsiveObserve.unsubscribe(this.token);
2017-11-01 18:33:41 +08:00
}
getGutter(): number | undefined {
2017-11-01 18:33:41 +08:00
const { gutter } = this.props;
if (typeof gutter === 'object') {
for (let i = 0; i < responsiveArray.length; i++) {
2017-11-21 20:34:06 +08:00
const breakpoint: Breakpoint = responsiveArray[i];
if (this.state.screens[breakpoint] && gutter[breakpoint] !== undefined) {
return gutter[breakpoint];
2017-11-01 20:31:26 +08:00
}
}
2017-11-01 18:33:41 +08:00
}
return gutter as number;
2017-11-01 18:33:41 +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
2018-12-07 20:02:01 +08:00
? {
marginLeft: gutter! / -2,
marginRight: gutter! / -2,
2018-12-07 20:02:01 +08:00
...style,
}
: 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>;
}
}