【发布时间】:2019-04-24 13:07:23
【问题描述】:
【问题讨论】:
-
请添加Minimal, Complete and Verifiable example 向我们展示您到目前为止所做的工作。
-
非常好的问题,我今天也遇到了同样的问题!
标签: reactjs react-select
【问题讨论】:
标签: reactjs react-select
在你所有的Select标签中添加这些行:
<Select
// other props
menuPortalTarget={document.body}
styles={{ menuPortal: base => ({ ...base, zIndex: 9999 }) }}
/>
【讨论】:
overflow: hidden,仅仅将menu的z-index设置为9999并不能阻止父容器裁剪的弹出窗口。
menuPortalTarget={document.body} 成功了
试试这种设置 zIndex 的 hacky 方法,如果它有效,请告诉我 :)
<Select
styles={{
// Fixes the overlapping problem of the component
menu: provided => ({ ...provided, zIndex: 9999 })
}}
value={selectedOption}
onChange={evt => onSelectChange(evt, index)}
options={options}
/>
【讨论】:
menuPortal: provided => ({ ...provided, zIndex: 9999 })
看了半打相关问题,还没找到解决办法,终于找到了。
<Select
menuPortalTarget={document.body}
menuPosition={'fixed'}
/>
将这两个选项添加到您的 Select 组件中。
这似乎让我们使用了新的React Portal 功能。
编辑:如果您执行上述操作,那么由于position: fixed,您会遇到菜单锚定到页面的问题。删除它可能会有所帮助。 https://github.com/JedWatson/react-select/issues/4088
【讨论】:
对我来说,解决方案是所有答案的总和(谢谢!);
const customStyles = {
///.....
menuPortal: provided => ({ ...provided, zIndex: 9999 }),
menu: provided => ({ ...provided, zIndex: 9999 })
///.....
}
<Select
//.....
menuPortalTarget={document.body}
menuPosition={'fixed'}
styles={customStyles}
//.....
/>
【讨论】:
另一种方法是我们可以配置classNamePrefix并通过外部类控制它。
import Select from "react-select";
<Select
classNamePrefix={"my-custom-react-select"}
/>
.my-custom-react-select__menu {
z-index: 2;
}
奖金,我们可以重新设计一切
.my-custom-react-select__control {
border-width: 1px !important;
border-color: #cbd5e0 !important;
padding-top: 0.16rem;
padding-bottom: 0.16rem;
}
.my-custom-react-select__control--is-focused {
border-color: #746df7 !important;
box-shadow: none !important;
}
.my-custom-react-select__indicator-separator {
display: none;
}
【讨论】:
只有这些答案的组合才能在Next.js 上为我提供可行的解决方案。 menuPortalTarget={document.body} 用错误 ReferenceError: document is not defined 破坏了应用程序,这在 SSG/SSR 上是有意义的 :)
menuPosition={"fixed"} 由@I Stand With Israel 建议结合来自@hemant rao 的答案:menuPortal: (base) => ({ ...base, zIndex: 4 }),。
【讨论】:
改变这个select的父组件的zIndex
<div style={{zIndex:1000}}>
<React-Select-Component/>
</div>
【讨论】: