提交 6c6096dd 编写于 作者: Y Ymm0008

fix: fixednav 优化

上级 0aaecda9
...@@ -135,6 +135,7 @@ const selected = (item: any, event: MouseEvent) => { ...@@ -135,6 +135,7 @@ const selected = (item: any, event: MouseEvent) => {
### Prop ### Prop
| 字段 | 说明 | 类型 | 默认值 | | 字段 | 说明 | 类型 | 默认值 |
|:---------------|:---------------------------|:--------|:-----------------------------| |:---------------|:---------------------------|:--------|:-----------------------------|
| fixednavClass | 自定义类名 | String | fixednav |
| visible | 是否打开 | Boolean | false | | visible | 是否打开 | Boolean | false |
| navList | 悬浮列表内容数据 | Array | [] | | navList | 悬浮列表内容数据 | Array | [] |
| activeText | 收起列表按钮文案 | String | 收起导航 | | activeText | 收起列表按钮文案 | String | 收起导航 |
......
import React, { FunctionComponent, useState, useEffect, MouseEvent, HTMLProps } from 'react' import React, { FunctionComponent, useState, useEffect, MouseEvent, HTMLProps } from 'react'
import { Icon } from '../icon/icon' import { Icon } from '../icon/icon'
import { Overlay } from '../overlay/overlay' import bem from '@/utils/bem'
import classNames from 'classnames' import classNames from 'classnames'
import { Overlay } from '../overlay/overlay'
import './fixednav.scss' import './fixednav.scss'
type Direction = 'right' | 'left'
type Position = {
top?: string
bottom?: string
}
export interface FixedNavProps { export interface FixedNavProps {
prefixCls: string fixednavClass: string
visible: boolean visible: boolean
overlay: boolean overlay: boolean
navList: Array<object> navList: Array<object>
activeText: string activeText: string
unActiveText: string unActiveText: string
position: object position: Position
type: string type: Direction
change: Function change: Function
selected: Function selected: Function
slotList: HTMLProps<HTMLElement> slotList: HTMLProps<HTMLElement>
...@@ -21,22 +27,21 @@ export interface FixedNavProps { ...@@ -21,22 +27,21 @@ export interface FixedNavProps {
} }
const defaultProps = { const defaultProps = {
prefixCls: 'nut-fixednav', fixednavClass: 'nut-fixednav',
activeText: '收起导航', activeText: '收起导航',
unActiveText: '快速导航', unActiveText: '快速导航',
type: 'right',
position: { position: {
top: 'auto', top: 'auto',
bottom: 'auto', bottom: 'auto',
}, },
type: 'right',
} as FixedNavProps } as FixedNavProps
export const FixedNav: FunctionComponent< export const FixedNav: FunctionComponent<
Partial<FixedNavProps> & React.HTMLAttributes<HTMLDivElement> Partial<FixedNavProps> & React.HTMLAttributes<HTMLDivElement>
> = (props) => { > = (props) => {
const { const {
prefixCls, fixednavClass,
children,
overlay, overlay,
visible, visible,
navList, navList,
...@@ -48,30 +53,42 @@ export const FixedNav: FunctionComponent< ...@@ -48,30 +53,42 @@ export const FixedNav: FunctionComponent<
type, type,
slotList, slotList,
slotBtn, slotBtn,
...rest
} = { ...defaultProps, ...props } } = { ...defaultProps, ...props }
const selectCb = (event: MouseEvent, item: any) => { const b = bem('fixednav')
const classes = classNames(
{
active: visible,
},
type,
fixednavClass,
b('')
)
const onSelectCb = (event: MouseEvent, item: any): void => {
selected(item, event) selected(item, event)
} }
const updateValue = (value: boolean = !visible) => { const onUpdateValue = (value: boolean = !visible): void => {
change(value) change(value)
} }
const [classNames, setClassNames] = useState('') // const [classNames, setClassNames] = useState('')
const classes = () => { // const classes = () => {
return `${prefixCls} ${type} ${visible ? 'active' : ''}` // return `${fixednavClass} ${type} ${visible ? 'active' : ''}`
} // }
useEffect(() => { // useEffect(() => {
setClassNames(classes()) // setClassNames(classes())
}, [visible]) // }, [visible])
return ( return (
<div className={`${classNames}`} style={position}> <div className={classes} style={position} {...rest}>
{overlay && ( {overlay && (
<Overlay visible={visible} zIndex={200} onClick={() => updateValue(false)}></Overlay> <Overlay visible={visible} zIndex={200} onClick={() => onUpdateValue(false)}></Overlay>
)} )}
<div className="list"> <div className="list">
{slotList ? ( {slotList ? (
...@@ -82,7 +99,7 @@ export const FixedNav: FunctionComponent< ...@@ -82,7 +99,7 @@ export const FixedNav: FunctionComponent<
return ( return (
<div <div
className="nut-fixednav__list-item" className="nut-fixednav__list-item"
onClick={(event) => selectCb(event, item)} onClick={(event) => onSelectCb(event, item)}
key={item.id || index} key={item.id || index}
> >
<img src={item.icon} /> <img src={item.icon} />
...@@ -95,7 +112,7 @@ export const FixedNav: FunctionComponent< ...@@ -95,7 +112,7 @@ export const FixedNav: FunctionComponent<
)} )}
</div> </div>
<div className="nut-fixednav__btn" onClick={() => updateValue()}> <div className="nut-fixednav__btn" onClick={() => onUpdateValue()}>
{slotBtn ? ( {slotBtn ? (
slotBtn slotBtn
) : ( ) : (
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册