import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; import * as React from 'react'; import classNames from 'classnames'; 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 { gutter?: number | Partial>; type?: 'flex'; align?: (typeof RowAligns)[number]; justify?: (typeof RowJustify)[number]; prefixCls?: string; } export interface RowState { screens: BreakpointMap; } export default class Row extends React.Component { static defaultProps = { gutter: 0, }; 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]), prefixCls: PropTypes.string, }; state: RowState = { screens: {}, }; token: string; componentDidMount() { this.token = ResponsiveObserve.subscribe(screens => { if (typeof this.props.gutter === 'object') { this.setState({ screens }); } }); } componentWillUnmount() { ResponsiveObserve.unsubscribe(this.token); } getGutter(): number | undefined { const { gutter } = this.props; if (typeof gutter === 'object') { for (let i = 0; i < responsiveArray.length; i++) { const breakpoint: Breakpoint = responsiveArray[i]; if (this.state.screens[breakpoint] && gutter[breakpoint] !== undefined) { return gutter[breakpoint]; } } } return gutter as number; } renderRow = ({ getPrefixCls }: ConfigConsumerProps) => { const { prefixCls: customizePrefixCls, type, justify, align, className, style, children, ...others } = this.props; const prefixCls = getPrefixCls('row', customizePrefixCls); const gutter = this.getGutter(); const classes = classNames( { [prefixCls]: !type, [`${prefixCls}-${type}`]: type, [`${prefixCls}-${type}-${justify}`]: type && justify, [`${prefixCls}-${type}-${align}`]: type && align, }, className, ); const rowStyle = gutter! > 0 ? { marginLeft: gutter! / -2, marginRight: gutter! / -2, ...style, } : style; const otherProps = { ...others }; delete otherProps.gutter; return (
{children}
); }; render() { return {this.renderRow}; } }