Elementary state handling for Dropdown links

This commit is contained in:
Aarni Halinen
2018-11-16 21:20:39 +02:00
parent c9a6d2d5ac
commit a14e263b6c
5 changed files with 77 additions and 8 deletions
@@ -0,0 +1,5 @@
@import "../../assets/scss/globals";
.drop-down-box {
color: $white;
}
@@ -0,0 +1,18 @@
import * as React from "react";
import "./DropDownBox.scss";
export interface DropDownBoxProps {}
export interface DropDownBoxState {}
class DropDownBox extends React.Component<DropDownBoxProps, DropDownBoxState> {
render() {
const { children } = this.props;
return (
<div className="drop-down-box">
{children}
</div>
);
}
}
export default DropDownBox;
+2
View File
@@ -0,0 +1,2 @@
import DropDownBox from "./DropDownBox";
export default DropDownBox;
@@ -4,12 +4,17 @@ import "./NavbarChildLink.scss";
export interface NavbarChildLinkProps {
to: string;
}
export interface NavbarChildLinkState {}
export interface NavbarChildLinkState {
open: Boolean;
}
class NavbarChildLink extends React.Component<NavbarChildLinkProps, NavbarChildLinkState> {
render() {
// style={this.state.open ? {} : {display: "none"}}
return (
<a className="navbar-child-link" href={this.props.to}>
<a className="navbar-child-link"
href={this.props.to}>
{this.props.children}
</a>
);
}
@@ -1,19 +1,58 @@
import * as React from "react";
import "./NavbarDropdownLink.scss";
import { Fragment } from "react";
import DropDownBox from "../DropDownBox/DropDownBox";
export interface NavbarDropdownLinkProps {
to: string;
text: string;
}
export interface NavbarDropdownLinkState {}
export interface NavbarDropdownLinkState {
open: Boolean;
}
class NavbarDropdownLink extends React.Component<NavbarDropdownLinkProps, NavbarDropdownLinkState> {
constructor(props) {
super(props);
this.state = {open: false};
}
handleOpen = () => {
this.setState({open: true});
}
handleClose = () => {
this.setState({open: false});
}
render() {
return (
<a className="navbar-dropdown-link" href={this.props.to}>
{this.props.text}
</a>
);
const { children } = this.props;
const { open } = this.state;
if (open) {
return (
<Fragment>
<a className="navbar-dropdown-link"
href={this.props.to}
onMouseEnter={this.handleOpen}
onMouseLeave={this.handleClose}
> {this.props.text} </a>
<DropDownBox>
{ children }
</DropDownBox>
</Fragment>
);
} else {
return (
<a className="navbar-dropdown-link"
href={this.props.to}
onMouseEnter={this.handleOpen}
onMouseLeave={this.handleClose}
>
{this.props.text}
</a>
);
}
}
}