5

I am creating dynamics tabs using ReactJS and Material UI, but I wonder how to place a functional closing tab button directly on the tab.

I mean this:

enter image description here

And by the way, is there any way to place the icon side by side with the text?

2
  • 3
    Try using iconLabelWrapper: { flexDirection: "row", justifyContent: "flex-start" } to make the icon and text side by side Commented Jul 21, 2020 at 1:22
  • 1
    This would be helpful stackoverflow.com/questions/54508683/… Commented Jul 21, 2020 at 1:25

2 Answers 2

6

Accepted answer worked great. To remove all warnings you can add component="span" to IconButton

<Tab key={key}  label={
    <span> 
        {tab.Name}
        <IconButton size="small" component="span" onClick={() => { alert("Closing this tab..."); }}>
            <CloseIcon />
        </IconButton>
    </span>
    }
/>
Sign up to request clarification or add additional context in comments.

1 Comment

better solution to avoid the warning and error. Thanks
5

I tried this and it worked for me:

<Tabs
        value={value}
        onChange={handleChange}
      >
        <Tab label={
          <span>
                    {'Active'}
                    <IconButton size="small" onClick={() => { handle() }}>
                      <CloseIcon />
                    </IconButton>
                  </span>
        } />
        <Tab label="Disabled" disabled />
        <Tab label="Active" />
      </Tabs>

2 Comments

=> Yields an 'invalid DOM nesting' warning, "button cannot be a descendent of button".
Additionally, I'm getting Warning: Failed prop type: The prop 'children' is not supported. Please remove it. with just the <span> in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.