ant-design/components/grid/row.tsx
陈帅 3e32364dc0
New component Descriptions (#14645)
* add new component: DescriptionList

* add warning message

* docs: fix doc typo

* feat: implement the size attribute

* docs: fix doc typo

* refactor: use new name Descriptions

* test: snapshots updated

* feat: support react15

* style: fix code style warring

* style: better var name

* style: better code style

* style: merge css class

* feat: add responsive config

* fix: fix error title

* style: use @border-radius-base

* update snapshot

* feat: set default column

* test: add test script

* style: fix property defaultProps is useless error

* style: more robust code

* style: fix codereview warning

* style: fix review warning

* use responsiveObserveserve

* fix review warning

* bug: add childrenArray copy,prevent changes to incoming parameters

* fix dom error

* fix typo

* fix test

* don't use this

* snapshot updated

* prettier md

* remove descriptions md text

* new rendering method

* doc :add dot

* style: add right border
2019-05-22 23:22:09 +08:00

113 lines
3.0 KiB
TypeScript

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<HTMLDivElement> {
gutter?: number | Partial<Record<Breakpoint, number>>;
type?: 'flex';
align?: (typeof RowAligns)[number];
justify?: (typeof RowJustify)[number];
prefixCls?: string;
}
export interface RowState {
screens: BreakpointMap;
}
export default class Row extends React.Component<RowProps, RowState> {
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 (
<RowContext.Provider value={{ gutter }}>
<div {...otherProps} className={classes} style={rowStyle}>
{children}
</div>
</RowContext.Provider>
);
};
render() {
return <ConfigConsumer>{this.renderRow}</ConfigConsumer>;
}
}