Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Menu] Support grouping / categories #2671

Closed
kodermax opened this issue Dec 27, 2015 · 14 comments · Fixed by #17037
Closed

[Menu] Support grouping / categories #2671

kodermax opened this issue Dec 27, 2015 · 14 comments · Fixed by #17037
Assignees
Labels
component: menu This is the name of the generic UI component, not the React module! component: select This is the name of the generic UI component, not the React module! new feature New feature or request priority: important This change can make a difference

Comments

@kodermax
Copy link
Contributor

Please add feature like this
image

@alitaheri
Copy link
Member

Thanks 👍 👍

@alitaheri alitaheri added new feature New feature or request Composability labels Dec 27, 2015
@oliviertassinari oliviertassinari changed the title DropdownMenu with categories [DropdownMenu] with categories Jan 1, 2017
@oliviertassinari oliviertassinari changed the title [DropdownMenu] with categories [Select] With categories Sep 6, 2017
@oliviertassinari oliviertassinari added component: select This is the name of the generic UI component, not the React module! and removed component: DropdownMenu labels Sep 6, 2017
@pajaydev
Copy link
Contributor

@oliviertassinari Can I try this one?.. If yes, Can I create seperate component or alter the existing select component to achieve this??.

@pajaydev
Copy link
Contributor

Okay. Will look into those issues.

@galmeida12
Copy link

hey, any updates on this feature request?

@marco-silva0000
Copy link
Contributor

Do we have any progress on this? are the more important issues that oliviertassinari listed still being worked on?

@velossien
Copy link

I am very interested in this as well! It would be fantastic to have this be a possibility without using a native select styling.

@knaos
Copy link

knaos commented Aug 14, 2018

+1 on the waiting list for this one...

@marco-silva0000
Copy link
Contributor

I've made a component that implements this, I can post a repo with this on the weekend.
It's in ES6, and I don't think it's mergable, put it's something...

@jankalfus
Copy link

As a walkaround, you can use the following snippet to get appearance pretty closely matching what the native one would be. One disadvantage to this approach is definitely accessibility though. Anyway, this works for me and might be useful to somebody else, so I'm posting it.

const Example = ({ classes }) => (
    <Select value={0}>
        <MenuItem disabled className={classes.group}>
            Nonsense
        </MenuItem>
        <MenuItem value={0} className={classes.item}>
            something
        </MenuItem>
        <MenuItem value={1} className={classes.item}>
            another
        </MenuItem>
        <MenuItem disabled className={classes.group}>
            Acronyms
        </MenuItem>
        <MenuItem value={2} className={classes.item}>
            lol
        </MenuItem>
        <MenuItem value={3} className={classes.item}>
            rofl
        </MenuItem>
    </Select>
);

const styles = (theme) => ({
    item: {
        paddingLeft: 3 * theme.spacing.unit,
    },
    group: {
        fontWeight: theme.typography.fontWeightMedium,
        opacity: 1,
    },
});

export default withStyles(styles)(Example);

image

@onzag
Copy link

onzag commented Mar 31, 2019

I get unexpected strange behaviour when doing that where I cannot debug or modify the style of the disabled groups properly, sometimes they will show gray, and they cannot be debugged on why they show as gray.

@dnnspaul
Copy link

dnnspaul commented Apr 8, 2019

Added some CSS rules and now it seems to be perfect:

  MenuItem: {
    paddingLeft: 3 * theme.spacing.unit
  },
  MenuGroup: {
    fontWeight: theme.typography.fontWeightMedium,
    opacity: 1,
    cursor: "default",
    "&:hover": {
      backgroundColor: "transparent !important"
    }
  }

@Alecell
Copy link

Alecell commented Sep 26, 2019

For the interested, I create a DRY way to the @jankalfus amazing solution:

import React from 'react';
import MenuItem from "@material-ui/core/MenuItem";
import withStyles from "@material-ui/core/styles/withStyles";

const styles = theme => ({
  item: {
    paddingLeft: theme.spacing(3),
  },
  group: {
    fontWeight: theme.typography.fontWeightMedium,
    opacity: 1,
  },
});

const customMenuItem = ({ classes, children, groupHead, groupItem, ...props}) => {
  let disabled = false;
  let className = [props.className];

  if (groupHead) {
    disabled = true;
    className.push(classes.group);
  }

  if (groupItem) {
    className.push(classes.item);
  }

  return <MenuItem
    disabled={disabled}
    className={className.join(' ')}
    {...props}>{children}</MenuItem>;
};

const AppMenuItem = withStyles(styles)(customMenuItem);

export default AppMenuItem;

And can use this way:

<AppMenuItem value='Foo'>Foo</AppMenuItem> // Just like MUI MenuItem
<AppMenuItem groupHead>Bar</AppMenuItem> // As a head
<AppMenuItem groupItem value={Baz}>Baz</AppMenuItem> // As an item

@jankalfus
Copy link

Actually, I'd recommend just creating two styled components:

const MenuGroupItem = withStyles({
    root: {
        paddingLeft: theme.spacing(3),
    },
})(MenuItem);

const MenuGroupNameBase = (props) => <MenuItem {...props} disabled />;

const MenuGroupName = withStyles({
    root: {
        fontWeight: theme.typography.fontWeightMedium,
        opacity: "1 !important",
    },
});

and use them like this:

<MenuGroupName>Some name</MenuGroupName>
<MenuGroupItem value="item1">Item 1</MenuGroupItem>
<MenuGroupItem value="item2">Item 2</MenuGroupItem>

It's more simple, without any logic. I haven't tested the code above, but you get the idea :)

@oliviertassinari oliviertassinari added component: menu This is the name of the generic UI component, not the React module! priority: important This change can make a difference and removed component: select This is the name of the generic UI component, not the React module! labels Oct 6, 2019
@oliviertassinari oliviertassinari changed the title [Select] With categories [Menu] Support grouping / categories Oct 6, 2019
@oliviertassinari oliviertassinari self-assigned this Oct 10, 2019
@oliviertassinari oliviertassinari added component: menu This is the name of the generic UI component, not the React module! component: select This is the name of the generic UI component, not the React module! new feature New feature or request priority: important This change can make a difference and removed component: menu This is the name of the generic UI component, not the React module! new feature New feature or request priority: important This change can make a difference labels Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: menu This is the name of the generic UI component, not the React module! component: select This is the name of the generic UI component, not the React module! new feature New feature or request priority: important This change can make a difference
Projects
None yet
Development

Successfully merging a pull request may close this issue.