row.tsx 5.0 KB

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