Skip to content

关于defaultTabBar长度的可扩展性问题 #23

@Jemair

Description

@Jemair

今天在开发的时候遇到这样一个问题
设计稿中tabBarItem每个长度为页面的1/6 当不足6个时不自动填满宽度而是将右侧留白 效果如下
image

但是在defaultTabBar中 是使用js动态添加行内样式到组件上 导致标签样式难以覆盖
同时使用!important强行覆盖之后 由于underline宽度和位置也是写死的 导致underline长度和位置都无法与tab标签对齐

期望增加tabWidth属性 可以自定义设置tab宽度 同时使用offsetLeft动态计算underline位置 可以增强组件的扩展性 自定义tabBar代码如下
(当tab为垂直时使用offsetTop)

export default class TabBar extends PureComponent {
  render() {
    return (
      <div className={s.tabBar}>
        {this.renderTabs()}
        {this.renderUnderline()}
      </div>
    )
  }

  renderTabs = () => {
    const { tabs, activeTab, goToTab } = this.props
    return tabs.map((i, index) =>
      <div
        key={i.key}
        ref={el => { this[index] = el }}
        onClick={() => { goToTab(index) }}
        // .tab { width: 16% }
        className={`${s.tab} ${activeTab === index ? s.active : ''}`}>
        {i.title}
      </div>
    )
  }

  renderUnderline = () => {
    const { activeTab } = this.props
    const offsetLeft = (this[activeTab] && this[activeTab].offsetLeft) || 0
    const offset = {
      transform: `translateX(${offsetLeft}px)`,
      WebkitTransform: `translateX(${offsetLeft}px)`,
    }
    // .underline 宽度默认与.tab一致 但通过覆盖样式也可以自定义宽度
    return <div className={s.underline} style={offset} />
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions