【问题标题】:How to implement a simple react multistep control如何实现一个简单的反应多步控制
【发布时间】:2021-10-06 08:10:28
【问题描述】:

我是 CSS 的新手,并尝试基于一些现有代码实现多步 React 控件(目前这些步骤是静态的,一旦我发现了我的问题,我就会让事情变得动态):

import styles from "./ProgressSteps.module.css";

const ACProgressSteps = () =>{
return(
    <div className={styles.container}>
        <ul className={`${styles.progressbar} ${styles.nobullets}`}>
            <li className={styles.selected}>Aircraft Type</li>
            <li>Weight  Index</li>
            <li>Airport Times</li>
            <li>Documents</li>
            <li>Tada</li>
        </ul>
    </div>
)}

活动步骤由{styles.selected} 标识(之前应用此样式的所有步骤也应标记为绿色)。对应的样式如下(我在自己的控件中导入):

.container{
    width: 100%;
    position: absolute;
    z-index: 1;
}

.progressbar{
    counter-reset: step;
}



ul.nobullets {
    list-style-type: none; /* Remove bullets */
    padding: 0; /* Remove padding */
    margin: 0; /* Remove margins */
}

.progressbar li{
    float: left;
    width: 20%;
    position: relative;
    text-align: center;
}

.progressbar li:before{
    counter-increment: step;
    content:counter(step);
    width: 30px;
    height: 30px;
    border: 2px solid #bebebe;
    display: block;
    margin: 0 auto 10px auto;
    border-radius: 50%;
    line-height: 27px;
    background: white;
    color: #bebebe;
    text-align: center;
    font-weight: bold;
  }
  

.progressbar li:after{
    content: '';
    position: absolute;
    width:100%;
    height: 3px;
    background: #979797;
    top: 15px;
    left: -50%;
    z-index: -1;
  }

.progressbar li:first-child:after{
    content: none;
}

.progressbar li.selected + li:after{
    background: #3aac5d;
}

.progressbar li.selected + li:before{
   border-color: #3aac5d;
   background: #3aac5d;
   color: white
}

但是,在前面的示例中,没有选择第一个“步骤”,而是选择了第二个步骤,如下图所示(似乎我遇到了某种“问题”):

任何解决此问题的帮助将不胜感激!

编辑:为了感谢原作者,我改编的例子可以在这里找到:https://steemit.com/utopian-io/@alfarisi94/how-to-make-step-progress-bar-only-using-css

【问题讨论】:

    标签: css reactjs css-selectors


    【解决方案1】:

    + 符号选择器选择紧跟在指定元素之后的元素,因此li.selected + li:before&lt;li&gt;Weight Index&lt;/li&gt; 为目标,因为它就在li 之后,类名为.selected

    以下将解决它:

    .progressbar li.selected:after {
      background: #3aac5d;
    }
    
    .progressbar li.selected:before {
      border-color: #3aac5d;
      background: #3aac5d;
      color: white;
    }
    

    const ACProgressSteps = () => {
      return (
        <div className='container'>
          <ul className='progressbar nobullets'>
            <li className='selected'>Aircraft Type</li>
            <li>Weight Index</li>
            <li>Airport Times</li>
            <li>Documents</li>
            <li>Tada</li>
          </ul>
        </div>
      );
    };
    
    ReactDOM.render(<ACProgressSteps />, document.getElementById('root'));
    .container {
      width: 100%;
      position: absolute;
      z-index: 1;
    }
    
    .progressbar {
      counter-reset: step;
    }
    
    ul.nobullets {
      list-style-type: none; /* Remove bullets */
      padding: 0; /* Remove padding */
      margin: 0; /* Remove margins */
    }
    
    .progressbar li {
      float: left;
      width: 20%;
      position: relative;
      text-align: center;
    }
    
    .progressbar li:before {
      counter-increment: step;
      content: counter(step);
      width: 30px;
      height: 30px;
      border: 2px solid #bebebe;
      display: block;
      margin: 0 auto 10px auto;
      border-radius: 50%;
      line-height: 27px;
      background: white;
      color: #bebebe;
      text-align: center;
      font-weight: bold;
    }
    
    .progressbar li:after {
      content: "";
      position: absolute;
      width: 100%;
      height: 3px;
      background: #979797;
      top: 15px;
      left: -50%;
      z-index: -1;
    }
    
    .progressbar li:first-child:after {
      content: none;
    }
    
    .progressbar li.selected:after {
      background: #3aac5d;
    }
    
    .progressbar li.selected:before {
      border-color: #3aac5d;
      background: #3aac5d;
      color: white;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
    <div id="root"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-02
      • 2012-09-19
      • 2012-03-09
      • 2020-09-02
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多