Create a collapsible card component












0















I want to present a few different types of information, in separate cards. Each Card, needs to be able to open and close when clicking on the Header.



Up until now, I wrote this piece of code:



import React, { Component } from 'react';
import { PropTypes } from 'prop-types';

// Importing Styles
import './styles.scss';

class CardToggle extends Component {
static propTypes = {
title: PropTypes.string.isRequired
};

constructor(props) {
super(props);

this.state = {
isOpen: false
};

this.handleToggleClick = this.handleToggleClick.bind(this);
}

handleToggleClick = () => {
this.setState({
isOpen: !isOpen
});
};

render() {
const { title } = this.props;
return (
<div className="card my-2 border-0 rounded-0">
<div className="card-body d-flex">
<h4>
<a className="text-secondary" href="">
{title}
</a>
</h4>
</div>
</div>
);
}
}

export default CardToggle;


I have a few problems though. There is an error, regarding handleToggleClick not being defined. Also, every component I use inside this component is not being placed inside the card body: It is placed underthing...



So, I need to actually place the content of the component I want to make appear and disappear inside the component, and make the collapse component work.



I am using pure Bootstrap 4 classes, for cards and header and text, and I want to use pure react for the functionality of open/close....



Here is an example component I want to insert into card component:



import React, { Component } from 'react';
import CardToggle from '../CardToggle';

import { ThePermissions } from '../../constants';

class Permissions extends Component {
render() {
return (
<div>
<CardToggle title="Permissions" />
<button className="btn btn-primary">Submit</button> // From
here it is outside the card
<div>
More Content here
</div>
</div>
);
}
}

export default Permissions;


Can anyone give me a hand. I don't want to use a library and I want the component to be reusable. I also want to handle additional logic regarding show/hide, in the main overview screen which is already huge.



If you want to see that as well, just ping me.










share|improve this question

























  • I want to add, that I don't want an accordion. It doesn't suit either the design or the functionality of the screen. It must be cards, and be able to be reused in multiple screens.

    – Dimitris Efst
    Jan 3 at 14:38











  • please show us how are you using handleToggleClick, you have a bug inside the method, you can't do isOpen: !isOpen, the negated isOpen is undefined since you don't have a variable with that name, you shoud do isOpen: !this.state.isOpen, and is not a good practice either, you should use the callback function, since you are using the value of the state to generate a new one, and setState is async, a working example codesandbox.io/s/ywkzpj1x7x

    – ramirozap
    Jan 4 at 9:51
















0















I want to present a few different types of information, in separate cards. Each Card, needs to be able to open and close when clicking on the Header.



Up until now, I wrote this piece of code:



import React, { Component } from 'react';
import { PropTypes } from 'prop-types';

// Importing Styles
import './styles.scss';

class CardToggle extends Component {
static propTypes = {
title: PropTypes.string.isRequired
};

constructor(props) {
super(props);

this.state = {
isOpen: false
};

this.handleToggleClick = this.handleToggleClick.bind(this);
}

handleToggleClick = () => {
this.setState({
isOpen: !isOpen
});
};

render() {
const { title } = this.props;
return (
<div className="card my-2 border-0 rounded-0">
<div className="card-body d-flex">
<h4>
<a className="text-secondary" href="">
{title}
</a>
</h4>
</div>
</div>
);
}
}

export default CardToggle;


I have a few problems though. There is an error, regarding handleToggleClick not being defined. Also, every component I use inside this component is not being placed inside the card body: It is placed underthing...



So, I need to actually place the content of the component I want to make appear and disappear inside the component, and make the collapse component work.



I am using pure Bootstrap 4 classes, for cards and header and text, and I want to use pure react for the functionality of open/close....



Here is an example component I want to insert into card component:



import React, { Component } from 'react';
import CardToggle from '../CardToggle';

import { ThePermissions } from '../../constants';

class Permissions extends Component {
render() {
return (
<div>
<CardToggle title="Permissions" />
<button className="btn btn-primary">Submit</button> // From
here it is outside the card
<div>
More Content here
</div>
</div>
);
}
}

export default Permissions;


Can anyone give me a hand. I don't want to use a library and I want the component to be reusable. I also want to handle additional logic regarding show/hide, in the main overview screen which is already huge.



If you want to see that as well, just ping me.










share|improve this question

























  • I want to add, that I don't want an accordion. It doesn't suit either the design or the functionality of the screen. It must be cards, and be able to be reused in multiple screens.

    – Dimitris Efst
    Jan 3 at 14:38











  • please show us how are you using handleToggleClick, you have a bug inside the method, you can't do isOpen: !isOpen, the negated isOpen is undefined since you don't have a variable with that name, you shoud do isOpen: !this.state.isOpen, and is not a good practice either, you should use the callback function, since you are using the value of the state to generate a new one, and setState is async, a working example codesandbox.io/s/ywkzpj1x7x

    – ramirozap
    Jan 4 at 9:51














0












0








0








I want to present a few different types of information, in separate cards. Each Card, needs to be able to open and close when clicking on the Header.



Up until now, I wrote this piece of code:



import React, { Component } from 'react';
import { PropTypes } from 'prop-types';

// Importing Styles
import './styles.scss';

class CardToggle extends Component {
static propTypes = {
title: PropTypes.string.isRequired
};

constructor(props) {
super(props);

this.state = {
isOpen: false
};

this.handleToggleClick = this.handleToggleClick.bind(this);
}

handleToggleClick = () => {
this.setState({
isOpen: !isOpen
});
};

render() {
const { title } = this.props;
return (
<div className="card my-2 border-0 rounded-0">
<div className="card-body d-flex">
<h4>
<a className="text-secondary" href="">
{title}
</a>
</h4>
</div>
</div>
);
}
}

export default CardToggle;


I have a few problems though. There is an error, regarding handleToggleClick not being defined. Also, every component I use inside this component is not being placed inside the card body: It is placed underthing...



So, I need to actually place the content of the component I want to make appear and disappear inside the component, and make the collapse component work.



I am using pure Bootstrap 4 classes, for cards and header and text, and I want to use pure react for the functionality of open/close....



Here is an example component I want to insert into card component:



import React, { Component } from 'react';
import CardToggle from '../CardToggle';

import { ThePermissions } from '../../constants';

class Permissions extends Component {
render() {
return (
<div>
<CardToggle title="Permissions" />
<button className="btn btn-primary">Submit</button> // From
here it is outside the card
<div>
More Content here
</div>
</div>
);
}
}

export default Permissions;


Can anyone give me a hand. I don't want to use a library and I want the component to be reusable. I also want to handle additional logic regarding show/hide, in the main overview screen which is already huge.



If you want to see that as well, just ping me.










share|improve this question
















I want to present a few different types of information, in separate cards. Each Card, needs to be able to open and close when clicking on the Header.



Up until now, I wrote this piece of code:



import React, { Component } from 'react';
import { PropTypes } from 'prop-types';

// Importing Styles
import './styles.scss';

class CardToggle extends Component {
static propTypes = {
title: PropTypes.string.isRequired
};

constructor(props) {
super(props);

this.state = {
isOpen: false
};

this.handleToggleClick = this.handleToggleClick.bind(this);
}

handleToggleClick = () => {
this.setState({
isOpen: !isOpen
});
};

render() {
const { title } = this.props;
return (
<div className="card my-2 border-0 rounded-0">
<div className="card-body d-flex">
<h4>
<a className="text-secondary" href="">
{title}
</a>
</h4>
</div>
</div>
);
}
}

export default CardToggle;


I have a few problems though. There is an error, regarding handleToggleClick not being defined. Also, every component I use inside this component is not being placed inside the card body: It is placed underthing...



So, I need to actually place the content of the component I want to make appear and disappear inside the component, and make the collapse component work.



I am using pure Bootstrap 4 classes, for cards and header and text, and I want to use pure react for the functionality of open/close....



Here is an example component I want to insert into card component:



import React, { Component } from 'react';
import CardToggle from '../CardToggle';

import { ThePermissions } from '../../constants';

class Permissions extends Component {
render() {
return (
<div>
<CardToggle title="Permissions" />
<button className="btn btn-primary">Submit</button> // From
here it is outside the card
<div>
More Content here
</div>
</div>
);
}
}

export default Permissions;


Can anyone give me a hand. I don't want to use a library and I want the component to be reusable. I also want to handle additional logic regarding show/hide, in the main overview screen which is already huge.



If you want to see that as well, just ping me.







javascript reactjs bootstrap-4 collapse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 14:44









Uwe Keim

27.7k32134216




27.7k32134216










asked Jan 3 at 14:36









Dimitris EfstDimitris Efst

168113




168113













  • I want to add, that I don't want an accordion. It doesn't suit either the design or the functionality of the screen. It must be cards, and be able to be reused in multiple screens.

    – Dimitris Efst
    Jan 3 at 14:38











  • please show us how are you using handleToggleClick, you have a bug inside the method, you can't do isOpen: !isOpen, the negated isOpen is undefined since you don't have a variable with that name, you shoud do isOpen: !this.state.isOpen, and is not a good practice either, you should use the callback function, since you are using the value of the state to generate a new one, and setState is async, a working example codesandbox.io/s/ywkzpj1x7x

    – ramirozap
    Jan 4 at 9:51



















  • I want to add, that I don't want an accordion. It doesn't suit either the design or the functionality of the screen. It must be cards, and be able to be reused in multiple screens.

    – Dimitris Efst
    Jan 3 at 14:38











  • please show us how are you using handleToggleClick, you have a bug inside the method, you can't do isOpen: !isOpen, the negated isOpen is undefined since you don't have a variable with that name, you shoud do isOpen: !this.state.isOpen, and is not a good practice either, you should use the callback function, since you are using the value of the state to generate a new one, and setState is async, a working example codesandbox.io/s/ywkzpj1x7x

    – ramirozap
    Jan 4 at 9:51

















I want to add, that I don't want an accordion. It doesn't suit either the design or the functionality of the screen. It must be cards, and be able to be reused in multiple screens.

– Dimitris Efst
Jan 3 at 14:38





I want to add, that I don't want an accordion. It doesn't suit either the design or the functionality of the screen. It must be cards, and be able to be reused in multiple screens.

– Dimitris Efst
Jan 3 at 14:38













please show us how are you using handleToggleClick, you have a bug inside the method, you can't do isOpen: !isOpen, the negated isOpen is undefined since you don't have a variable with that name, you shoud do isOpen: !this.state.isOpen, and is not a good practice either, you should use the callback function, since you are using the value of the state to generate a new one, and setState is async, a working example codesandbox.io/s/ywkzpj1x7x

– ramirozap
Jan 4 at 9:51





please show us how are you using handleToggleClick, you have a bug inside the method, you can't do isOpen: !isOpen, the negated isOpen is undefined since you don't have a variable with that name, you shoud do isOpen: !this.state.isOpen, and is not a good practice either, you should use the callback function, since you are using the value of the state to generate a new one, and setState is async, a working example codesandbox.io/s/ywkzpj1x7x

– ramirozap
Jan 4 at 9:51












1 Answer
1






active

oldest

votes


















0














1.For the error of undefined, instead of



handleToggleClick = () => {
this.setState({
isOpen: !isOpen
});
};


try



handleToggleClick() {
this.setState({
isOpen: !isOpen
});
}


2.for the content not appearing inside the component try:



render() {
const { title, children } = this.props;
return (
<div className="card my-2 border-0 rounded-0">
<div className="card-body d-flex">
<h4>
<a className="text-secondary" href="">
{title}
</a>
</h4>
<div className="/*some class*/">
{children} // this is where the content will be shown
</div>
</div>
</div>
);
}





share|improve this answer
























  • For the second issue. You are right. For the first though, the handleToggleClick, it says the same error. handleToggleClick is not defined`. Funny thing is, I am doing it exactly as the React docs say to do it... Thanks though. You helped a lot..

    – Dimitris Efst
    Jan 3 at 15:49












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024400%2fcreate-a-collapsible-card-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














1.For the error of undefined, instead of



handleToggleClick = () => {
this.setState({
isOpen: !isOpen
});
};


try



handleToggleClick() {
this.setState({
isOpen: !isOpen
});
}


2.for the content not appearing inside the component try:



render() {
const { title, children } = this.props;
return (
<div className="card my-2 border-0 rounded-0">
<div className="card-body d-flex">
<h4>
<a className="text-secondary" href="">
{title}
</a>
</h4>
<div className="/*some class*/">
{children} // this is where the content will be shown
</div>
</div>
</div>
);
}





share|improve this answer
























  • For the second issue. You are right. For the first though, the handleToggleClick, it says the same error. handleToggleClick is not defined`. Funny thing is, I am doing it exactly as the React docs say to do it... Thanks though. You helped a lot..

    – Dimitris Efst
    Jan 3 at 15:49
















0














1.For the error of undefined, instead of



handleToggleClick = () => {
this.setState({
isOpen: !isOpen
});
};


try



handleToggleClick() {
this.setState({
isOpen: !isOpen
});
}


2.for the content not appearing inside the component try:



render() {
const { title, children } = this.props;
return (
<div className="card my-2 border-0 rounded-0">
<div className="card-body d-flex">
<h4>
<a className="text-secondary" href="">
{title}
</a>
</h4>
<div className="/*some class*/">
{children} // this is where the content will be shown
</div>
</div>
</div>
);
}





share|improve this answer
























  • For the second issue. You are right. For the first though, the handleToggleClick, it says the same error. handleToggleClick is not defined`. Funny thing is, I am doing it exactly as the React docs say to do it... Thanks though. You helped a lot..

    – Dimitris Efst
    Jan 3 at 15:49














0












0








0







1.For the error of undefined, instead of



handleToggleClick = () => {
this.setState({
isOpen: !isOpen
});
};


try



handleToggleClick() {
this.setState({
isOpen: !isOpen
});
}


2.for the content not appearing inside the component try:



render() {
const { title, children } = this.props;
return (
<div className="card my-2 border-0 rounded-0">
<div className="card-body d-flex">
<h4>
<a className="text-secondary" href="">
{title}
</a>
</h4>
<div className="/*some class*/">
{children} // this is where the content will be shown
</div>
</div>
</div>
);
}





share|improve this answer













1.For the error of undefined, instead of



handleToggleClick = () => {
this.setState({
isOpen: !isOpen
});
};


try



handleToggleClick() {
this.setState({
isOpen: !isOpen
});
}


2.for the content not appearing inside the component try:



render() {
const { title, children } = this.props;
return (
<div className="card my-2 border-0 rounded-0">
<div className="card-body d-flex">
<h4>
<a className="text-secondary" href="">
{title}
</a>
</h4>
<div className="/*some class*/">
{children} // this is where the content will be shown
</div>
</div>
</div>
);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 14:53









marius4896marius4896

12




12













  • For the second issue. You are right. For the first though, the handleToggleClick, it says the same error. handleToggleClick is not defined`. Funny thing is, I am doing it exactly as the React docs say to do it... Thanks though. You helped a lot..

    – Dimitris Efst
    Jan 3 at 15:49



















  • For the second issue. You are right. For the first though, the handleToggleClick, it says the same error. handleToggleClick is not defined`. Funny thing is, I am doing it exactly as the React docs say to do it... Thanks though. You helped a lot..

    – Dimitris Efst
    Jan 3 at 15:49

















For the second issue. You are right. For the first though, the handleToggleClick, it says the same error. handleToggleClick is not defined`. Funny thing is, I am doing it exactly as the React docs say to do it... Thanks though. You helped a lot..

– Dimitris Efst
Jan 3 at 15:49





For the second issue. You are right. For the first though, the handleToggleClick, it says the same error. handleToggleClick is not defined`. Funny thing is, I am doing it exactly as the React docs say to do it... Thanks though. You helped a lot..

– Dimitris Efst
Jan 3 at 15:49




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024400%2fcreate-a-collapsible-card-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas