Merge pull request #47 from phaniteja1/collapse

change collapse component to hooks implementation
This commit is contained in:
Angelos Chalaris
2019-02-12 20:25:06 +02:00
committed by GitHub

View File

@ -2,56 +2,47 @@
Renders a component with collapsible content. Renders a component with collapsible content.
Use the value of the `collapsed` prop to determine the initial state of the content (collapsed/expanded). Use the `React.setState()` hook to create the `isCollapsed` state variable with an initial value of `props.collapsed`.
Set the `state` of the component to the value of the `collapsed` prop (cast to a boolean value) and bind the `toggleCollapse` method to the component's context.
Use an object, `style`, to hold the styles for individual components and their states. Use an object, `style`, to hold the styles for individual components and their states.
Create a method, `toggleCollapse`, which uses `Component.prototype.setState` to change the component's `state` from collapsed to expanded and vice versa. Use a `<div>` to wrap both the `<button>` that alters the component's `isCollapsed` state and the content of the component, passed down via `props.children`.
In the `render()` method, use a `<div>` to wrap both the `<button>` that alters the component's `state` and the content of the component, passed down via `props.children`. Determine the appearance of the content, based on `isCollapsed` and apply the appropriate CSS rules from the `style` object.
Determine the appearance of the content, based on `state.collapsed` and apply the appropriate CSS rules from the `style` object. Finally, update the value of the `aria-expanded` attribute based on `isCollapsed` to make the component accessible.
Finally, update the value of the `aria-expanded` attribute based on `state.collapsed` to make the component accessible.
```jsx ```jsx
class Collapse extends React.Component { function Collapse(props) {
constructor(props) { const [isCollapsed, setIsCollapsed] = React.useState(props.collapsed);
super(props);
this.state = { const style = {
collapsed: !!props.collapsed
};
this.style = {
collapsed: { collapsed: {
display: 'none' display: "none"
}, },
expanded: { expanded: {
display: 'block' display: "block"
}, },
buttonStyle: { buttonStyle: {
display: 'block', display: "block",
width: '100%' width: "100%"
} }
}; };
this.toggleCollapse = this.toggleCollapse.bind(this);
}
toggleCollapse() {
this.setState(state => ({ collapsed: !state.collapsed }));
}
render() {
return ( return (
<div> <div>
<button style={this.style.buttonStyle} onClick={this.toggleCollapse}> <button
{this.state.collapsed ? 'Show' : 'Hide'} content style={style.buttonStyle}
onClick={() => setIsCollapsed(!isCollapsed)}
>
{isCollapsed ? "Show" : "Hide"} content
</button> </button>
<div <div
style= {this.state.collapsed ? this.style.collapsed : this.style.expanded} className="collapse-content"
aria-expanded = {this.state.collapsed} style={isCollapsed ? style.collapsed : style.expanded}
aria-expanded={isCollapsed}
> >
{this.props.children} {props.children}
</div> </div>
</div> </div>
); );
} }
}
``` ```
```jsx ```jsx