Migration: slidebar to hooks component

This commit is contained in:
Dreamacro 2019-07-02 18:04:37 +08:00
parent d3e6fffc80
commit 659f41ec5a
2 changed files with 28 additions and 27 deletions

View File

@ -1,4 +1,4 @@
import React, { useState, useRef, useMemo } from 'react' import React, { useState, useRef, useEffect } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { BaseComponentProps, I18nProps } from '@models' import { BaseComponentProps, I18nProps } from '@models'
import { noop } from '@lib/helper' import { noop } from '@lib/helper'
@ -16,11 +16,14 @@ interface TagsProps extends BaseComponentProps, I18nProps {
export function Tags (props: TagsProps) { export function Tags (props: TagsProps) {
const { className, data, onClick, select, canClick, rowHeight: rawHeight } = props const { className, data, onClick, select, canClick, rowHeight: rawHeight } = props
const { t } = useTranslation() const { t } = useTranslation(['Proxies'])
const [expand, setExpand] = useState(false) const [expand, setExpand] = useState(false)
const [showExtend, setShowExtend] = useState(false)
const ulRef = useRef<HTMLUListElement>() const ulRef = useRef<HTMLUListElement>()
const showExtend = useMemo(() => ulRef.current && ulRef.current.offsetHeight > 30, [ulRef.current]) useEffect(() => {
setShowExtend(ulRef.current.offsetHeight > 30)
}, [])
const rowHeight = expand ? 'auto' : rawHeight const rowHeight = expand ? 'auto' : rawHeight
const handleClick = canClick ? onClick : noop const handleClick = canClick ? onClick : noop

View File

@ -1,12 +1,12 @@
import * as React from 'react' import * as React from 'react'
import { NavLink } from 'react-router-dom' import { NavLink } from 'react-router-dom'
import { withTranslation, WithTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import classnames from 'classnames' import classnames from 'classnames'
import './style.scss' import './style.scss'
const logo = require('@assets/logo.png') const logo = require('@assets/logo.png')
interface SidebarProps extends WithTranslation { interface SidebarProps {
routes: { routes: {
path: string path: string
name: string name: string
@ -15,26 +15,24 @@ interface SidebarProps extends WithTranslation {
}[] }[]
} }
class Sidebar extends React.Component<SidebarProps, {}> { export default function Sidebar (props: SidebarProps) {
render () { const { routes } = props
const { routes, t } = this.props const { t } = useTranslation(['SideBar'])
return (
<div className="sidebar"> const navlinks = routes.map(
<img src={logo} className="sidebar-logo" />
<ul className="sidebar-menu">
{
routes.map(
({ path, name, exact, noMobile }) => ( ({ path, name, exact, noMobile }) => (
<li className={classnames('item', { 'no-mobile': noMobile })} key={name}> <li className={classnames('item', { 'no-mobile': noMobile })} key={name}>
<NavLink to={path} activeClassName="active" exact={!!exact}>{ t(name) }</NavLink> <NavLink to={path} activeClassName="active" exact={!!exact}>{ t(name) }</NavLink>
</li> </li>
) )
) )
}
return (
<div className="sidebar">
<img src={logo} className="sidebar-logo" />
<ul className="sidebar-menu">
{ navlinks }
</ul> </ul>
</div> </div>
) )
}
} }
export default withTranslation(['SideBar'])(Sidebar)