Skip to content

Commit

Permalink
feat: dialog、toast、notify新增创建全局唯一组件方法
Browse files Browse the repository at this point in the history
  • Loading branch information
kongjing committed Mar 7, 2023
1 parent c5e4fdc commit 516181e
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 23 deletions.
28 changes: 20 additions & 8 deletions packages/vantui-doc/src/dialog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ import { Dialog } from '@antmjs/vantui'
- confirm 用于确认消息,包含取消和确认按钮。

```jsx
const Dialog_ = Dialog.createOnlyDialog()
function Demo() {
const [value, setValue] = react.useState('')
const alert = react.useCallback((title) => {
Dialog.alert({
Dialog_.alert({
title: title || '',
message: '弹窗内容',
selector: 'vanDialog0',
}).then((value) => {
console.log('dialog result', value)
})
})

const confirm = react.useCallback(() => {
Dialog.confirm({
Dialog_.confirm({
title: '标题',
message: (
<Input
Expand All @@ -44,15 +44,14 @@ function Demo() {
onChange={(e) => setValue(e.detail.value)}
/>
),
selector: 'vanDialog0',
}).then((value) => {
console.log('dialog result', value)
})
}, [value])

return (
<View>
<Dialog id="vanDialog0" />
<Dialog_ />
<Cell title="提示弹窗" onClick={() => alert('提示一下')} />
<Cell title="提示弹窗(无标题)" onClick={() => alert()} />
<Cell title="确认弹窗" onClick={confirm} />
Expand All @@ -66,21 +65,22 @@ function Demo() {
将 theme 选项设置为 `roundButton` 可以展示圆角按钮风格的弹窗。

```jsx
const Dialog_ = Dialog.createOnlyDialog()

function Demo() {
const alert = react.useCallback((title) => {
Dialog.alert({
Dialog_.alert({
title: title || '',
message: '弹窗内容',
theme: 'roundButton',
selector: '#vanDialog1',
}).then((value) => {
console.log('dialog result', value)
})
})

return (
<View>
<Dialog id="vanDialog1" />
<Dialog_ />
<Cell title="提示弹窗" onClick={() => alert('提示一下')} />
<Cell title="提示弹窗(无标题)" onClick={() => alert()} />
</View>
Expand Down Expand Up @@ -190,6 +190,18 @@ function Demo() {
| confirmButtonOpenType | - | _&nbsp;&nbsp;TaroButtonProps.OpenType<br/>_ | - | `false` |
| renderTitle | - | _&nbsp;&nbsp;ReactNode<br/>_ | - | `false` |

### dialogProps [[详情]](https://github.com/AntmJS/vantui/tree/main/packages/vantui/types/dialog.d.ts)

| 参数 | 说明 | 类型 | 默认值 | 必填 |
| ------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------ | ------ |
| alert | - | _&nbsp;&nbsp;(<br/>&nbsp;&nbsp;&nbsp;&nbsp;options:&nbsp;DialogProps<br/>&nbsp;&nbsp;)&nbsp;=>&nbsp;Promise<<br/>&nbsp;&nbsp;&nbsp;&nbsp;"confirm"&nbsp;&brvbar;&nbsp;"cancel"<br/>&nbsp;&nbsp;><br/>_ | - | `true` |
| confirm | - | _&nbsp;&nbsp;(<br/>&nbsp;&nbsp;&nbsp;&nbsp;options:&nbsp;DialogProps<br/>&nbsp;&nbsp;)&nbsp;=>&nbsp;Promise<<br/>&nbsp;&nbsp;&nbsp;&nbsp;"confirm"&nbsp;&brvbar;&nbsp;"cancel"<br/>&nbsp;&nbsp;><br/>_ | - | `true` |
| setDefaultOptions | - | _&nbsp;&nbsp;(<br/>&nbsp;&nbsp;&nbsp;&nbsp;options:&nbsp;DialogProps<br/>&nbsp;&nbsp;)&nbsp;=>&nbsp;void<br/>_ | - | `true` |
| resetDefaultOptions | - | _&nbsp;&nbsp;()&nbsp;=>&nbsp;void<br/>_ | - | `true` |
| close | - | _&nbsp;&nbsp;()&nbsp;=>&nbsp;void<br/>_ | - | `true` |
| stopLoading | - | _&nbsp;&nbsp;()&nbsp;=>&nbsp;void<br/>_ | - | `true` |
| createOnlyDialog | 创建唯一的 Dialog, 命令式调用不需要设置 selector 和 id | _&nbsp;&nbsp;()&nbsp;=>&nbsp;FunctionComponent<DialogProps>&nbsp;&<br/>&nbsp;&nbsp;&nbsp;&nbsp;dialogProps<br/>_ | - | `true` |

### 样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考[ConfigProvider 组件](https://antmjs.github.io/vantui/#/config-provider)
Expand Down
16 changes: 10 additions & 6 deletions packages/vantui-doc/src/toast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import { Toast } from 'vantui'
```

```jsx
const Toast_ = Toast.createOnlyToast()
function Demo() {
const show = () => {
Toast.show('我是提示文案')
Toast_.show('我是提示文案')
}

const showLong = () => {
Toast.show('我是提示文案, 超过了十五就会换行额~~~')
Toast_.show('我是提示文案, 超过了十五就会换行额~~~')
}

return (
Expand All @@ -41,7 +42,7 @@ function Demo() {
<Button type="primary" onClick={showLong}>
长文字提示
</Button>
<Toast id="vanToast-demo1" />
<Toast_ />
</View>
)
}
Expand All @@ -52,16 +53,18 @@ function Demo() {
使用 `Toast.loading` 方法展示加载提示,通过 `forbidClick` 属性可以禁用背景点击,通过 `loadingType` 属性可以自定义加载图标类型。

```jsx
const Toast_ = Toast.createOnlyToast()

function Demo() {
const show = () => {
Toast.success({
Toast_.success({
message: '成功文案',
selector: '#vanToast-demo2',
})
}

const showLong = () => {
Toast.fail({
Toast_.fail({
message: '失败文案',
selector: '#vanToast-demo2',
})
Expand All @@ -75,7 +78,7 @@ function Demo() {
<Button type="primary" onClick={showLong}>
自定义加载图标
</Button>
<Toast id="vanToast-demo2" />
<Toast_ />
</View>
)
}
Expand Down Expand Up @@ -191,6 +194,7 @@ Toast.show({
| clear | - | _&nbsp;&nbsp;(<br/>&nbsp;&nbsp;&nbsp;&nbsp;options?:&nbsp;ToastProps<br/>&nbsp;&nbsp;)&nbsp;=>&nbsp;void<br/>_ | - | `true` |
| setDefaultOptions | - | _&nbsp;&nbsp;(<br/>&nbsp;&nbsp;&nbsp;&nbsp;options:&nbsp;ToastProps<br/>&nbsp;&nbsp;)&nbsp;=>&nbsp;void<br/>_ | - | `true` |
| resetDefaultOptions | - | _&nbsp;&nbsp;(<br/>&nbsp;&nbsp;&nbsp;&nbsp;options:&nbsp;any<br/>&nbsp;&nbsp;)&nbsp;=>&nbsp;void<br/>_ | - | `true` |
| createOnlyToast | 获取唯一的 toast 组件,不需要手动设置 id 和 selector | _&nbsp;&nbsp;()&nbsp;=>&nbsp;FunctionComponent<ToastProps>&nbsp;&<br/>&nbsp;&nbsp;&nbsp;&nbsp;toastProps<br/>_ | - | `true` |

### 样式变量

Expand Down
33 changes: 33 additions & 0 deletions packages/vantui/src/dialog/create-only-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Dialog } from './index'

let idIndex = 1

export function createOnlyDialog() {
const id = `van-create-dialog${idIndex++}`
const Dialog_ = function Modal(props = {}) {
return <Dialog {...props} id={id} />
}

const actionNames = [
'alert',
'confirm',
'setDefaultOptions',
'resetDefaultOptions',
'close',
'stopLoading',
'createOnlyDialog',
]

for (let i = 0; i < actionNames.length; i++) {
const name = actionNames[i]
if (name && Dialog[name])
Dialog_[name] = function (props) {
Dialog[name]({
...props,
selector: `#${id}`,
})
}
}

return Dialog_
}
3 changes: 3 additions & 0 deletions packages/vantui/src/dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import VanPopup from '../popup/index'
import { DialogProps } from '../../types/dialog'
import { on, off, trigger } from './events'
import * as utils from './../wxs/utils'
import { createOnlyDialog } from './create-only-dialog'
import dialog from './dialog'
export function Dialog(props: DialogProps) {
const [options, setOptions] = useState<DialogProps>({})
Expand Down Expand Up @@ -303,4 +304,6 @@ Dialog.resetDefaultOptions = function () {
dialog.resetDefaultOptions()
}

Dialog.createOnlyDialog = createOnlyDialog

export default Dialog
26 changes: 26 additions & 0 deletions packages/vantui/src/notify/create-only-notify.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Notify } from './index'

let idIndex = 1

export function createOnlyNotify() {
const id = `van-create-notify${idIndex++}`
const Notify_ = function Modal(props) {
return <Notify {...props} id={id} />
}

const actionNames = ['show', 'clear']

for (let i = 0; i < actionNames.length; i++) {
const name = actionNames[i]
if (name && Notify_[name]) {
Notify_[name] = function (props) {
Notify[name]({
...props,
selector: `#${id}`,
})
}
}
}

return Notify_
}
2 changes: 2 additions & 0 deletions packages/vantui/src/notify/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getSystemInfoSync } from '../common/utils'
import * as computed from './wxs'
import { on, off, trigger } from './events'
import notify from './notify'
import { createOnlyNotify } from './create-only-notify'

const defaultId = 'van-notify'
const defaultOptions = {
Expand Down Expand Up @@ -174,5 +175,6 @@ export function Notify(props: NotifyProps) {

Notify.show = notify
Notify.clear = notify.clear
Notify.createOnlyNotify = createOnlyNotify

export default Notify
39 changes: 39 additions & 0 deletions packages/vantui/src/toast/create-only-toast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ToastProps, toastProps } from '../../types/toast'
import { Toast } from './index'

let idIndex = 1

export function createOnlyToast() {
const id = `van-create-toast${idIndex++}`
const Toast_ = function Modal(props: ToastProps) {
return <Toast {...props} id={id} />
}

const actionNames: Array<keyof toastProps> = [
'show',
'loading',
'success',
'fail',
'setDefaultOptions',
'resetDefaultOptions',
'createOnlyToast',
]

for (let i = 0; i < actionNames.length; i++) {
const actName = actionNames[i]
if (actName) {
Toast_[actName] = function (props) {
let params: any = {}
if (typeof props === 'string') {
params = {
message: props,
selector: `#${id}`,
}
} else params = props
Toast[actName](params)
}
}
}

return Toast_ as React.FunctionComponent<ToastProps> & toastProps
}
3 changes: 3 additions & 0 deletions packages/vantui/src/toast/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import VanIcon from '../icon/index'
import VanLoading from '../loading/index'
import { isObj } from '../common/validator'
import { on, off, trigger } from './events'
import { createOnlyToast } from './create-only-toast'
import toast from './toast'

const defaultId = 'van-toast'
Expand Down Expand Up @@ -187,4 +188,6 @@ Toast.fail = toast.fail
Toast.clear = toast.clear
Toast.setDefaultOptions = toast.setDefaultOptions
Toast.resetDefaultOptions = toast.resetDefaultOptions
Toast.createOnlyToast = createOnlyToast

export default Toast
3 changes: 3 additions & 0 deletions packages/vantui/src/toast/toast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isObj } from '../common/validator'
import { ToastProps } from '../../types/toast'
import { createOnlyToast } from './create-only-toast'
import { trigger } from './events'

function parseOptions(message: ToastProps | string) {
Expand All @@ -26,4 +27,6 @@ Toast.resetDefaultOptions = () => {
trigger('toast_resetDefaultOptions')
}

Toast.createOnlyToast = createOnlyToast

export default Toast
24 changes: 15 additions & 9 deletions packages/vantui/types/dialog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ViewProps, ButtonProps as TaroButtonProps } from '@tarojs/components'

import { ButtonProps } from './button'
import { PopupProps } from './popup'

// @ts-ignore
export interface DialogProps
extends ViewProps,
Omit<PopupProps, 'onClose' | 'children'>,
Expand Down Expand Up @@ -43,16 +45,20 @@ export interface DialogProps
renderTitle?: ReactNode
}

interface dialog {
alert(options: DialogProps): Promise<'confirm' | 'cancel'>
confirm(options: DialogProps): Promise<'confirm' | 'cancel'>
setDefaultOptions(options: DialogProps): void
resetDefaultOptions(): void
close(): void
stopLoading(): void
export interface dialogProps {
alert: (options: DialogProps) => Promise<'confirm' | 'cancel'>
confirm: (options: DialogProps) => Promise<'confirm' | 'cancel'>
setDefaultOptions: (options: DialogProps) => void
resetDefaultOptions: () => void
close: () => void
stopLoading: () => void
/**
* @description 创建唯一的Dialog, 命令式调用不需要设置selector和id
*/
createOnlyDialog: () => FunctionComponent<DialogProps> & dialogProps
}

// & DialogCom
declare const Dialog: FunctionComponent<DialogProps> & dialog
declare const Dialog: FunctionComponent<DialogProps> & dialogProps

export { Dialog, dialog }
export { Dialog }
4 changes: 4 additions & 0 deletions packages/vantui/types/notify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface NotifyProps extends ViewProps {
interface notifyProps {
show: (options: NotifyProps | string) => any
clear: (options?: NotifyProps) => any
/**
* @description 创建唯一的Notify, 命令方式调用不需要设置id和selector
*/
createOnlyNotify: () => FunctionComponent<NotifyProps> & notifyProps
}

declare const Notify: FunctionComponent<NotifyProps> & notifyProps
Expand Down
4 changes: 4 additions & 0 deletions packages/vantui/types/toast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export interface toastProps {
clear: (options?: ToastProps) => void
setDefaultOptions: (options: ToastProps) => void
resetDefaultOptions: (options: any) => void
/**
* @description 获取唯一的toast组件,不需要手动设置id和selector
*/
createOnlyToast: () => FunctionComponent<ToastProps> & toastProps
}

declare const Toast: FunctionComponent<ToastProps> & toastProps
Expand Down

0 comments on commit 516181e

Please sign in to comment.