提交 d30aa9b1 编写于 作者: L liuyijun

feat: 提交overlay遮罩层组件

上级 b78819f2
...@@ -151,6 +151,16 @@ ...@@ -151,6 +151,16 @@
"sort": 1, "sort": 1,
"show": false, "show": false,
"author": "songsong" "author": "songsong"
},
{
"version": "1.0.0",
"name": "Overlay",
"type": "component",
"cName": "遮罩层",
"desc": "创建一个遮罩层,通常用于阻止用户进行其他操作",
"sort": 5,
"show": true,
"author": "junjun"
} }
] ]
}, },
......
import React from 'react' import React from 'react'
import { Avatar } from './avatar' import { Avatar } from './avatar'
import Cell from '@/packages/cell'
const AvatarDemo = () => { const AvatarDemo = () => {
const AvatarStyle = {
alignItems: 'flex-end',
}
const handleClick = () => { const handleClick = () => {
console.log('触发点击头像') console.log('触发点击头像')
} }
...@@ -9,29 +13,41 @@ const AvatarDemo = () => { ...@@ -9,29 +13,41 @@ const AvatarDemo = () => {
<> <>
<div className="demo"> <div className="demo">
<h2>默认用法 (内置&quot;small&quot;,&quot;normal&quot;,&quot;large&quot;三种尺寸规格)</h2> <h2>默认用法 (内置&quot;small&quot;,&quot;normal&quot;,&quot;large&quot;三种尺寸规格)</h2>
<Avatar <Cell style={AvatarStyle}>
size="large" <Avatar
src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png" size="large"
></Avatar> src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png"
<Avatar ></Avatar>
size="normal" <Avatar
src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png" size="normal"
></Avatar> src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png"
<Avatar ></Avatar>
size="small" <Avatar
src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png" size="small"
></Avatar> src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png"
></Avatar>
</Cell>
<h2>修改形状</h2> <h2>修改形状</h2>
<Avatar shape="square"></Avatar> <Cell>
<Avatar shape="round"></Avatar> <Avatar shape="square"></Avatar>
<Avatar shape="round"></Avatar>
</Cell>
<h2>修改背景色</h2> <h2>修改背景色</h2>
<Avatar bgColor="#FA2C19"></Avatar> <Cell>
<Avatar bgColor="#FA2C19"></Avatar>
</Cell>
<h2>修改背景图片</h2> <h2>修改背景图片</h2>
<Avatar icon="https://img30.360buyimg.com/uba/jfs/t1/84318/29/2102/10483/5d0704c1Eb767fa74/fc456b03fdd6cbab.png"></Avatar> <Cell>
<Avatar icon="https://img30.360buyimg.com/uba/jfs/t1/84318/29/2102/10483/5d0704c1Eb767fa74/fc456b03fdd6cbab.png"></Avatar>
</Cell>
<h2>可以修改头像的内容</h2> <h2>可以修改头像的内容</h2>
<Avatar icon="">N</Avatar> <Cell>
<Avatar icon="">N</Avatar>
</Cell>
<h2>点击头像触发事件</h2> <h2>点击头像触发事件</h2>
<Avatar icon="my" onClick={handleClick}></Avatar> <Cell>
<Avatar icon="my" onClick={handleClick}></Avatar>
</Cell>
</div> </div>
</> </>
) )
......
...@@ -6,11 +6,7 @@ ...@@ -6,11 +6,7 @@
### 安装 ### 安装
``` javascript ``` javascript
import { createApp } from 'vue';
import { Avatar } from '@nutui/nutui'; import { Avatar } from '@nutui/nutui';
const app = createApp();
app.use(Avatar);
``` ```
## 代码示例 ## 代码示例
...@@ -19,38 +15,38 @@ app.use(Avatar); ...@@ -19,38 +15,38 @@ app.use(Avatar);
内置 smal / normal / large 三种尺寸规格 内置 smal / normal / large 三种尺寸规格
``` html ``` tsx
<nut-avatar size="large" src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png" <Avatar size="large" src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png"
></nut-avatar> ></Avatar>
<nut-avatar size="normal" src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png" <Avatar size="normal" src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png"
></nut-avatar> ></Avatar>
<nut-avatar size="small" src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png" <Avatar size="small" src="https://img12.360buyimg.com/imagetools/jfs/t1/143702/31/16654/116794/5fc6f541Edebf8a57/4138097748889987.png"
></nut-avatar> ></Avatar>
``` ```
### 修改形状类型 ### 修改形状类型
``` html ``` tsx
<nut-avatar shape="square"></nut-avatar> <Avatar shape="square"></Avatar>
<nut-avatar shape="round"></nut-avatar> <Avatar shape="round"></Avatar>
``` ```
### 修改背景色 ### 修改背景色
``` html ``` tsx
<nut-avatar bg-color="#f0250f"></nut-avatar> <Avatar bg-color="#f0250f"></Avatar>
``` ```
### 修改背景icon ### 修改背景icon
``` html ``` tsx
<nut-avatar icon="https://img30.360buyimg.com/uba/jfs/t1/84318/29/2102/10483/5d0704c1Eb767fa74/fc456b03fdd6cbab.png"></nut-avatar> <Avatar icon="https://img30.360buyimg.com/uba/jfs/t1/84318/29/2102/10483/5d0704c1Eb767fa74/fc456b03fdd6cbab.png"></Avatar>
``` ```
### 设置头像的文本内容 ### 设置头像的文本内容
``` html ``` tsx
<nut-avatar icon>N</nut-avatar> <Avatar icon>N</Avatar>
``` ```
...@@ -58,7 +54,7 @@ app.use(Avatar); ...@@ -58,7 +54,7 @@ app.use(Avatar);
| 字段 | 说明 | 类型 | 默认值 | | 字段 | 说明 | 类型 | 默认值 |
|----------|--------------------------------------------------------------------------|--------|--------| |----------|--------------------------------------------------------------------------|--------|--------|
| bg-color | 设置头像背景色 | String | #eee | | bgColor | 设置头像背景色 | String | #eee |
| size | 设置头像的大小,提供三种:large/normal/small,支持直接输入数字 | String | normal | | size | 设置头像的大小,提供三种:large/normal/small,支持直接输入数字 | String | normal |
| shape | 设置头像的形状,默认是圆形,可以设置为square方形 | String | round | | shape | 设置头像的形状,默认是圆形,可以设置为square方形 | String | round |
| src | 设置头像的背景图片 | String | '' | | src | 设置头像的背景图片 | String | '' |
...@@ -68,4 +64,4 @@ app.use(Avatar); ...@@ -68,4 +64,4 @@ app.use(Avatar);
| 字段 | 说明 | 类型 | 回调参数 | | 字段 | 说明 | 类型 | 回调参数 |
|----------|----------------------|----------|----------| |----------|----------------------|----------|----------|
| on-error | 加载图片失败触发事件 | Function | event | | onClick | 点击图片触发事件 | Function | event |
\ No newline at end of file \ No newline at end of file
...@@ -86,7 +86,6 @@ export const Button: FunctionComponent<Partial<ButtonProps>> = (props) => { ...@@ -86,7 +86,6 @@ export const Button: FunctionComponent<Partial<ButtonProps>> = (props) => {
${disabled ? `${prefixCls}--disabled` : ''} ${disabled ? `${prefixCls}--disabled` : ''}
${loading ? `${prefixCls}--loading` : ''}` ${loading ? `${prefixCls}--loading` : ''}`
} }
const handleClick = (e: any) => { const handleClick = (e: any) => {
if (!loading && !disabled && onClick) { if (!loading && !disabled && onClick) {
onClick(e) onClick(e)
...@@ -94,7 +93,6 @@ export const Button: FunctionComponent<Partial<ButtonProps>> = (props) => { ...@@ -94,7 +93,6 @@ export const Button: FunctionComponent<Partial<ButtonProps>> = (props) => {
} }
const getStyle = () => { const getStyle = () => {
const style: CSSProperties = {} const style: CSSProperties = {}
console.log(plain)
if (color) { if (color) {
if (plain) { if (plain) {
style.color = color style.color = color
......
...@@ -32,7 +32,9 @@ const defaultProps = { ...@@ -32,7 +32,9 @@ const defaultProps = {
extra: '', extra: '',
click: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {}, click: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {},
} as CellProps } as CellProps
export const Cell: FunctionComponent<Partial<CellProps>> = (props) => { export const Cell: FunctionComponent<Partial<CellProps> & React.HTMLAttributes<HTMLDivElement>> = (
props
) => {
const { const {
children, children,
click, click,
...@@ -47,6 +49,7 @@ export const Cell: FunctionComponent<Partial<CellProps>> = (props) => { ...@@ -47,6 +49,7 @@ export const Cell: FunctionComponent<Partial<CellProps>> = (props) => {
title, title,
subTitle, subTitle,
extra, extra,
...rest
} = { } = {
...defaultProps, ...defaultProps,
...props, ...props,
...@@ -69,6 +72,7 @@ export const Cell: FunctionComponent<Partial<CellProps>> = (props) => { ...@@ -69,6 +72,7 @@ export const Cell: FunctionComponent<Partial<CellProps>> = (props) => {
<div <div
className={`${b({ clickable: isLink || to ? true : false }, [classPrefix])} `} className={`${b({ clickable: isLink || to ? true : false }, [classPrefix])} `}
onClick={(event) => handleClick(event)} onClick={(event) => handleClick(event)}
{...rest}
> >
{children ? ( {children ? (
children children
......
.wrapper {
display: flex;
height: 100%;
align-items: center;
justify-content: center;
.content {
display: flex;
width: 150px;
height: 150px;
background: #fff;
border-radius: 8px;
align-items: center;
justify-content: center;
color: red;
}
}
import React, { useState } from 'react'
import { Overlay } from './overlay'
import Cell from '@/packages/cell'
import Button from '@/packages/button'
import './demo.scss'
const OverlayDemo = () => {
const [visible, setVisible] = useState(false)
const [visible2, setVisible2] = useState(false)
const handleToggleShow = () => {
setVisible(true)
}
const handleToggleShow2 = () => {
setVisible2(true)
}
const onClose = () => {
setVisible(false)
}
const onClose2 = () => {
setVisible2(false)
}
return (
<>
<div className="demo">
<h2>基础用法</h2>
<Cell>
<Button type="primary" onClick={handleToggleShow}>
显示遮罩层
</Button>
<Overlay visible={visible} onClick={onClose}></Overlay>
</Cell>
<h2>嵌套内容</h2>
<Cell>
<Button type="success" onClick={handleToggleShow2}>
嵌套内容
</Button>
<Overlay visible={visible2} onClick={onClose2}>
<div className="wrapper">
<div className="content">这里是正文</div>
</div>
</Overlay>
</Cell>
</div>
</>
)
}
export default OverlayDemo
# Overlay 组件
### 介绍
创建一个遮罩层,通常用于阻止用户进行其他操作
### 安装
``` javascript
import { OverLay } from '@nutui/nutui';
```
## 代码演示
### 基础用法
```tsx
<Overlay visible={true} zindex={2000}></Overlay>
```
### 嵌套内容
```tsx
<nut-overlay visible={true} zIndex={2000}>
<div className="wrapper">
<div className="content">这里是正文</div>
</div>
</nut-overlay>
```
## API
### Props
| 参数 | 说明 | 类型 | 默认值 |
| ---------------------- | ---------------- | -------------- | ------ |
| visible | 当前组件是否显示 | Boolean | `false` |
| zIndex | 遮罩层级 | Number | 2000 |
| duration | 动画时长,单位秒 | Number | 0.3 |
| overlayClass | 自定义遮罩类名 | String | - |
| overlayStyle | 自定义遮罩样式 | CSSProperties | - |
| closeOnClickOverlay | 是否点击遮罩关闭 | Boolean | `true` |
### Events
| 事件名 | 说明 | 回调参数 |
| ------ | ---------- | ------------ |
| onClick | 点击时触发 | event: Event |
import { Overlay } from './overlay'
export default Overlay
.nut-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: $overlay-bg-color;
}
.overlay-fade-enter-active {
animation: nut-fade-in;
}
.overlay-fade-leave-active {
animation: nut-fade-out;
}
.first-render {
display: none;
}
.hidden-render {
display: none;
}
@keyframes nut-fade-in {
0% {
opacity: 0;
}
1% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes nut-fade-out {
0% {
opacity: 1;
}
1% {
opacity: 1;
}
100% {
opacity: 0;
}
}
import React, { FunctionComponent, MouseEventHandler, useEffect, useRef, useState } from 'react'
import bem from '@/utils/bem'
import classNames from 'classnames'
import './overlay.scss'
export interface OverlayProps {
zIndex: number
duration: number
overlayClass: string
overlayStyle: React.CSSProperties
closeOnClickOverlay: boolean
visible: boolean
}
const defaultProps = {
zIndex: 2000,
duration: 0.3,
overlayClass: '',
closeOnClickOverlay: true,
visible: false,
} as OverlayProps
export const Overlay: FunctionComponent<
Partial<OverlayProps> & React.HTMLAttributes<HTMLDivElement>
> = (props) => {
const [show, setShow] = useState(false)
const renderRef = useRef(true)
const intervalRef = useRef(0)
const { children, zIndex, duration, overlayClass, closeOnClickOverlay, visible, ...rest } = {
...defaultProps,
...props,
}
useEffect(() => {
setShow(false)
}, [visible])
useEffect(() => {
return () => {
clearTimeout(intervalRef.current)
}
}, [])
const b = bem('overlay')
const classes = classNames(
{
[overlayClass]: true,
'overlay-fade-leave-active': !renderRef.current && !visible,
'overlay-fade-enter-active': visible,
'first-render': renderRef.current && !visible,
'hidden-render': show,
},
b('')
)
const styles = {
zIndex: zIndex,
animationDuration: `${props.duration}s`,
...props.overlayStyle,
}
const handleClick: MouseEventHandler<HTMLDivElement> = (e) => {
renderRef.current = false
let id = setTimeout(() => {
setShow(true)
}, duration * 1000 * 0.8)
intervalRef.current = id
if (props.onClick) {
props.onClick(e)
}
}
return (
<React.Fragment>
<div className={classes} style={styles} {...rest} onClick={handleClick}>
{children}
</div>
</React.Fragment>
)
}
Overlay.defaultProps = defaultProps
Overlay.displayName = 'NutOverlay'
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册