前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ant design源码分析 2 Grid栅格

ant design源码分析 2 Grid栅格

作者头像
mafeifan
发布2018-09-10 11:57:34
1.3K1
发布2018-09-10 11:57:34
举报
文章被收录于专栏:finleyMafinleyMa

使用文档 源码

grid/index.tsx

代码语言:javascript
复制
import Row from './row';
import Col from './col';

export {
  Row,
  Col,
};

grid/row.js

代码语言:javascript
复制
export default class Row extends React.Component {
  static defaultProps = {
    // gutter是col之间的间隔,默认0
    // <Row gutter={24}> 
    // 生成 <div class="ant-row" style="margin-left: -8px; margin-right: -8px"></div>
    gutter: 0,
  };

  render() {
    const { type, justify, align, className, gutter, style, children,
      prefixCls = 'ant-row', ...others } = this.props;

    // https://ant.design/components/grid-cn/#components-grid-demo-gutter
    // 默认class只有一个ant-row
    // type 一般是flex
    // 如果传入<Row justify="center"> 则输出 ant-row-flex-center
 
    const classes = classNames({
      [prefixCls]: !type,
      [`${prefixCls}-${type}`]: type,
      [`${prefixCls}-${type}-${justify}`]: type && justify,
      [`${prefixCls}-${type}-${align}`]: type && align,
    }, className);

    // 汇总style,如果gutter属性大于0设置marginLeft,marginRight。gutter最好满足(16+8n)px
    const rowStyle = gutter > 0 ? {
      marginLeft: gutter / -2,
      marginRight: gutter / -2,
      ...style
    } : style;

    // 下面的比较简单,对每一个col设置 paddingLeft 和 paddingRight
    const cols = Children.map(children, (col) => {
      if (!col) {
        return null;
      }
      if (col.props && gutter > 0) {
        return cloneElement(col, {
          style: {
            paddingLeft: gutter / 2,
            paddingRight: gutter / 2,
            ...col.props.style
          },
        });
      }
    })

    return(
      <div {...others} className={classes} style={rowStyle}>{cols}</div>
    )
  }
}

grid/col.js

col也比较简单,需要对 flex布局 比较熟悉

代码语言:javascript
复制
export default class Col extends React.Component {
  render() {
    const props = this.props;
    const { span, order, offset, push, pull, className, children, prefixCls = 'ant-col', ...others } = props;
    let sizeClassObj = {};

    ['xs', 'sm', 'md', 'lg', 'xl'].forEach(size => {
      let sizeProps = {};
      if (typeof props[size] === 'number') {
        sizeProps.span = props[size];
      } else if (typeof props[size] === 'object') {
        sizeProps = props[size] || {};
      }

      delete others[size];

      sizeClassObj = {
        ...sizeClassObj,
        [`${prefixCls}-${size}-${sizeProps.span}`]: sizeProps.span !== undefined,
        [`${prefixCls}-${size}-order-${sizeProps.order}`]: sizeProps.order || sizeProps.order === 0,
        [`${prefixCls}-${size}-offset-${sizeProps.offset}`]: sizeProps.offset || sizeProps.offset === 0,
        [`${prefixCls}-${size}-push-${sizeProps.push}`]: sizeProps.push || sizeProps.push === 0,
        [`${prefixCls}-${size}-pull-${sizeProps.pull}`]: sizeProps.pull || sizeProps.pull === 0,
      };
    });

    // 汇总style,如果gutter属性大于0,gutter最好满足(16+8n)px
    // const colStyle = span > 0 ? {
    //   marginLeft: gutter / -2,
    //   marginRight: gutter / -2,
    //   ...style
    // } : style;

    const classes = classNames({
      [`${prefixCls}-${span}`]: span !== undefined,
      [`${prefixCls}-order-${order}`]: order,
      [`${prefixCls}-offset-${offset}`]: offset,
      [`${prefixCls}-push-${push}`]: push,
      [`${prefixCls}-pull-${pull}`]: pull,
    }, className, sizeClassObj);

    return(
      <div className={classes}>{children}</div>
    )
  }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.11.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • grid/index.tsx
  • grid/row.js
  • grid/col.js
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档