How to show contents inside a tab? - reactjs

I am trying to create a form with three tabs.Now the tabs are created and i am able to switch between them but can not figure out how to display contents inside a tab
I have tried writing markup tags like h1 in between but nothing is shown on the output.
export class Stackreact extends Component {
constructor(props) {
super(props);
this.state = { value: '1' }
}
handle_change = (value) => {
this.setState({ value })
}
render() {
return (
<div>
<Tabs value={this.state.value} onChange={(e, v) => { this.handle_change(v) }} indicatorColor="primary"
textColor="primary" centered>
<Tab value='1' label='BASIC DETAILS'><h1>First Tab</h1></Tab>
<Tab value='2' label='CONTACT DETAILS'><h1>Second Tab</h1></Tab>
<Tab value='3' label='MORE DETAILS'><h1>Third tab</h1></Tab>
</Tabs>
</div>
)
}
}
I want the contents of h1 tags to be displayed in their respective tabs. Please help.Also i am new to this language.

After working a lot I have come up with this solution which is working fine.
render() {
let content_array = [<h1>First Tab</h1>, <h1>Second Tab</h1>, <h1>Third Tab</h1>];
return (
<div>
<Tabs value={this.state.value} onChange={(e, v) => { this.handle_change(v) }} indicatorColor="primary"
textColor="primary" centered>
<Tab value='1' label='BASIC DETAILS'></Tab>
<Tab value='2' label='CONTACT DETAILS'></Tab>
<Tab value='3' label='MORE DETAILS'></Tab>
</Tabs>
<Paper>
{content_array[this.state.value - 1]}
</Paper>
</div>
)
}

You can try using TabView and TabPanel from primereact
<TabView>
<TabPanel>
<h1>First Tab</h1>
<TabPanel />
<TabPanel>
<h1>Second Tab</h1>
<TabPanel />
<TabView />
You can research on your own on the props accepted by TabView and TabPanel

you can use components like this:
<div className={classes.root}>
<AppBar position="static">
<Tabs value={value} onChange={handleChange} aria-label="simple tabs example">
<Tab label="Item One" {...a11yProps(0)} />
<Tab label="Item Two" {...a11yProps(1)} />
<Tab label="Item Three" {...a11yProps(2)} />
</Tabs>
</AppBar>
<TabPanel value={value} index={0}>
Item One
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
</div>
official link : Tabs

You are trying to see the content but you've defined it in the wrong place, here you have an example of how to use Tabs and Tab, because you need to define the data that you want inside of each Tab outside the Tabs component (Tab in meant to be used as a self-closing tag).

Here is the simplest answer using hooks inspired by the accepted answer.
import { useState } from 'react';
..
const [tabState, setTabState] = useState(1);
...
const handleTabs = (value) => {
setTabState(value);
return;
}
let tabs_array = [<>{variable_with_contents_tab_1}</>, <>{tab_2_contents}</>];
Then in the render, you can provide the tab ui
<Paper className={classes.root}>
<Tabs
value={tabState}
onChange={(event, value) => { handleTabs(value) }}
indicatorColor="primary"
textColor="primary"
centered
>
<Tab value={1} label='Tab1'>
</Tab>
<Tab value={2} label="Tab2" />
</Tabs>
</Paper>
<Paper>
{tabs_array[tabState-1]}
</Paper>

Related

How to create custom scroll icon inside the Material-UI scrolling Tabs

I am using Scrollable tabs from Material-UI. How do I change the svg icon of the scroll arrows?
I see there is TabScrollButton component you can use. So do I have to create one for the left and right arrow?
And in Tabs component you can pass a prop ScrollButtonComponent. But how do I have to use it?
I tried below but it isn't working yet:
import Tab from '#material-ui/core/Tab';
import Tabs from '#material-ui/core/Tabs';
import TabScrollButton from '#material-ui/core/TabScrollButton';
import { CloseIcon } from 'app/components/atoms/icons';
const MyCarousel = ({ value }: Props) => {
const selectedItem = value;
const scrollButton = () => {
return (
<div>
<TabScrollButton orientation="horizontal" direction="right">
{CloseIcon}
</TabScrollButton>
</div>
);
};
return (
<div css={styles.root}>
<Tabs
value={value}
indicatorColor="primary"
variant="scrollable"
scrollButtons="auto"
aria-label="scrollable auto tabs example"
ScrollButtonComponent={scrollButton}
>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
<Tab label="Item 4" />
<Tab label="Item 5" />
<Tab label="Item 6" />
<Tab label="Item 7" />
<Tab label="Item 8" />
<Tab label="Item 9" />
</Tabs>
</div>
);
};
export default MyCarousel;
You're close. TabScrollButton is the default props of ScrollButtonComponent. Unfortunately, TabScrollButton doesn't provide any props to override the back/forward icons. So you have to create one yourself.
This is the definition of TabScrollButton. What you want to do is to copy the definition, remove unnecessary parts and add your own icons instead. Below is an example:
import ButtonBase from "#material-ui/core/ButtonBase";
import ArrowBackIcon from "#material-ui/icons/ArrowBack";
import ArrowForwardIcon from "#material-ui/icons/ArrowForward";
const MyTabScrollButton = forwardRef((props, ref) => {
const { direction, ...other } = props;
return (
<ButtonBase
component="div"
ref={ref}
style={{ opacity: other.disabled ? 0 : 1 }}
{...other}
>
{direction === "left" ? (
<ArrowBackIcon fontSize="small" />
) : (
<ArrowForwardIcon fontSize="small" />
)}
</ButtonBase>
);
});
<Tabs
{...tabsProps}
variant="scrollable"
scrollButtons="auto"
ScrollButtonComponent={MyTabScrollButton}
>
{...}
</Tabs>
Live Demo

React Metarial ui redirect to another tab after submission?

I have a /home route that has two tabs, after submitting a form (I use Formik) I want to redirect the user to the first tab in the same route. So basically
onSubmit: (values) => {
dispatch(actions.userAdded(values));
history.push(`/home`); // ??
},
doesn't work out. What is the best solution?
I am using MUI basic tab model.
<AppBar position="static" color="default">
<Tabs
value={value}
onChange={handleChange}
indicatorColor="primary"
textColor="primary"
variant="fullWidth"
aria-label="Rules"
>
<Tab label="Your Rules" {...a11yProps(0)} />
<Tab label="Add New Rule" {...a11yProps(1)} />
</Tabs>
</AppBar>
<SwipeableViews
axis={theme.direction === 'rtl' ? 'x-reverse' : 'x'}
index={value}
onChangeIndex={handleChangeIndex}
>
<TabPanel value={value} index={0} dir={theme.direction}>
<DisplayUsers /> // I WANT TO GO HERE AFTER SUBMITION
</TabPanel>
<TabPanel value={value} index={1} dir={theme.direction}>
<AddUsers /> // HERE I HAVE FORM
</TabPanel>
</SwipeableViews>

Horizontal Scroll is not working in material ui tabs?

I'm building my react js application from scratch. At some point i have use a dynamic tabs, where the length of tabs may be unable to define. I have find the code of scrollable tabs from material ui. But everything is working fine except the horizontal scroll of tab when it exceeds a particular length.The material ui code is given below.
import React from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '#material-ui/core/styles';
import AppBar from '#material-ui/core/AppBar';
import Tabs from '#material-ui/core/Tabs';
import Tab from '#material-ui/core/Tab';
import PhoneIcon from '#material-ui/icons/Phone';
import FavoriteIcon from '#material-ui/icons/Favorite';
import PersonPinIcon from '#material-ui/icons/PersonPin';
import HelpIcon from '#material-ui/icons/Help';
import ShoppingBasket from '#material-ui/icons/ShoppingBasket';
import ThumbDown from '#material-ui/icons/ThumbDown';
import ThumbUp from '#material-ui/icons/ThumbUp';
import Typography from '#material-ui/core/Typography';
import Box from '#material-ui/core/Box';
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`scrollable-force-tabpanel-${index}`}
aria-labelledby={`scrollable-force-tab-${index}`}
{...other}
>
{value === index && (
<Box p={3}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.any.isRequired,
value: PropTypes.any.isRequired,
};
function a11yProps(index) {
return {
id: `scrollable-force-tab-${index}`,
'aria-controls': `scrollable-force-tabpanel-${index}`,
};
}
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
width: '100%',
backgroundColor: theme.palette.background.paper,
},
}));
export default function TabsButton() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<div className={classes.root}>
<AppBar position="static" color="default">
<Tabs
value={value}
onChange={handleChange}
variant="scrollable"
scrollButtons="on"
indicatorColor="primary"
textColor="primary"
aria-label="scrollable force tabs example"
>
<Tab label="Item One" icon={<PhoneIcon />} {...a11yProps(0)} />
<Tab label="Item Two" icon={<FavoriteIcon />} {...a11yProps(1)} />
<Tab label="Item Three" icon={<PersonPinIcon />} {...a11yProps(2)} />
<Tab label="Item Four" icon={<HelpIcon />} {...a11yProps(3)} />
<Tab label="Item Five" icon={<ShoppingBasket />} {...a11yProps(4)} />
<Tab label="Item Six" icon={<ThumbDown />} {...a11yProps(5)} />
<Tab label="Item Seven" icon={<ThumbUp />} {...a11yProps(6)} />
<Tab label="Item Five" icon={<ShoppingBasket />} {...a11yProps(4)} />
<Tab label="Item Six" icon={<ThumbDown />} {...a11yProps(5)} />
<Tab label="Item Seven" icon={<ThumbUp />} {...a11yProps(6)} />
</Tabs>
</AppBar>
<TabPanel value={value} index={0}>
Item One
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
<TabPanel value={value} index={3}>
Item Four
</TabPanel>
<TabPanel value={value} index={4}>
Item Five
</TabPanel>
<TabPanel value={value} index={5}>
Item Six
</TabPanel>
<TabPanel value={value} index={6}>
Item Seven
</TabPanel>
<TabPanel value={value} index={4}>
Item Five
</TabPanel>
<TabPanel value={value} index={5}>
Item Six
</TabPanel>
<TabPanel value={value} index={6}>
Item Seven
</TabPanel>
</div>
);
}
Help will be appreciated

React Material UI - Tabs - How to disable lazy loading of tabs?

I am creating some react tabs based off of the example shown here for Material UI: https://material-ui.com/components/tabs/.
The problem I am encountering is that my tabs are lazy loading the linegraph components instead of performing all the queries on initial page load. Does anyone have some suggestions of where I can optimize this to make each tab load immediately on page load?
function TabPanel(props) {
const { children, value, index} = props;
return (
<Typography>
{value === index && <Box p={3}>{children}</Box>}
</Typography>
);
}
class SimpleTabs extends Component {
constructor(props) {
super(props)
console.log(props.color)
this.state = {
value: 0
};
}
handleChange = (event, newValue) => {
this.setState({value: newValue});
};
render() {
return (
<div>
<AppBar position="static">
<Tabs value={this.state.value} onChange={this.handleChange}>
<Tab label="Item One"/>
<Tab label="Item Two"/>
<Tab label="Item Three"/>
</Tabs>
</AppBar>
<TabPanel value={this.state.value} index={0}>
<LineGraph className={styles.graphBox} name={"Guest Count"} url={'http://127.0.0.1:5000/***/***'} initialFilterData={this.props.initialFilterData} filterData={this.props.filterData}/>
</TabPanel>
<TabPanel value={this.state.value} index={1}>
<LineGraph className={styles.graphBox} name={"Total Sales"} url={'http://127.0.0.1:5000/***/***'} initialFilterData={this.props.initialFilterData} filterData={this.props.filterData}/>
</TabPanel>
<TabPanel value={this.state.value} index={2}>
<LineGraph className={styles.graphBox} name={"Avg Check Size"} url={'http://127.0.0.1:5000/***/***'} initialFilterData={this.props.initialFilterData} filterData={this.props.filterData}/>
</TabPanel>
</div>
)}
}
export default SimpleTabs;
Add hidden={value !== index} to the Typography in TabPanel and remove the value === index && condition inside the Typography so that the children of all the tab panels are rendered immediately but hidden (except for the currently selected one).
Example:
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
{...other}
>
<Box p={3}>{children}</Box>
</Typography>
);
}

Resetting view to default tab with react-web-tabs

My default tab is Income. I have a dropdown component on top of Tab. If the value in dropdown changes i need to reset the view to default tab(income). Please help me on this.
<Tabs
defaultTab="income"
onChange={(tabId) => { console.log(tabId) }}>
<TabList>
<Tab tabFor="income">Income</Tab>
<Tab tabFor="expense">Expense</Tab>
<Tab tabFor="liability">Liabilities</Tab>
</TabList>
<TabPanel tabId="income">
<p>Income</p>
</TabPanel>
<TabPanel tabId="expense">
<p>Expense</p>
</TabPanel>
<TabPanel tabId="liability">
<p>Liability</p>
</TabPanel>
</Tabs>
Make your defaultTab value as a state so it will be dynamic either and update accordingly when needed(in your case onChange value of your dropdown component.
e.g.
class TabSample extends React.Component {
state = {
defaultTab: 'Income'
}
onDropdownChange = () => {
// reset default tab value to initial state
this.setState({defaultTab: 'Income'})
}
render(){
const { defaultTab } = this.state;
return <Tabs
defaultTab={defaultTab}
onChange={(tabId) => { console.log(tabId) }}>
<TabList>
<Tab tabFor="income">Income</Tab>
<Tab tabFor="expense">Expense</Tab>
<Tab tabFor="liability">Liabilities</Tab>
</TabList>
<TabPanel tabId="income">
<p>Income</p>
</TabPanel>
<TabPanel tabId="expense">
<p>Expense</p>
</TabPanel>
<TabPanel tabId="liability">
<p>Liability</p>
</TabPanel>
</Tabs>
}
}

Resources