2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-11-24 20:03:57 +08:00
|
|
|
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';
|
2018-09-21 19:30:39 +08:00
|
|
|
import RowContext from './RowContext';
|
2018-12-22 21:21:42 +08:00
|
|
|
import { tuple } from '../_util/type';
|
2019-05-22 23:22:09 +08:00
|
|
|
import ResponsiveObserve, {
|
|
|
|
Breakpoint,
|
|
|
|
BreakpointMap,
|
|
|
|
responsiveArray,
|
|
|
|
} from '../_util/responsiveObserve';
|
2016-08-19 17:11:06 +08:00
|
|
|
|
2019-09-23 11:58:03 +08:00
|
|
|
const RowAligns = tuple('top', 'middle', 'bottom', 'stretch');
|
2018-12-22 21:21:42 +08:00
|
|
|
const RowJustify = tuple('start', 'end', 'center', 'space-around', 'space-between');
|
2019-09-24 21:44:20 +08:00
|
|
|
|
|
|
|
export type Gutter = number | Partial<Record<Breakpoint, 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];
|
2016-08-19 17:11:06 +08:00
|
|
|
type?: 'flex';
|
2018-12-22 21:21:42 +08:00
|
|
|
align?: (typeof RowAligns)[number];
|
|
|
|
justify?: (typeof RowJustify)[number];
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:06 +08:00
|
|
|
export interface RowState {
|
|
|
|
screens: BreakpointMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Row extends React.Component<RowProps, RowState> {
|
2016-03-28 23:21:47 +08:00
|
|
|
static defaultProps = {
|
|
|
|
gutter: 0,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2017-11-21 20:34:06 +08:00
|
|
|
|
2016-03-28 23:21:47 +08:00
|
|
|
static propTypes = {
|
2018-12-22 21:21:42 +08:00
|
|
|
type: PropTypes.oneOf<'flex'>(['flex']),
|
|
|
|
align: PropTypes.oneOf(RowAligns),
|
|
|
|
justify: PropTypes.oneOf(RowJustify),
|
2017-04-12 04:49:08 +08:00
|
|
|
className: PropTypes.string,
|
|
|
|
children: PropTypes.node,
|
2019-09-24 21:44:20 +08:00
|
|
|
gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
|
2017-04-12 04:49:08 +08:00
|
|
|
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
|
|
|
|
2019-05-22 23:22:09 +08:00
|
|
|
token: string;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2017-11-01 18:33:41 +08:00
|
|
|
componentDidMount() {
|
2019-05-22 23:22:09 +08:00
|
|
|
this.token = ResponsiveObserve.subscribe(screens => {
|
2019-09-24 21:44:20 +08:00
|
|
|
const { gutter } = this.props;
|
|
|
|
if (
|
|
|
|
typeof gutter === 'object' ||
|
|
|
|
(Array.isArray(gutter) && (typeof gutter[0] === 'object' || typeof gutter[1] === 'object'))
|
|
|
|
) {
|
2019-05-22 23:22:09 +08:00
|
|
|
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() {
|
2019-05-22 23:22:09 +08:00
|
|
|
ResponsiveObserve.unsubscribe(this.token);
|
2017-11-01 18:33:41 +08:00
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-09-24 21:44:20 +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) => {
|
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 {
|
2019-10-20 12:48:03 +08:00
|
|
|
results[index] = g || 0;
|
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;
|
2017-11-01 18:33:41 +08:00
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderRow = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2017-11-01 15:46:06 +08:00
|
|
|
const {
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2018-12-07 20:02:01 +08:00
|
|
|
type,
|
|
|
|
justify,
|
|
|
|
align,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
children,
|
2018-12-05 19:12:18 +08:00
|
|
|
...others
|
2017-11-01 15:46:06 +08:00
|
|
|
} = this.props;
|
2018-12-05 19:12:18 +08:00
|
|
|
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,
|
|
|
|
);
|
2019-09-24 21:44:20 +08:00
|
|
|
const rowStyle = {
|
|
|
|
...(gutter[0]! > 0
|
|
|
|
? {
|
|
|
|
marginLeft: gutter[0]! / -2,
|
|
|
|
marginRight: gutter[0]! / -2,
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(gutter[1]! > 0
|
2018-12-07 20:02:01 +08:00
|
|
|
? {
|
2019-09-24 21:44:20 +08:00
|
|
|
marginTop: gutter[1]! / -2,
|
|
|
|
marginBottom: gutter[1]! / -2,
|
2018-12-07 20:02:01 +08:00
|
|
|
}
|
2019-09-24 21:44:20 +08:00
|
|
|
: {}),
|
|
|
|
...style,
|
|
|
|
};
|
2017-11-01 18:33:41 +08:00
|
|
|
const otherProps = { ...others };
|
|
|
|
delete otherProps.gutter;
|
2019-09-24 21:44:20 +08:00
|
|
|
|
2018-09-21 19:30:39 +08:00
|
|
|
return (
|
|
|
|
<RowContext.Provider value={{ gutter }}>
|
|
|
|
<div {...otherProps} className={classes} style={rowStyle}>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</RowContext.Provider>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderRow}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|