【发布时间】:2021-08-06 18:33:44
【问题描述】:
我想知道如何在 React 中关闭一系列模态窗口。我想要的是当有一个已经打开时,前一个将被关闭。
export const Labels = ({ id, content, color }) => {
const [changeColor, setChangeColor] = useState(false);
const hanldeChange = () => { };
return (
<>
<div className="px-3 border-2 border-blur-lg flex justify-between m-5 rounded-md">
<div className="flex py-3">
<BlockColor color={color} />
<input
type="text"
id="txtLabel"
name="txtLabel"
className="text-base ring-white focus:ring-transparent mx-3 text-gray-700 outline-none"
value={content}
onChange={hanldeChange}
/>
</div>
<div className="flex py-3">
<button className="mx-2">
<img
src={`./assets/Pantone.svg`}
alt="New Label"
onClick={() => setChangeColor(!changeColor)}
/>
</button>
<button className="mx-3">
<img src={`./assets/Delete.svg`} alt="Delete" />
</button>
<ModalColor
changeColorState={changeColor}
setChangeColorState={setChangeColor}
/>
</div>
</div>
</>
);
};
它是一个模态组件,接收三个属性,ID、内容和颜色。这是动态生成的,每个“标签”都有一个 ID、内容和颜色。当他们按下一个按钮时,它会触发另一个显示可用颜色的模式。每次他们单击按钮更改颜色时,窗口就会打开,如果他们转到另一个标签并再次按下,则会打开一个新标签。
我想知道的是如何做到这一点,以便我知道当一个模式窗口打开以选择颜色,而另一个模式窗口打开以选择颜色时,它会关闭。
export const ModalColor = ({ changeColorState, setChangeColorState }) => {
const [blockColor, setBlockColor] = useState("#409FFF");
const handleColorChange = (e) => {
setBlockColor(e.target.value);
setChangeColorState(false);
};
return (
<>
{changeColorState && (
<div className="container absolute bg-white -right-2 top-auto mt-7 shadow-lg border-2 w-64 m-5 rounded-md overflow-x-hidden">
<p className="mx-2 text-gray-700 font-medium">Change Color</p>
<div className="mx-3 grid grid-cols-5 gap-4 my-2 rounded-full">
<input
type="radio"
className="cursor-pointer form-radio h-6 w-6 text-rojo"
value="#FD7972"
checked
readOnly
onClick={handleColorChange}
/>
<input
type="radio"
value="#FE9F5E"
checked
readOnly
onClick={handleColorChange}
className="cursor-pointer form-radio h-6 w-6 text-naranja"
/>
<input
type="radio"
value="#FFD454"
checked
readOnly
onClick={handleColorChange}
className="cursor-pointer form-radio h-6 w-6 text-amarillo"
/>
</div>
<p>{blockColor}</p>
</div>
)}
</>
);
};
【问题讨论】:
-
你有多种方法可以做到这一点,我认为最好的一种是添加
active类到你点击的标签对应的模态,点击其他改变颜色后,清除所有'活动类,然后将活动类添加到应该打开的类。 'active' 类使模态可见,因此如果它不存在于模态的类列表中,它将不会出现。
标签: javascript reactjs web react-redux progressive-web-apps