change collapse component to hooks implementation

This commit is contained in:
komv8i
2019-02-07 14:23:46 -05:00
parent 509f7e87fa
commit 0eeac148c6

View File

@ -5,52 +5,46 @@ 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 value of the `collapsed` prop to determine the initial state of the content (collapsed/expanded).
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. 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. Create a method, `toggleCollapse` to change the component's `state` from collapsed to expanded and vice versa.
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`. 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 `state.collapsed` 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 `state.collapsed` 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 { const style = {
constructor(props) { collapsed: {
super(props); display: "none"
this.state = { },
collapsed: !!props.collapsed expanded: {
}; display: "block"
this.style = { },
collapsed: { buttonStyle: {
display: 'none' display: "block",
}, width: "100%"
expanded: {
display: 'block'
},
buttonStyle: {
display: 'block',
width: '100%'
}
};
this.toggleCollapse = this.toggleCollapse.bind(this);
} }
};
toggleCollapse() { function Collapse(props) {
this.setState(state => ({ collapsed: !state.collapsed })); const [isCollapsed, setCollapsed] = useState(props.collapsed);
}
render() { const toggleCollapse = () => {
return ( setCollapsed(!isCollapsed);
<div> };
<button style={this.style.buttonStyle} onClick={this.toggleCollapse}>
{this.state.collapsed ? 'Show' : 'Hide'} content return (
</button> <div>
<div <button style={style.buttonStyle} onClick={() => toggleCollapse()}>
style= {this.state.collapsed ? this.style.collapsed : this.style.expanded} {isCollapsed ? "Show" : "Hide"} content
aria-expanded = {this.state.collapsed} </button>
> <div
{this.props.children} className="collapse-content"
</div> style={isCollapsed ? style.collapsed : style.expanded}
aria-expanded={isCollapsed}
>
{props.children}
</div> </div>
); </div>
} );
} }
``` ```