row.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import classNames from 'classnames';
  12. import { responsiveArray } from '../_util/responsiveObserver';
  13. import { ConfigContext } from '../config-provider';
  14. import useBreakpoint from './hooks/useBreakpoint';
  15. import useGutter from './hooks/useGutter';
  16. import RowContext from './RowContext';
  17. import { useRowStyle } from './style';
  18. const _RowAligns = ['top', 'middle', 'bottom', 'stretch'];
  19. const _RowJustify = ['start', 'end', 'center', 'space-around', 'space-between', 'space-evenly'];
  20. function useMergedPropByScreen(oriProp, screen) {
  21. const [prop, setProp] = React.useState(typeof oriProp === 'string' ? oriProp : '');
  22. const calcMergedAlignOrJustify = () => {
  23. if (typeof oriProp === 'string') {
  24. setProp(oriProp);
  25. }
  26. if (typeof oriProp !== 'object') {
  27. return;
  28. }
  29. for (let i = 0; i < responsiveArray.length; i++) {
  30. const breakpoint = responsiveArray[i];
  31. // if do not match, do nothing
  32. if (!screen || !screen[breakpoint]) {
  33. continue;
  34. }
  35. const curVal = oriProp[breakpoint];
  36. if (curVal !== undefined) {
  37. setProp(curVal);
  38. return;
  39. }
  40. }
  41. };
  42. React.useEffect(() => {
  43. calcMergedAlignOrJustify();
  44. }, [JSON.stringify(oriProp), screen]);
  45. return prop;
  46. }
  47. const Row = /*#__PURE__*/React.forwardRef((props, ref) => {
  48. const {
  49. prefixCls: customizePrefixCls,
  50. justify,
  51. align,
  52. className,
  53. style,
  54. children,
  55. gutter = 0,
  56. wrap
  57. } = props,
  58. others = __rest(props, ["prefixCls", "justify", "align", "className", "style", "children", "gutter", "wrap"]);
  59. const {
  60. getPrefixCls,
  61. direction
  62. } = React.useContext(ConfigContext);
  63. const screens = useBreakpoint(true, null);
  64. const mergedAlign = useMergedPropByScreen(align, screens);
  65. const mergedJustify = useMergedPropByScreen(justify, screens);
  66. const prefixCls = getPrefixCls('row', customizePrefixCls);
  67. const [wrapCSSVar, hashId, cssVarCls] = useRowStyle(prefixCls);
  68. const gutters = useGutter(gutter, screens);
  69. const classes = classNames(prefixCls, {
  70. [`${prefixCls}-no-wrap`]: wrap === false,
  71. [`${prefixCls}-${mergedJustify}`]: mergedJustify,
  72. [`${prefixCls}-${mergedAlign}`]: mergedAlign,
  73. [`${prefixCls}-rtl`]: direction === 'rtl'
  74. }, className, hashId, cssVarCls);
  75. // Add gutter related style
  76. const rowStyle = {};
  77. const horizontalGutter = gutters[0] != null && gutters[0] > 0 ? gutters[0] / -2 : undefined;
  78. if (horizontalGutter) {
  79. rowStyle.marginLeft = horizontalGutter;
  80. rowStyle.marginRight = horizontalGutter;
  81. }
  82. // "gutters" is a new array in each rendering phase, it'll make 'React.useMemo' effectless.
  83. // So we deconstruct "gutters" variable here.
  84. const [gutterH, gutterV] = gutters;
  85. rowStyle.rowGap = gutterV;
  86. const rowContext = React.useMemo(() => ({
  87. gutter: [gutterH, gutterV],
  88. wrap
  89. }), [gutterH, gutterV, wrap]);
  90. return wrapCSSVar(/*#__PURE__*/React.createElement(RowContext.Provider, {
  91. value: rowContext
  92. }, /*#__PURE__*/React.createElement("div", Object.assign({}, others, {
  93. className: classes,
  94. style: Object.assign(Object.assign({}, rowStyle), style),
  95. ref: ref
  96. }), children)));
  97. });
  98. if (process.env.NODE_ENV !== 'production') {
  99. Row.displayName = 'Row';
  100. }
  101. export default Row;