row.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Implementation reference from: https://github.com/ant-design/ant-design/blob/master/components/grid/row.tsx
  3. */
  4. import React from 'react';
  5. import classnames from 'classnames';
  6. import PropTypes from 'prop-types';
  7. import { cssClasses } from '@douyinfe/semi-foundation/grid/constants';
  8. import '@douyinfe/semi-foundation/grid/grid.scss';
  9. import { registerMediaQuery } from '../_utils';
  10. const responsiveArray = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
  11. export const RowContext = React.createContext<{ gutters: Gutter | [Gutter, Gutter] }>(null);
  12. export type Breakpoint = 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs';
  13. export type Gutter = number | Partial<Record<Breakpoint, number>>;
  14. export interface RowProps {
  15. type?: 'flex';
  16. align?: 'top' | 'middle' | 'bottom';
  17. justify?: 'start' | 'end' | 'center' | 'space-around' | 'space-between';
  18. className?: string;
  19. style?: React.CSSProperties;
  20. children?: React.ReactNode;
  21. gutter?: Gutter | [Gutter, Gutter];
  22. prefixCls?: string;
  23. }
  24. export interface RowState {
  25. screens: Partial<Record<Breakpoint, boolean>>;
  26. }
  27. const responsiveMap = {
  28. xs: '(max-width: 575px)',
  29. sm: '(min-width: 576px)',
  30. md: '(min-width: 768px)',
  31. lg: '(min-width: 992px)',
  32. xl: '(min-width: 1200px)',
  33. xxl: '(min-width: 1600px)',
  34. };
  35. class Row extends React.Component<RowProps, RowState> {
  36. static propTypes = {
  37. type: PropTypes.oneOf(['flex']),
  38. align: PropTypes.oneOf(['top', 'middle', 'bottom']),
  39. justify: PropTypes.oneOf(['start', 'end', 'center', 'space-around', 'space-between']),
  40. className: PropTypes.string,
  41. style: PropTypes.object,
  42. children: PropTypes.node,
  43. gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]),
  44. prefixCls: PropTypes.string,
  45. };
  46. static defaultProps = {
  47. prefixCls: cssClasses.PREFIX,
  48. };
  49. static RowContext = {
  50. gutters: PropTypes.any,
  51. };
  52. state = {
  53. screens: {
  54. xs: true,
  55. sm: true,
  56. md: true,
  57. lg: true,
  58. xl: true,
  59. xxl: true,
  60. }
  61. };
  62. unRegisters: Array<() => void> = [];
  63. componentDidMount() {
  64. this.unRegisters = Object.keys(responsiveMap).map(screen => registerMediaQuery(responsiveMap[screen], {
  65. match: () => {
  66. if (typeof this.props.gutter !== 'object') {
  67. return;
  68. }
  69. this.setState(prevState => ({
  70. screens: {
  71. ...prevState.screens,
  72. [screen]: true,
  73. },
  74. }));
  75. },
  76. unmatch: () => {
  77. if (typeof this.props.gutter !== 'object') {
  78. return;
  79. }
  80. this.setState(prevState => ({
  81. screens: {
  82. ...prevState.screens,
  83. [screen]: false,
  84. },
  85. }));
  86. },
  87. }));
  88. }
  89. componentWillUnmount() {
  90. this.unRegisters.forEach(unRegister => unRegister());
  91. }
  92. getGutter() {
  93. const { gutter = 0 } = this.props;
  94. const results: [number, number] = [0, 0];
  95. const normalizedGutter = Array.isArray(gutter) ? gutter.slice(0, 2) : [gutter, 0];
  96. normalizedGutter.forEach((g, index) => {
  97. if (typeof g === 'object') {
  98. for (let i = 0; i < responsiveArray.length; i++) {
  99. const breakpoint = responsiveArray[i];
  100. if (this.state.screens[breakpoint] && g[breakpoint] !== undefined) {
  101. results[index] = g[breakpoint];
  102. break;
  103. }
  104. }
  105. } else {
  106. results[index] = g || 0;
  107. }
  108. });
  109. return results;
  110. }
  111. render() {
  112. const { prefixCls, type, justify, align, className, style, children, ...others } = this.props;
  113. const gutters = this.getGutter();
  114. const prefix = `${prefixCls}-row`;
  115. const classes = classnames(
  116. {
  117. [prefix]: type !== 'flex',
  118. [`${prefix}-${type}`]: type,
  119. [`${prefix}-${type}-${justify}`]: type && justify,
  120. [`${prefix}-${type}-${align}`]: type && align,
  121. },
  122. className
  123. );
  124. const rowStyle = {
  125. ...(gutters[0] > 0 ?
  126. {
  127. marginLeft: gutters[0] / -2,
  128. marginRight: gutters[0] / -2,
  129. } :
  130. {}),
  131. ...(gutters[1] > 0 ?
  132. {
  133. marginTop: gutters[1] / -2,
  134. marginBottom: gutters[1] / -2,
  135. } :
  136. {}),
  137. ...style,
  138. };
  139. const otherProps = { ...others };
  140. delete otherProps.gutter;
  141. return (
  142. <RowContext.Provider
  143. value={{
  144. gutters,
  145. }}
  146. >
  147. <div {...otherProps} className={classes} style={rowStyle}>
  148. {children}
  149. </div>
  150. </RowContext.Provider>
  151. );
  152. }
  153. }
  154. export default Row;