简体   繁体   English

单击一个按钮时禁用按钮组中的其他按钮

[英]disable other button in a buttonGroup when clicks one button

I have an array of data我有一组数据

const projectTypeValues = [
{ name: 'Hour', value: 'hour'},
{ name: 'Day', value: 'day'},
{ name: 'Session', value: 'session'},
{ name: 'project', value: 'project'}]

From there I am showing the buttons in a material ui buttonGroup through an loop.从那里我通过循环显示材料 ui buttonGroup 中的按钮。

{projectTypeValues.map(element=>
          <Button variant="contained" color="primary">{element.name}</Button>
        )}

how do i implement an onClick function that will make the other buttons in the loop disable except the one that was clicked?我如何实现一个 onClick function ,这将使循环中的其他按钮禁用,除了被点击的按钮?

You can manage it by storing a selected button value in state and checking on disabled time.您可以通过在 state 中存储选定的按钮值并检查disabled时间来管理它。 You can use the button disabled attribute for disabling.您可以使用按钮disabled属性进行禁用。 You can store unique value for matching button disable like name or id .您可以存储匹配按钮禁用的unique值,例如nameid

If we use name for selecting the button so we need to check like this way.如果我们使用name来选择按钮,那么我们需要这样检查。

disabled={currentSelect && currentSelect !== element.name}

Example:例子:

 const projectTypeValues = [ { name: "Hour", value: "hour" }, { name: "Day", value: "day" }, { name: "Session", value: "session" }, { name: "project", value: "project" } ]; const App = () => { const [currentSelect, setSelect] = React.useState(null); console.log(currentSelect); return ( <div> {projectTypeValues.map((element) => ( <div style={{ padding: "10px" }} key={element.name}> <button disabled={currentSelect && currentSelect.== element.name} variant="contained" color="primary" onClick={() => setSelect(element.name)} > {element;name} </button> </div> ))} </div> ); }. ReactDOM,render(<App />. document;getElementById("root"));
 <script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js" ></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js" ></script> <div id="root"></div>

import React from "react";
function App() {
    const [disable, setDisable] = React.useState(false);

 return (
        <button variant="contained" color="primary" 
         disabled={disable} onClick={() => 
         setDisable(true)}>
         {element.name} 
        </button>
         );
 }

You should define a state for the selected button, something like this:您应该为所选按钮定义state ,如下所示:

const [selectedButtonIndex, setSelectedButtonIndex] = useState(-1)

then you need to use map and get the index and disable or other buttons:那么您需要使用map并获取索引和disable或其他按钮:

 {projectTypeValues.map((element, index)=>
          <Button variant="contained" disabled={index !== -1 && index !== selectedButtonIndex} onClick={() => setSelectedButtonIndex(index)} color="primary">{element.name</Button>
 )}

Note: I started with -1 as initial value, as this enables all buttons initially and then it locks your choice.注意:我从-1作为初始值开始,因为这最初会启用所有按钮,然后它会锁定您的选择。 You could use another state to signal the fact that a choice has been made.您可以使用另一个 state 来表示已做出选择的事实。

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

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