Skip to content

Commit 449da50

Browse files
authored
Merge pull request #176 from funnyzak/refactor/actionsheet
2 parents 9adc65c + 5476cec commit 449da50

File tree

13 files changed

+505
-420
lines changed

13 files changed

+505
-420
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react'
2+
import { ColorValue, GestureResponderEvent, StyleSheet, Text, TouchableHighlight, View } from 'react-native'
3+
4+
type MenuButtonProps = {
5+
title: string
6+
onPress?: ((event: GestureResponderEvent) => void) | undefined
7+
color?: ColorValue | undefined
8+
}
9+
10+
function MenuButton({ title, color, onPress }: MenuButtonProps) {
11+
return (
12+
<TouchableHighlight accessible={true} underlayColor={'rgba(0, 0, 0, 0.15)'} onPress={onPress}>
13+
<View style={styles.container}>
14+
<Text style={[styles.title, { color: color ?? '#1A212E' }]}>{title}</Text>
15+
</View>
16+
</TouchableHighlight>
17+
)
18+
}
19+
20+
const styles = StyleSheet.create({
21+
container: {
22+
flexDirection: 'row',
23+
alignItems: 'center',
24+
width: '100%',
25+
paddingTop: 10,
26+
paddingBottom: 10,
27+
paddingLeft: 20,
28+
paddingRight: 20
29+
},
30+
title: {
31+
fontSize: 16,
32+
marginLeft: 15,
33+
color: '#1A212E'
34+
}
35+
})
36+
37+
export default MenuButton
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Created by leon<[email protected]> on 22/3/10.
3+
*/
4+
import { Button } from '@src/components'
5+
import { translate } from '@src/i18n'
6+
import { SylCommon, useTheme } from '@src/theme'
7+
import { ITheme } from '@src/types'
8+
import React, { useRef } from 'react'
9+
import { Text, TextStyle, View, ViewStyle } from 'react-native'
10+
import ActionSheet, { ActionSheetRef, SheetManager, SheetProps } from 'react-native-actions-sheet'
11+
12+
/* usage:
13+
SheetManager.show('confirm-sheet', {
14+
onClose: (data: any) => {
15+
console.log('onClose', data)
16+
showToast(data)
17+
},
18+
payload: {
19+
title: '请确认',
20+
description: '确定进行此操作吗?',
21+
confirmText: '确定',
22+
cancelText: '取消'
23+
}
24+
})
25+
**/
26+
27+
const ConfirmActionSheet = (props: SheetProps) => {
28+
const { theme } = useTheme()
29+
const {
30+
sheetId,
31+
payload: {
32+
title = translate('brand.name'),
33+
description,
34+
confirmText = translate('common.confirm'),
35+
cancelText = translate('common.cancel')
36+
}
37+
} = props
38+
const actionSheetRef = useRef<ActionSheetRef>(null)
39+
40+
const buttonConfirm = (yes: boolean) => {
41+
console.log('buttonConfirm', yes, sheetId)
42+
SheetManager.hide(sheetId, {
43+
payload: yes,
44+
context: 'global'
45+
})
46+
}
47+
48+
return (
49+
<ActionSheet
50+
id={sheetId}
51+
springOffset={1}
52+
onBeforeShow={(data) => console.log(data)}
53+
ref={actionSheetRef}
54+
statusBarTranslucent
55+
drawUnderStatusBar={true}
56+
gestureEnabled={true}
57+
defaultOverlayOpacity={0.5}
58+
containerStyle={{
59+
paddingTop: theme.spacing.medium,
60+
backgroundColor: theme.colors.surface,
61+
borderTopLeftRadius: 20,
62+
borderTopRightRadius: 20
63+
}}>
64+
<View style={[styles.container(theme), SylCommon.Card.container(theme)]}>
65+
{title && <Text style={styles.title(theme)}>{title}</Text>}
66+
{description && <Text style={styles.text(theme)}>{description}</Text>}
67+
<View style={styles.buttonContainer(theme)}>
68+
<Button
69+
type="large"
70+
onPress={() => buttonConfirm(false)}
71+
textColor={theme.colors.titleText}
72+
style={[
73+
styles.button(theme),
74+
{
75+
backgroundColor: theme.colors.transparent
76+
}
77+
]}>
78+
{cancelText || translate('common.cancel')}
79+
</Button>
80+
<Button type="large" onPress={() => buttonConfirm(true)} style={styles.button(theme)}>
81+
{confirmText || translate('common.confirm')}
82+
</Button>
83+
</View>
84+
</View>
85+
</ActionSheet>
86+
)
87+
}
88+
89+
const styles = {
90+
safeareview: (theme: ITheme): ViewStyle => ({}),
91+
container: (theme: ITheme): ViewStyle => ({
92+
paddingBottom: theme.spacing.extraLarge,
93+
width: '100%',
94+
display: 'flex',
95+
flexDirection: 'column',
96+
justifyContent: 'flex-start',
97+
alignItems: 'center',
98+
backgroundColor: theme.colors.surface
99+
}),
100+
title: (theme: ITheme): TextStyle => ({
101+
...theme.typography.headingTextBold,
102+
paddingVertical: theme.spacing.small
103+
}),
104+
text: (theme: ITheme): TextStyle => ({
105+
...theme.typography.labelText,
106+
width: '100%',
107+
textAlign: 'left',
108+
paddingVertical: theme.spacing.small
109+
}),
110+
buttonContainer: (theme: ITheme): ViewStyle => ({
111+
paddingVertical: theme.spacing.small,
112+
flexDirection: 'row',
113+
display: 'flex',
114+
width: '100%',
115+
justifyContent: 'space-between'
116+
}),
117+
button: (theme: ITheme): ViewStyle => ({
118+
width: '48%',
119+
borderRadius: 20
120+
})
121+
}
122+
123+
export default ConfirmActionSheet
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React, { useRef } from 'react'
2+
import ActionSheet, { ActionSheetRef, registerSheet, SheetProps } from 'react-native-actions-sheet'
3+
import MenuButton from './Btn'
4+
5+
const Sheet = (props: SheetProps) => {
6+
const actionSheetRef = useRef<ActionSheetRef>(null)
7+
8+
return (
9+
<ActionSheet
10+
ref={actionSheetRef}
11+
id={props.sheetId}
12+
statusBarTranslucent
13+
gestureEnabled={true}
14+
containerStyle={{ backgroundColor: '#BFD7EA' }}
15+
indicatorStyle={{ backgroundColor: 'rgba(71, 87, 114, 0.13)' }}>
16+
<MenuButton title="操作 一" onPress={() => console.log('action 1')} />
17+
<MenuButton title="操作 二" onPress={() => console.log('action 2')} />
18+
<MenuButton title="操作 三" color={'#c0392b'} onPress={() => console.log('action 3...')} />
19+
</ActionSheet>
20+
)
21+
}
22+
23+
registerSheet('menu-sheet', Sheet)
24+
25+
export default Sheet
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { registerSheet } from 'react-native-actions-sheet'
2+
import ConfirmSheet from './ConfirmSheet'
3+
import './MenuSheet'
4+
5+
/**
6+
* Registering the sheets here because otherwise sheet closes on
7+
* hot reload during development.
8+
*/
9+
registerSheet('confirm-sheet', ConfirmSheet, 'global')
10+
11+
export {}
12+
13+
/**
14+
* Since we are not importing our Sheets in any component or file, we want to make sure
15+
* they are bundled by the JS bundler. Hence we will import this file in App.js.
16+
*/

src/components/common/Input.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import React from 'react'
2-
import {
3-
TextInput,
4-
View,
5-
ViewPropTypes,
6-
TextInputProps as NativeTextInputProps,
7-
ViewStyle,
8-
TextStyle
9-
} from 'react-native'
10-
import { Text } from './Text'
111
import { useTheme } from '@src/theme'
122
import { ITheme } from '@src/types'
3+
import React from 'react'
4+
import { TextInput, TextInputProps as NativeTextInputProps, TextStyle, View, ViewStyle } from 'react-native'
5+
import { Text } from './Text'
136

147
export interface TextInputProps extends NativeTextInputProps {
158
label?: string

src/components/common/Text.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React from 'react'
21
import { useTheme } from '@src/theme'
3-
import { Text as RNText, TextProps as NativeTextProps, StyleSheet, TextStyle } from 'react-native'
42
import { ITheme } from '@src/types'
53
import { validKey } from '@src/utils'
4+
import React from 'react'
5+
import { Text as RNText, TextProps as NativeTextProps, TextStyle } from 'react-native'
66

77
// Possible value for prop "type" for Text
88
const HEADING = 'heading'

src/components/toast/ToastProvider.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Created by leon<[email protected]> on 22/3/10.
33
*/
4+
import { logInfo } from '@src/helper/logger'
45
import { translate } from '@src/i18n'
56
import React, { useState } from 'react'
67
import { TextStyle } from 'react-native'
@@ -15,19 +16,23 @@ type Props = {
1516

1617
const ToastProvider = ({ children }: Props) => {
1718
const [toast, setToast] = useState<ToastComponent | undefined>(undefined)
18-
const [toastPosition, setToastPosition] = useState<ToastPositionType>('bottom')
19+
const [toastPosition, setToastPosition] = useState<ToastPositionType>('center')
1920
const [toastOpacity, setToastOpacity] = useState<number>(1)
2021

2122
const showToast = (opts: ToastShowType) => {
22-
if (typeof opts !== 'string') {
23+
logInfo('ToastProvider', 'showToast', 'opts', typeof opts, opts)
24+
25+
if (opts === undefined) {
26+
return
27+
} else if (typeof opts === 'object') {
2328
const { text, position, duration, callback, opacity } = opts
2429

25-
setToastPosition(position || 'bottom')
30+
setToastPosition(position || 'center')
2631
setToastOpacity(opacity || 1)
2732

2833
toast?.show(text, duration || 1500, callback)
2934
} else {
30-
toast?.show(opts)
35+
toast?.show(opts.toString())
3136
}
3237
}
3338

src/helper/logger.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@
4141
export const logError = (error: any) => {
4242
console.log(error)
4343
}
44+
45+
export const logInfo = (...info: any[]) => {
46+
if (info[0] instanceof Error) {
47+
console.error(info)
48+
} else {
49+
console.log(...info)
50+
}
51+
}

0 commit comments

Comments
 (0)