ant-design/components/grid/row.tsx

163 lines
4.4 KiB
TypeScript
Raw Normal View History

import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
2017-11-01 18:33:41 +08:00
// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
2017-11-21 20:34:06 +08:00
let enquire: any;
2017-11-01 18:33:41 +08:00
if (typeof window !== 'undefined') {
2018-09-30 16:07:01 +08:00
const matchMediaPolyfill = (mediaQuery: string) => {
2017-11-01 18:33:41 +08:00
return {
media: mediaQuery,
matches: false,
2018-12-07 20:02:01 +08:00
addListener() {},
removeListener() {},
2017-11-01 18:33:41 +08:00
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
2017-11-01 19:08:16 +08:00
enquire = require('enquire.js');
2017-11-01 18:33:41 +08:00
}
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';
2017-11-21 20:34:06 +08:00
export type Breakpoint = 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs';
2018-05-17 21:41:25 +08:00
export type BreakpointMap = Partial<Record<Breakpoint, string>>;
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;
}
const responsiveArray: Breakpoint[] = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
2017-11-01 20:31:26 +08:00
2017-11-21 20:34:06 +08:00
const responsiveMap: BreakpointMap = {
2017-11-01 18:33:41 +08:00
xs: '(max-width: 575px)',
2017-11-01 20:31:26 +08:00
sm: '(min-width: 576px)',
md: '(min-width: 768px)',
lg: '(min-width: 992px)',
xl: '(min-width: 1200px)',
2017-11-01 18:33:41 +08:00
xxl: '(min-width: 1600px)',
};
2017-11-21 20:34:06 +08:00
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
};
2017-11-21 20:34:06 +08:00
2017-11-01 18:33:41 +08:00
componentDidMount() {
2018-12-07 20:02:01 +08:00
Object.keys(responsiveMap).map((screen: Breakpoint) =>
enquire.register(responsiveMap[screen], {
match: () => {
if (typeof this.props.gutter !== 'object') {
return;
}
this.setState(prevState => ({
screens: {
...prevState.screens,
[screen]: true,
},
}));
},
unmatch: () => {
if (typeof this.props.gutter !== 'object') {
return;
}
this.setState(prevState => ({
screens: {
...prevState.screens,
[screen]: false,
},
}));
2017-11-01 20:31:26 +08:00
},
2018-12-07 20:02:01 +08:00
// Keep a empty destory to avoid triggering unmatch when unregister
destroy() {},
}),
);
2017-11-01 18:33:41 +08:00
}
componentWillUnmount() {
2018-12-07 20:02:01 +08:00
Object.keys(responsiveMap).map((screen: Breakpoint) =>
enquire.unregister(responsiveMap[screen]),
);
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 as number) > 0
? {
marginLeft: (gutter as number) / -2,
marginRight: (gutter as number) / -2,
...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>;
}
}