【问题标题】:Automatically scroll down to next listgroup item and display the content in center in mobile view (React)自动向下滚动到下一个列表组项目并在移动视图的中心显示内容(React)
【发布时间】:2018-08-17 11:42:22
【问题描述】:

1) 我正在尝试自动滚动到列表组中的下一项。例如,如果用户回答第一个问题,它应该自动滚动到第二个问题。 (反应)和 onSubmit 它应该滚动到第一个未回答的问题
2) 当用户在移动视图中查看此列表时,“是”或“否”单选按钮应显示在中心,并且还应显示提交和清除按钮(引导)
3)如何知道从下拉列表中选择了哪个项目并在控制台中显示。

Code

【问题讨论】:

    标签: javascript reactjs bootstrap-4


    【解决方案1】:

    有多种方法可以实现这一目标。一种方法是通过“vanilla js”添加一个滚动到表单中的项目的方法,然后在 onInputChangedonSubmut 方法中使用它。

    您可以在组件中将此函数定义为:

      // Scrolls the list to a list item by list item index
      scrollToItemByIndex = (index) => {
    
        // Find the list item element (by index), and scroll wrapper element 
        const scrollItem = document.querySelector(`[scrollIndex="${ (index) }"]`)
        const scrollWrapper = document.querySelector(`[scrollWrapper="scrollWrapper"]`)
    
        if(scrollItem && scrollWrapper) {
    
          // If list item found in DOM, get the top offset
          const itemRect = scrollItem.offsetTop // [UPDATED]
          const wrapperRect = scrollWrapper.offsetTop // [UPDATED]
    
          // Scroll the wrapper to the offset of the list item we're scrolling to
          scrollWrapper.scrollTo(0, itemRect - wrapperRect)
        }
      }
    

    您的onInputChange 函数可以更新如下:

     onInputChange = ({ target }) => {
        const { cards } = this.state;
        const { options } = this.state;
    
        const nexState = cards.map((card, index) => {
          if (card.cardName !== target.name) return card;
    
          const options = card.options.map(opt => {
            const checked = opt.radioName === target.value;
            return {
              ...opt,
              selected: checked
            }
          })
    
          // [ADD] When input changes (ie something is set), scroll to next item
          this.scrollToItemByIndex( index + 1 )
    
          const style = options.every(option => !option.selected) ? 'danger' : 'info'
    
          return {
            ...card,
            style,
            options
          }
        });
        this.setState({ cards: nexState })
      }
    

    此外,您的onSubmit 将被更新以滚动到任何无效的表单项:

     onSubmit = () => {
    
        this.state.cards.forEach((card, index) => {
          var invalid = card.options.every(option => !option.selected)
          if (invalid) {
            card.style = 'danger'
    
            // [ADD] this item has invalid input, so scroll to it
            this.scrollToItemByIndex(index)
    
          }
          else {
            card.style = 'info'
          }
        });
    
        ...
     }
    

    最后,您需要使用以下内容更新组件的渲染方法,以确保上述查询选择器正常运行:

    <ul class="nav nav-pills nav-stacked anyClass" scrollWrapper="scrollWrapper">
    

    和:

    {cards.map((card, idx) => (<ListGroup bsStyle="custom" scrollIndex={idx}>
        ... 
    </  ListGroup   >)}
    

    [更新] 可以在此处找到完整的工作示例: https://stackblitz.com/edit/react-z7nhgd?file=index.js

    希望这会有所帮助!

    【讨论】:

    • link 它正在跳过第二个索引并显示第三个,当点击第三个索引时它不显示下一个索引。
    • 当我添加更多问题时会导致问题。
    • 抱歉,请参阅答案中的更新。这对你有用吗?
    • 嘿伙计 onSubmit 我们使用 this.scrollToItemByIndex(index) 。这里它取最后一个未选择的索引值。如何分配第一个索引而不是关注最后一个索引。例如,如果我错过了第一个和第三个问题。 onsubmit 它专注于第三个问题。我想展示第一个问题。
    • 当然,看看这个分叉:stackblitz.com/edit/react-hith5g?file=index.js
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多