简体   繁体   English

在 material-ui \ core \ Slider 中单击获取值

[英]Get Value clicked in material-ui \ core \ Slider

Development - react.发展——反应。

Task:任务:

Display a line with dots that mark percentages between 0 and 100.显示一条带点的线,用于标记 0 到 100 之间的百分比。

The data comes outside the component (for the purpose of the example I created a static array).数据来自组件之外(出于示例的目的,我创建了一个 static 数组)。

The quantity and value of the values is dynamic.价值的数量和价值是动态的。

The task is required to click on a point, for which purpose it is necessary to know at which point in the click event which clicked.该任务需要单击一个点,为此需要知道在单击事件中的哪个点单击了哪个。

The solution I tried:我尝试的解决方案:

Using material-ui \ core \ Slider.使用 material-ui\core\Slider。

I disabled the ability of the slide so that the points remain static by updating the value each time in the original values.我禁用了幻灯片的功能,以便通过每次更新原始值中的值来使点保持 static。

The only event that exists in Slider is onChange it happens when you click anywhere on Slider. Slider 中存在的唯一事件是 onChange,当您单击 Slider 上的任意位置时,它会发生。 But the event does not return the location where you clicked but the new values it wants to calculate.但是该事件不会返回您单击的位置,而是返回它要计算的新值。 And by clicking on one of the points the values simply remain as they are unchanged.通过单击其中一个点,值将保持不变。

My code:我的代码:

import React from 'react';
import { Slider, Tooltip } from '@material-ui/core';

export default function App() {
  
  const marks = [
    { value: 0, label: '0%' },
    { value: 100, label: '100%' },
  ];
  
  const ValueLabelComponent = (props) => {
    const { children, open, value } = props;
      return (
          <Tooltip open={open} enterTouchDelay={0} placement="bottom" title={`${value}%`}>
            {children}
          </Tooltip>
      );
  }

  return (
    <div className='container'>
      <Slider
        value={[ 0, 5, 70, 88, 93]}
        ValueLabelComponent={ValueLabelComponent}
        track={false}
        onChange={(event, value) => { console.log(event, value) }}
        marks={marks}
      />
    </div>
  );
}

My questions are:我的问题是:

  1. Does anyone have any idea how to get the value of the pressed point?有谁知道如何获得按下点的值?

  2. Or alternatively whether there is a solution in the use of another component.或者,是否存在使用其他组件的解决方案。

Try this:尝试这个:

import React from 'react';
import { Slider, Tooltip } from '@material-ui/core';

export default function App() {
  
  const marks = [
    // Save the fetched values here instead of under <Slider value=...>
    { value: 0, label: '0%' },
    { value: 5, label: '5%' },
    { value: 70, label: '70%' },
    { value: 88, label: '88%' },
    { value: 93, label: '93%' },
    { value: 100, label: '100%' },
  ];
  
  const ValueLabelComponent = (props) => {
    const { children, open, value } = props;
      return (
          <Tooltip open={open} enterTouchDelay={0} placement="bottom" title={`${value}%`}>
            {children}
          </Tooltip>
      );
  }

  return (
    <div className='container'>
      <Slider
        ValueLabelComponent={ValueLabelComponent}
        track={false}
        onChange={(event, value) => console.log(`Value of ${value} clicked`) }
        marks={marks}
        steps={null}
      />
    </div>
  );
}

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

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