Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
cncolder committed May 22, 2021
1 parent a52c623 commit 3b9a810
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 339 deletions.
116 changes: 59 additions & 57 deletions src/Message.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
import React, { useRef, useState, useEffect } from 'react'
import Taro from '@tarojs/taro'
import classNames from 'classnames'
import { View } from '@tarojs/components'
import React, { useRef, useState, useEffect } from 'react';
import Taro from '@tarojs/taro';
import classNames from 'classnames';
import { View } from '@tarojs/components';

import '../style/Message.scss'
import '../style/Message.scss';

export interface MessageProps {
className?: string
style?: React.CSSProperties
className?: string;
style?: React.CSSProperties;
}

// 绑定函数
Taro.atMessage = Taro.eventCenter.trigger.bind(Taro.eventCenter, 'atMessage')

export const Message: React.FC<MessageProps> = props => {
const { className, style } = props

const timerRef = useRef<any>()
const [{ isOpened, type, message }, setState] = useState({
isOpened: false,
message: '',
type: 'info',
duration: 3000,
})

const atMessageHandler = (options = {} as Taro.AtMessageOptions) => {
clearTimeout(timerRef.current)

const { message, type, duration = 3000 } = options
setState(prev => ({ ...prev, isOpened: true, message, type, duration }))

timerRef.current = setTimeout(() => setState(prev => ({ ...prev, isOpened: false })), duration)
}

useEffect(() => {
Taro.eventCenter.on('atMessage', atMessageHandler)

return () => Taro.eventCenter.off('atMessage', atMessageHandler)
}, [])

// TODO taro 3.0.0-beta.3 不允许子包中出现 @tarojs/runtime, link 后 runtime 中提供的 hooks 全部失效.
// useDidShow(() => {
// Taro.eventCenter.on('atMessage', atMessageHandler)
// })
// useDidHide(() => {
// Taro.eventCenter.off('atMessage', atMessageHandler)
// })

return (
<View
className={classNames(
'at-message',
{ 'at-message--show': isOpened, 'at-message--hidden': !isOpened },
`at-message--${type}`,
className
)}
style={style}
>
{message}
</View>
)
}
Taro.atMessage = Taro.eventCenter.trigger.bind(Taro.eventCenter, 'atMessage');

export const Message: React.FC<MessageProps> = (props) => {
const { className, style } = props;

const timerRef = useRef<any>();
const [{ isOpened, type, message }, setState] = useState({
isOpened: false,
message: '',
type: 'info',
duration: 3000,
});

const atMessageHandler = (options = {} as Taro.AtMessageOptions) => {
clearTimeout(timerRef.current);

const { message, type, duration = 3000 } = options;
setState((prev) => ({ ...prev, isOpened: true, message, type, duration }));

timerRef.current = setTimeout(() => setState((prev) => ({ ...prev, isOpened: false })), duration);
};

useEffect(() => {
Taro.eventCenter.on('atMessage', atMessageHandler);

return () => {
Taro.eventCenter.off('atMessage', atMessageHandler);
};
}, []);

// TODO taro 3.0.0-beta.3 不允许子包中出现 @tarojs/runtime, link 后 runtime 中提供的 hooks 全部失效.
// useDidShow(() => {
// Taro.eventCenter.on('atMessage', atMessageHandler)
// })
// useDidHide(() => {
// Taro.eventCenter.off('atMessage', atMessageHandler)
// })

return (
<View
className={classNames(
'at-message',
{ 'at-message--show': isOpened, 'at-message--hidden': !isOpened },
`at-message--${type}`,
className
)}
style={style}
>
{message}
</View>
);
};
154 changes: 77 additions & 77 deletions src/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,90 +1,90 @@
import React, { useCallback } from 'react'
import classNames from 'classnames'
import { View, ScrollView, Button } from '@tarojs/components'
import { AtModalProps } from 'taro-ui/types/modal'
import React, { useCallback } from 'react';
import classNames from 'classnames';
import { View, ScrollView, Button } from '@tarojs/components';
import { AtModalProps } from 'taro-ui/types/modal';

import '../style/Modal.scss'
import '../style/Modal.scss';

export interface ModalProps extends Omit<AtModalProps, 'title' | 'content'> {
style?: React.CSSProperties
title?: React.ReactNode
content?: React.ReactNode
style?: React.CSSProperties;
title?: React.ReactNode;
content?: React.ReactNode;
}

export const Modal: React.FC<ModalProps> = props => {
const {
className,
style = {},
children,
isOpened,
title,
content,
cancelText,
confirmText,
closeOnClickOverlay = true,
} = props
export const Modal: React.FC<ModalProps> = (props) => {
const {
className,
style = {},
children,
isOpened,
title,
content,
cancelText,
confirmText,
closeOnClickOverlay = true,
} = props;

const onClose = useCallback(
e => {
props.onClose?.(e)
},
[props.onClose]
)
const onClose = useCallback(
(e) => {
props.onClose?.(e);
},
[props.onClose]
);

const onCancel = useCallback(
e => {
props.onCancel?.(e)
onClose(e)
},
[props.onCancel, onClose]
)
const onCancel = useCallback(
(e) => {
props.onCancel?.(e);
onClose(e);
},
[props.onCancel, onClose]
);

const onConfirm = useCallback(
e => {
props.onConfirm?.(e)
},
[props.onConfirm]
)
const onConfirm = useCallback(
(e) => {
props.onConfirm?.(e);
},
[props.onConfirm]
);

const onClickOverlay = useCallback(
e => {
if (closeOnClickOverlay) {
onClose(e)
}
},
[closeOnClickOverlay, onClose]
)
const onClickOverlay = useCallback(
(e) => {
if (closeOnClickOverlay) {
onClose(e);
}
},
[closeOnClickOverlay, onClose]
);

const rootClass = classNames('at-modal', { 'at-modal--active': isOpened }, className)

if (title || content) {
return (
<View className={rootClass} style={style}>
<View onClick={this.handleClickOverlay} className="at-modal__overlay" />
<View className="at-modal__container">
{title && <View className="at-modal__header">{title}</View>}
{content && (
<ScrollView className="at-modal__content" scrollY>
<View className="content-simple">{content}</View>
</ScrollView>
)}
{(cancelText || confirmText) && (
<View className={classNames('at-modal__footer', 'at-modal__footer--simple')}>
<View className="at-modal__action">
{cancelText && <Button onClick={onCancel}>{cancelText}</Button>}
{confirmText && <Button onClick={onConfirm}>{confirmText}</Button>}
</View>
</View>
)}
</View>
</View>
)
}
const rootClass = classNames('at-modal', { 'at-modal--active': isOpened }, className);

if (title || content) {
return (
<View className={rootClass} style={style} onTouchMove={e => e.stopPropagation()}>
<View className="at-modal__overlay" onClick={onClickOverlay} />
<View className="at-modal__container">{children}</View>
<View className={rootClass} style={style}>
<View onClick={onClickOverlay} className="at-modal__overlay" />
<View className="at-modal__container">
{title && <View className="at-modal__header">{title}</View>}
{content && (
<ScrollView className="at-modal__content" scrollY>
<View className="content-simple">{content}</View>
</ScrollView>
)}
{(cancelText || confirmText) && (
<View className={classNames('at-modal__footer', 'at-modal__footer--simple')}>
<View className="at-modal__action">
{cancelText && <Button onClick={onCancel}>{cancelText}</Button>}
{confirmText && <Button onClick={onConfirm}>{confirmText}</Button>}
</View>
</View>
)}
</View>
)
}
</View>
);
}

return (
<View className={rootClass} style={style} onTouchMove={(e) => e.stopPropagation()}>
<View className="at-modal__overlay" onClick={onClickOverlay} />
<View className="at-modal__container">{children}</View>
</View>
);
};
Loading

0 comments on commit 3b9a810

Please sign in to comment.