简体   繁体   English

React MUI 自定义 Autocomplete 的 Popper 组件

[英]React MUI customizing Autocomplete's Popper component

I'd like to customize the width of the Popper which comes with the Autocomplete component.我想自定义自动完成组件附带的 Popper 的宽度。 In my case the Autocomplete component it is placed inside a parent component, alongside a TextField.在我的例子中,Autocomplete 组件被放置在一个父组件中,旁边是一个 TextField。

Here the snippet on Codesandbox:这里是 Codesandbox 上的片段:

编辑 BasicTextFields 材质演示(分叉)

At the moment I have the following:目前我有以下内容: 在此处输入图像描述

The parent component background color is set to green.父组件背景颜色设置为绿色。

MY GOAL: the Autocomplete Popper component should cover the entire green area , in other words, the entire area delimited by the parent component (as consequence of this also the TextField).的目标:Autocomplete Popper 组件应该覆盖整个绿色区域,换句话说,由父组件分隔的整个区域(因此也是 TextField)。

EDIT 1: after Amr Eraky answer (solving the width problem)编辑 1:在 Amr Eraky 回答之后(解决宽度问题) 在此处输入图像描述

EDIT 2: So, the expected behaviour is the following (sorry for my poor graphic skills, I hope it is understandable anyway):编辑 2:因此,预期的行为如下(对不起,我的图形技能不佳,我希望它是可以理解的):

真实的预期行为

So, we've got 2 problems here:所以,我们这里有两个问题:

  1. The width宽度
  2. The placement安置

As for the width I am able to set a custom width with the following code:至于宽度,我可以使用以下代码设置自定义宽度:

    const PopperMy = useCallback((props) => {
        return (<Popper {...props} style={{ width: 350 }} placement='right-end' />)
    },
 []);

But as you can see, this is not responsive and it is not even bound in some way to the width of the parent component, so this is not a good approach.但是正如你所看到的,这不是响应式的,它甚至没有以某种方式绑定到父组件的宽度,所以这不是一个好方法。

As for the placement, I'd like the popper to overlap its parent, but in the placement options I cannot find something like this, every option never allows the overlapping.至于放置,我希望 popper 与其父级重叠,但在放置选项中我找不到这样的东西,每个选项都不允许重叠。

Could you please suggest a way to address these issues?您能否提出解决这些问题的方法?

You should use anchorEl prop of Popper to set the position of the popper.您应该使用PopperanchorEl来设置 popper 的位置。

anchorEl : Type of : HTML element | anchorEl:类型:HTML 元素 | object |对象 | func功能
An HTML element, virtualElement, or a function that returns either. HTML 元素、virtualElement 或返回其中任何一个的函数。 It's used to set the position of the popper.它用于设置popper的位置。 The return value will passed as the reference object of the Popper instance.返回值将作为 Popper 实例的引用对象传递。

You can get anchorEl of any element using useRef ,您可以使用anchorEl获取任何元素的useRef
But because of your anchor element is in other component you can use id但是由于您的锚元素在其他组件中,您可以使用id
And set height of Popper equal to anchorEl 's height.并将Popper的高度设置为anchorEl的高度。

const PopperMy = React.useCallback((props) => {
    const anchorEl = document.getElementById('new1');   // Or any other element
    return <Popper {...props} anchorEl={anchorEl} style={{ width: anchorEl.clientWidth }} placement='bottom' />;
}, []);

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

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