javascript - 创建带有悬停按钮的 React 列表

标签 javascript reactjs

我正在创建一个下拉列表框,列表中的每个项目都有一个删除 (X) 按钮,用于从列表中删除该项目。当项目悬停在上方时,如何“仅”显示删除按钮?

当前代码显示每个项目的清除按钮,但我只希望它在项目悬停时显示

enter image description here

Sorry, here is the code


  import React from 'react';
   import PropTypes from 'prop-types';
   import styled from 'styled-components';

    const ListWrapper = styled.div`
  position: absolute;
  width: 16rem;
  z-index: 1;
  background: white;

  &:hover {
    cursor: pointer;
  } 

`;

const ListMenu = styled.div`
position: absolute;
width: 100%;
z-index: 1;
background: white;
overflow-x: hidden;
`;

const ListMenuHeader = styled.div`
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
`;

const DropdownText = Text.Link.extend`
  padding-top: 3rem;
`;

const DropdownButton = styled.div`
  padding:  1 rem 0.75rem;
`;

const ListMenuItem = styled.div`
  display: flex;
  background-color: grey)};
  color: grey}; 

  >[name~=icon] {
    right: 0rem;  
    border-radius: 0;
    background: none;  
    align-items: right;
    justify-content: right;  

  &:hover {
    background-color: grey)};
  }
  &:focus {
    outline: none;
  }
`;

class ListListMenu extends React.Component {
  static propTypes = {
    id: PropTypes.string.isRequired,
    text: PropTypes.node.isRequired,
    items: PropTypes.arrayOf(PropTypes.any).isRequired,
    component: PropTypes.func.isRequired,  
    selectedItem: PropTypes.any,
    getItemProps: PropTypes.func.isRequired,
    highlightedIndex: PropTypes.number,
    closeListMenu: PropTypes.func.isRequired,
  };

  static defaultProps = {
    selectedItem: null,
    highlightedIndex: null,
  }


  onClearClick = (items,item1) => (item) => {

    const index = items.indexOf(item1);
    if (index > -1) {
        items.splice(index, 1);
   }
  }

  render() {
    const {
      id, text, items, component,  selectedItem, getItemProps,
      highlightedIndex, closeListMenu,
    } = this.props; 


    return (
      <ListWrapper id={id} >
        <ListMenuHeader onClick={closeListMenu}>
          <DropdownText>{text}</DropdownText>
          <DropdownButton
            id={`${id}-button`}
          >
            <Icon type="caret-up" appearance="neutral" />
          </DropdownButton>
        </ListMenuHeader>
        <ListMenu>
          {selectedItems.map((item, index) => (
            <ListMenuItem
              {...getItemProps({
                item,
                isActive: highlightedIndex === index,
                isSelected: _.isEqual(selectedItem, item),
              })}
              key={index} 
            >
              {React.createElement(component, { item })}


              <Button   // CLEAR BUTTON
          name={item}
          id={item}
          icon="remove"
          onClick={this.onClearClick(items, item)}
          circle
          display="flat"
          appearance="disabled"
          id="clear-search-button"          
        />

            </ListMenuItem>
          ))}


        </ListMenu>
      </ListWrapper>
    );
  }
}

export default ListListMenu;

最佳答案

这是一种方法,您可以让“x”出现在悬停时。

与其寻找“hover”事件,不如寻找与“onmouseleave”组合的“onmouseenter”事件?

就像这样...

class Example extends React.Component {

  onHover() {
    this.refs.deleteX.style.display = "block";
  }

  onExit() {
    this.refs.deleteX.style.display = "none";
  }

  render() {
    return (
      <div>
        <input onmouseenter={ this.onHover } onmouseleave={ this.onExit } />
        <p ref="deleteX">x</p>
      </div>
    )
  }
}

有点像this post

关于javascript - 创建带有悬停按钮的 React 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50915539/

相关文章:

javascript - 日期转换 JavaScript 到 PHP

javascript - 在同一窗口和同一选项卡中打开 URL

javascript - 将 React 组件从函数重构为 ES6 类

javascript - 如何从容器更新组件的状态?

javascript - 内容未加载,因为其 MIME 类型 "text/html"不是 "text/css"

javascript - 无法在 jquery 自动完成中获取所选项目

影响 CSS 样式的 Javascript onChange

javascript - React Native,更改 React Navigation 标题样式

reactjs - React js 重定向不会重定向到新的 url

reactjs - 在 React 中渲染重定向总是比使用 this.props.history.push 更好吗?