React Tabs content - reactjs

I was playing around with React for the first time and trying to figure out how to make tabs. I found an outline of the code that looks simple and clean enough (do correct me if I'm wrong though, please), but I don't know how to show the content of the tabs outside of the 'constructor'. As I want the tab content to be more than just a couple of words, and the current code seems constricting.
This is the part that I'm talking about:
constructor(props) {
super(props);
this.state = {
activeTabIndex: 0,
initialData: [
{
label: 'Tab 1',
content: 'Content 1',
},
{
label: 'Tab 2',
content: 'Content 2',
},
{
label: 'Tab 3',
content: 'Content 3',
},
],
};
this.handleTabClick = this.handleTabClick.bind(this);
}
How do I pull the content outside of this, if I have too much content to be put inside that? Here is the entire code outline: link. Any suggestions/links are welcome.
And I apologize if I'm explaining this wrong, I'm still trying to figure stuff out. Thanks!

Just create a Component you want to render in Tab Content and assign it to content.
import React, { Component } from "react";
import Tabs from "./Tabs";
import Content from "./Content";
export default class TabView extends Component {
constructor(props) {
super(props);
this.state = {
activeTabIndex: 0,
initialData: [
{
label: "Tab 1",
content: <TabContent />
},
{
label: "Tab 2",
content: "Content 2"
},
{
label: "Tab 3",
content: "Content 3"
}
]
};
this.handleTabClick = this.handleTabClick.bind(this);
}
handleTabClick(index) {
this.setState({
activeTabIndex: index
});
}
render() {
const { initialData, activeTabIndex } = this.state;
const activeItem = this.state.initialData[activeTabIndex];
return (
<div>
<Tabs
handleTabClick={this.handleTabClick}
data={this.state.initialData}
activeTabIndex={activeTabIndex}
/>
<Content content={activeItem.content} />
</div>
);
}
}
const TabContent = () => (
<div>
<p>I am here</p>
</div>
);

Related

Ant Design for React : Show/Hide particular column

I need a bit of help here, In an Ant Design table, I need to hide/show a particular column of a table depending on a state value. In the given sandbox link, I need to hide the surname column whenever the switch is Off and show when the switch is On.
Please, someone, look into this, and help me out.
Reference: https://codesandbox.io/s/purple-sun-1rtz1?file=/index.js
There is a working code, but it should be more customize, interactivize, and refactorize depending on your need:
// You can also modify the data in the `handleChnage`
// Or conditionally display it like here:
class EditableTable extends React.Component {
state = {
surNameShow: false
};
constructor(props) {
super(props);
this.columns = [
{
title: "Name",
dataIndex: "name",
width: "30%"
},
{
title: "Surname",
dataIndex: "surname",
width: "30%"
}
];
this.state = {
dataSource: [
{
key: "0",
name: "Edward 1",
surname: "King 1"
},
{
key: "1",
name: "Edward 2",
surname: "King 2"
}
]
};
}
handleChnage = key => {
this.setState({ surNameShow: !this.state.surNameShow }, () => {
console.log(this.state.surNameShow);
});
};
render() {
const { dataSource } = this.state;
const columns = this.columns;
return (
<div>
<p className="mr-3"> Show surname</p>
<Switch onChange={() => this.handleChnage()} />
<Table
bordered
dataSource={dataSource}
columns={
this.state.surNameShow
? columns
: columns.filter(ea => ea.dataIndex !== "surname")
}
pagination={false}
/>
</div>
);
}
}
ReactDOM.render(<EditableTable />, document.getElementById("container"));

fullcalendar react wrapper scheduler passing events

I'm trying to use fullcalendar-react-wrapper-scheduler in my project.
The documentation shows an example of passing events into the FullCalendar component, however it does not show how to pass in resources.
I'm attempting to pass in "resources" by mimicking how "events" are being passed in. But that does not display any resources on the DOM.
Has anyone successfully used this package that can provide guidance for passing in resources?
Documentation:
https://www.npmjs.com/package/fullcalendar-reactwrapper-scheduler#examples
Here's a code snippet showing how I am passing in events (successfully) and resources (not successfully):
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import Nav from '../../components/Nav/Nav';
import { USER_ACTIONS } from '../../redux/actions/userActions';
import { LOGIN_ACTIONS } from '../../redux/actions/loginActions';
//START CALENDAR LIBRARY IMPORTS//
import FullCalendar from 'fullcalendar-reactwrapper-scheduler';
import 'fullcalendar-reactwrapper-scheduler/dist/css/fullcalendar.min.css';
//END CALENDAR LIBRARY IMPORTS//
const mapStateToProps = state => ({
user: state.user,
});
class ExampleComponent extends Component {
constructor(props) {
super(props);
this.state = {
events: [
{
resourceId: 'a',
id: 1,
title: 'Shoot 1',
start: '2017-06-27T08:00:00',
end: '2017-06-27T09:00:00'
},
{
resourceId: 'b',
id: 2,
title: 'Shoot 2',
start: '2017-06-27T10:00:00',
end: '2017-06-27T11:00:00'
},
{
resourceId: 'a',
id: 3,
title: 'Shoot 3',
start: '2017-06-27T13:00:00',
end: '2017-06-27T14:00:00'
},
{
resourceId: 'c',
id: 4,
title: 'Shoot 4',
start: '2017-06-27T08:00:00',
end: '2017-06-27T09:00:00'
},
{
resourceId: 'd',
id: 5,
title: 'Shoot 5',
start: '2017-06-27T012:00:00',
end: '2017-06-27T13:00:00'
},
],
resources: [
{ id: 'a', title: 'Room A' },
{ id: 'b', title: 'Room B' },
{ id: 'c', title: 'Room C' },
{ id: 'd', title: 'Room D' },
]
}
}
componentDidMount() {
this.props.dispatch({
type: USER_ACTIONS.FETCH_USER
});
}
componentDidUpdate() {
if (!this.props.user.isLoading && this.props.user.userName === null) {
this.props.history.push('home');
}
}
logout = () => {
this.props.dispatch({
type: LOGIN_ACTIONS.LOGOUT
});
// this.props.history.push('home');
}
render() {
let content = null;
if (this.props.user.userName) {
content = (
<div id="example-component">
<FullCalendar
id="your-custom-ID"
header={{
left: 'prev,next today myCustomButton',
center: 'title',
right: 'month,basicWeek,basicDay'
}}
defaultDate={'2017-06-27'}
navLinks={true} // can click day/week names to navigate views
editable={true}
eventLimit={true} // allow "more" link when too many events
events={this.state.events}
resources={this.state.resources}
defaultView='agendaDay'
/>
</div>
);
}
return (
<div>
<Nav />
{content}
</div>
);
}
}
// this allows us to use <App /> in index.js
export default connect(mapStateToProps)(ExampleComponent);
Looking through the source code, it looks like fullcalendar-reactwrapper-scheduler doesn't support resources.
You have a couple of options. You can use another library that is specifically made for React, such as react-calendar. This is the best approach.
If for some reason you are absolutely set on using Fullcalendar, you can integrate jQuery with your React app and then use Fullcalendar directly without the wrapper. But using jQuery with React is just asking for trouble, so I strongly advise against this approach.

React : Best way to put dummy content in a component

I want to prepare my component to receive an JSON object from the backend.
So in my main component, I've got a state :
constructor(props) {
super(props);
this.state = {
contributions : {
[
{
name: 'Test',
value: '1',
},
{
name: 'Test2',
value: '12',
},
]
}
render() {
const { contributions } = this.state;
return (
<MyComponent contributions={contributions} />
);
}
So now, I want to know the best solution to render my object in MyComponent so I can have for output :
<div>
<span class="name">Test</span>
<span class="value">1</span>
</div>
<div>
<span class="name">Test2</span>
<span class="value">12</span>
</div>
JSON objects are key-value pairs. So if you're saving the JSON to your state, it can look something like
this.state = {
contributions : {
"nameValuePairs":[
{
"name": 'Test',
"value": '1',
},
{
"name": 'Test2',
"value": '12',
},
]
}
Now to map through the objects you can do something like
{this.state.contributions.nameValuePairs.map((item)=>(<TestChild item={item}/>))
Bascially your parent would look something like
import React, { Component } from 'react';
import TestChild from './TestChild'
class Test extends Component {
constructor(props) {
super(props);
this.state = {
contributions : {
"nameValuePairs":[
{
"name": 'Test',
"value": '1',
},
{
"name": 'Test2',
"value": '12',
},
]
}
}
}
render() {
return (
<div>
{this.state.contributions.nameValuePairs.map((item)=>(<TestChild item={item}/>))}
</div>
);
}
}
export default Test;
and your child component can have inside it, something like
render () {
return (
<div>{this.props.item.name}</div>
<div>{this.props.item.value}</div>
)
}
MyComponent
<div>
<span class="name">{this.props.name}</span>
<span class="value">{this.props.value}</span>
</div>
ParentComponent
constructor(props) {
super(props);
this.state = {
contributions : {
[
{
name: 'Test',
value: '1',
},
{
name: 'Test2',
value: '12',
},
]
}
render() {
const { contributions } = this.state;
return (
contributions && contributions.length && contributions.map(contrib => (
<MyComponent {...contrib) />
))
);
}

How do I access my state inside semantic-ui-react components?

I am trying to build a menu using the semantic-ui-react Menu component. But I am unable to use my state inside the Menu.Item component to display individual menu items. How can I use my state inside the Menu.Item
class JobTabs extends React.Component{
constructor(props){
super(props);
this.state={
tabs: [
{title: 'Tab 1', index: 0},
{title: 'Tab 2', index: 1},
{title: 'Tab 3', index: 2}
],
activeTab: 0
}
this.renderTab = this.renderTab.bind(this);
}
renderTab(){
return this.state.tabs.map((tab) => {
return (
<Menu.Item
name={tab.name}
key={tab.index}
active={this.state.activetab === tab.index} />
);
})
}
render() {
return (
<Menu tabular>
{this.renderTab()}
</Menu>
);
}
}
use title instead name
class JobTabs extends React.Component{
constructor(props){
super(props);
this.state={
tabs: [
{title: 'Tab 1', index: 0},
{title: 'Tab 2', index: 1},
{title: 'Tab 3', index: 2}
],
activeTab: 0
}
this.renderTab = this.renderTab.bind(this);
}
renderTab(){
return this.state.tabs.map((tab) => {
return (
<Menu.Item
name={tab.title}
key={tab.index}
active={this.state.activeTab === tab.index} />
);
})
}
render() {
return (
<Menu tabular>
{this.renderTab()}
</Menu>
);
}
}

No errors can't return data from map function?

In the browser console I get the object. But when I try and return just the text, link, title nothing returns. I get the object in the console, but when I try and add .text or .link the page shows blank. The console shows no errors.
import React, { Component } from 'react';
class Navigation extends Component {
getLinks = (object) => {
Object.keys(object).map((property, i) => {
const { text, title } = object[property][i];
console.log(object[property][i])
return (
<ul>{title}
<li>{text}</li>
</ul>
)
});
}
render() {
let { items } = this.props;
return (
<div>
<ul>
{this.getLinks(items)}
</ul>
</div>
);
}
}
export default Navigation;
JSON
const navigation = {
pageInfo: [
{title: "A title"},
{text: "item 1", link: "https://google.com"},
{text: " item 2", link: "https://googleconquestio.com"}
],
pageInfo2: [
{title: "A title"},
{text: "item 1", link: "https://google.com"},
{text: "item 2", link: "https://googleconquestio.com"}
],
pageInfo3: [
{title: "Applications"},
{text: "application item 1", link: "https://google.com"},
{text: "application item 2", link: "https://googleconquestio.com"}
]
}
A couple things are happening. First you are not returning the newly created array that map returns. Second you will also need to loop through each of your keys in your navigation object since they are arrays. You are trying to grab {title, text} from object[property][i], but that won't work since title is at index 0 and text is at index 1. I would recommend re-structuring your data.
You could do something like this:
const navigation = [
{
title: 'Title 1',
text: [
{
name: 'item 1',
},
{
name: 'item 2',
}
],
},
{
title: 'Title 2',
text: [
{
name: 'item 1',
},
{
name: 'item 2',
}
],
}
];
return navigation.map(obj => {
let listElements = obj.text.map((li, c) => {
return (
<li key={c}>{li.name}</li>
);
});
return (
<ul>
{obj.title}
{listElements}
</ul>
);
});
You need to have a return in the arrow function if you use braces that way:
Here is an example JSBin: https://jsbin.com/mijuh/2/edit?js,console
getLinks = (object) => {
return Object.keys(object).map((property, i) => {
const { text, title } = object[property][i];
console.log(object[property][i])
return (
<ul>{title}
<li>{text}</li>
</ul>
)
});

Resources