简体   繁体   English

悬停时获取ant-design Select下拉值

[英]Get the ant-design Select dropdown value when hover

How can we find the ant-design dropdown hovered value?我们如何找到 ant-design 下拉悬停值?

 <Select defaultValue="lucy" style={{ width: 120 }} onChange={handleChange}>
  <Option value="jack">Jack</Option>
  <Option value="lucy">Lucy</Option>
  <Option value="Yiminghe">yiminghe</Option>
</Select>

Suppose when I hover any value from dropdown, I should read the hovered value.假设当我从下拉菜单中悬停任何值时,我应该读取悬停的值。

I think this is not supported by antd select out of the box, but you can catch the mouseevents eg by adding an onMouseEnter to each antd select option .我认为antd select即用的antd select不支持这一点,但是您可以捕获鼠标事件,例如通过向每个 antd select option添加一个onMouseEnter

<Option value="jack" onMouseEnter={handleChange}>
   Jack
</Option>

Here is working stackblitz.是工作堆栈闪电战。

Complete code from the stackblitz:来自 stackblitz 的完整代码:

import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import { Select } from 'antd';

const { Option } = Select;

function CustomSelect() {
  const [value, setValue] = useState('');

  function handleChange(value) {
    const hoverText = value.target.innerText;
    setValue(hoverText);
    console.log(`hover ${hoverText}`);
  }

  return (
    <div style={{ display: 'flex' }}>
      <div>
        <Select
          defaultValue="lucy"
          style={{ width: 120 }}
          onChange={handleChange}
        >
          <Option value="jack" onMouseEnter={handleChange}>
            Jack
          </Option>
          <Option value="lucy" onMouseEnter={handleChange}>
            Lucy
          </Option>
          <Option value="Yiminghe" onMouseEnter={handleChange}>
            yiminghe
          </Option>
        </Select>
      </div>
      <div>last hover value: {value}</div>
    </div>
  );
}

ReactDOM.render(<CustomSelect />, document.getElementById('container'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM