Creating a customized SelectField component for Material-UI v1.0.0-alpha.21 with a fix for the Menu anchorEl problem

Currently, Material-UI v1.0.0 does not have a selectField implemented yet so I am attempting to create my own version using TextField, Menu, and MenuItem Components.

Below is the code for my custom selectField:

export default class SelectField extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            anchorEl: undefined,
            open: false,
        };
    }
    
    handleClick = event => {
        this.setState({ open: true, anchorEl: event.currentTarget });
    };

    handleRequestClose = () => {
        this.setState({ open: false });
    };
    
    handleMenuItemClick = (event, index) => {
        this.setState({ open: false });
        this.props.onChange(index);
    };
    
    render(){
        const { style, name, label, value, onChange, children } = this.props;
        
        return (
            <div>
            <TextField style={style} onClick={this.handleClick} type="text" name={name} label={label} value={value} InputProps={{ placeholder: label }} />
                <Menu open={this.state.open} anchorEl={this.state.anchorEl} onRequestClose={this.handleRequestClose} >
                    {children.map((key) =>
                        <MenuItem key={key.value} selected={key.value === this.props.value} onClick={event => this.handleMenuItemClick(event, key.  value)} >
                            {key.name}
                        </MenuItem>,
                    )}
                </Menu>
            </div>          
        );
    }
}

One issue I encountered is with positioning the Menu. The Material-UI documentation suggests using the anchorEl property to position the menu in front of the opening element, which works well but becomes problematic when multiple items are selected. Additionally, I would like to adjust the menu's position to be below the TextField.

Another minor concern is setting the width of the menu to match the TextField's width and enabling search functionality within the Menu.

Answer №1

Yesterday, I encountered a similar issue with the menu positioning. Upon inspecting the source code for Menu.js, I discovered that the Menu Component is enclosed within the internal Popover Component. This means that you can use the anchorOrigin prop to customize the positioning of the Menu Component. However, in my experimentation, I noticed that only the anchorOrigin prop is accepted by the component. Attempting to pass the targetOrigin prop results in an error, so I am currently exploring potential solutions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Expanding the tailwind color palette to incorporate material-ui/core theme palette is a simple process that can

I have a create-react-app project that utilizes @material-ui/core. How can I incorporate the colors from the @material-ui/core palette into my tailwind css theme? Below is my tailwind.config.js file where I am trying to extend tailwind with @material-ui/c ...

Learning fundamental MVC concepts with Express.JS

Express.js offers a basic framework for implementing the standard MVC development pattern. However, many tutorials I have come across tend to mix controller logic within route files or a global app file. In an ideal scenario: Model - Manages core behavio ...

Tips for utilizing Vue router query parameters while in hash mode:

Is there a more efficient way to access URL parameters in Vue methodology, without having to rely on window.location.href and parsing the URL? router/index.js const router = new Router({ mode: 'hash', routes: [] }); router.beforeEach((to, f ...

Leverage cookies within a custom service in AngularJS

I attempted to implement angular cookies within a custom service, only to encounter the following error: Unknown provider: ngCookiesProvider <- ngCookies <- checkLoginService My approach involves storing modules, controllers, and services in separat ...

The $(document).ready function may not be compatible with Internet Explorer

I have a page with two links - one for registering and one for logging in. Currently, the register link is the most important. When clicked, it loads a .tpl file using jQuery's load function. Within this tpl file, I include a new JavaScript file usin ...

What is the best way to adapt a wavetable for compatibility with the `OscillatorNode.setPeriodicWave` function?

I am exploring the use of a custom waveform with a WebAudio OscillatorNode. While I have basic programming skills, I am struggling with the mathematical aspects of audio synthesis. Waveforms are essentially functions, which means I can sample them. Howeve ...

Tips for importing several makeStyles in tss-react

When upgrading from MUI4 to MUI5 using tss-react, we encountered a problem with multiple styles imports in some files. const { classes } = GridStyles(); const { classes } = IntakeTableStyles(); const { classes } = CommonThemeStyles(); This resulted in ...

Updating the content within the main body of the page rather than the sidebar in react-router v5

I have a question regarding the functioning of my sidebar and content. So, when I click on any item in the sidebar, the content changes accordingly. But what if I want to change the route directly from the content itself? That's where I'm facing ...

Error occurs when attempting to change the color of a dynamically generated div, resulting in the message: "Type Error: Unable to assign a value to the property '

I'm facing an issue with my code where I have a parent div that loads with a random color, and upon clicking a button, it should create a new colored div inside. However, I keep encountering the following error: TypeError: Cannot set property ' ...

Photoswipe using identical source code, yet the output is malfunctioning

Having an issue with my code that refreshes the content of a ul element. Initially, it works fine by directly loading the ul content, but I am constantly creating new content every 10 seconds to provide users with fresh updates. Even though the source co ...

I have been working on creating a hangman game, and although I have figured out the logic behind it, I am experiencing an issue where it is not functioning properly. After inspect

I am currently working on a basic hangman game using only HTML and JavaScript. I have the logic in place, but it doesn't seem to be functioning correctly. When I inspected the element, it showed an error saying 'undefined toUppercase.' As a ...

Execute AJAX request for two individual buttons

I have a scenario where I want to use two buttons to open separate PHP pages, but I would like to trigger both buttons with a single function. The AJAX function should then determine which button was clicked and open the corresponding PHP page - for exam ...

Challenges with dependencies when updating to MUI 5

Currently in the process of upgrading MUI v4 to MUI v5, and I've thoroughly reviewed the documentation on transitioning to v5 here. Encountering an error upon running npm update npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tr ...

What is the best way to display a header element in front of an article element?

I'm struggling with making my header element sticky and appear in front of my article. Modifying the z-index hasn't given me the desired result so far. Is the z-index ineffective when dealing with floating elements? Or is there a workaround to m ...

Angular filter is causing an error message saying "Unable to interpolate," but the filter is functioning as expected

I have implemented the filter below (which I found on StackOverflow) that works perfectly fine on one page. However, when using the same object, it throws an error as shown below: app.filter('dateFormat', function dateFormat($filter){ return f ...

The mobile menu functions correctly on Jfiddle but is not functioning on the local server

I was working on a responsive mobile menu and used a toggleClass function to open the menu. It's functioning correctly in Jfiddle and below, but every time I click the nav icon in the live preview within brackets, nothing happens. I even tried it in a ...

What could be causing the Twitter Timeline to fail to load based on a variable in a Vue.js component?

My goal is to display the Twitter timeline of multiple accounts based on the route. I initially attempted to use a plugin called vue-tweet-embed, but encountered issues with it. As a result, I resorted to the traditional method by embedding twitter's ...

Loading content dynamically into a div from an external or internal source can greatly enhance user experience on a website. By

As I work on my website, I am incorporating a div structure as shown below: <div class="container"> <div class="one-third column"> <a id="tab1" href="inc/tab1.html"><h2>tab1</h2></a> </div> & ...

iOS experiences a lag in loading dynamic images

I have created a unique jquery carousel specifically designed for mobile devices. It features three containers that display three images at a time, with jquery handling the animation. By detecting the user's swipe direction (left or right), I update t ...

Exploring the depths of time travel in Redux without the aid of developer

Has anyone successfully achieved time traveling capabilities with Redux core? It seems that this feature is limited to the devtools and not advised for production use. I had planned on implementing Redux in a multiplayer game to assist with managing clie ...