-
Notifications
You must be signed in to change notification settings - Fork 226
/
ShowActionSheetButton.tsx
157 lines (149 loc) · 4.25 KB
/
ShowActionSheetButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { ActionSheetOptions } from '@expo/react-native-action-sheet';
import { MaterialIcons, Entypo } from '@expo/vector-icons';
import * as React from 'react';
import { Text, View, TextStyle, ViewStyle, findNodeHandle } from 'react-native';
const icon = (name: React.ComponentProps<typeof MaterialIcons>['name']) => (
<MaterialIcons key={name} name={name} size={24} />
);
interface Props {
title: string;
showActionSheetWithOptions: (
options: ActionSheetOptions,
callback: (buttonIndex?: number) => void
) => void;
onSelection: (index?: number) => void;
withTitle?: boolean;
withMessage?: boolean;
withIcons?: boolean;
withSeparators?: boolean;
withCustomStyles?: boolean;
withCancelButtonTintColor?: boolean;
withAnchor?: boolean;
useModal?: boolean;
}
// A custom button that shows examples of different share sheet configurations
export default class ShowActionSheetButton extends React.PureComponent<Props> {
static defaultProps = {
withTitle: false,
withMessage: false,
withIcons: false,
withSeparators: false,
withCustomStyles: false,
withAnchor: false,
withCancelButtonTintColor: false,
onSelection: null,
useModal: false,
};
_anchorRef = React.createRef<any>();
_showActionSheet = () => {
const {
withAnchor,
withTitle,
withMessage,
withIcons,
withSeparators,
withCustomStyles,
withCancelButtonTintColor,
onSelection,
showActionSheetWithOptions,
useModal,
} = this.props;
// Same interface as https://facebook.github.io/react-native/docs/actionsheetios.html
const options = ['Delete', 'Disabled', 'Save', 'Cancel'];
const icons = withIcons
? [icon('delete'), icon('save'), icon('share'), icon('cancel')]
: undefined;
const title = withTitle ? 'Choose An Action' : undefined;
const message = withMessage
? 'This library tries to mimic the native share sheets as close as possible.'
: undefined;
const destructiveButtonIndex = 0;
const disabledButtonIndices = [1];
const cancelButtonIndex = 3;
const textStyle: TextStyle | undefined = withCustomStyles
? {
fontSize: 20,
fontWeight: '500',
color: 'blue',
}
: undefined;
const titleTextStyle: TextStyle | undefined = withCustomStyles
? {
fontSize: 24,
textAlign: 'center',
fontWeight: '700',
color: 'orange',
}
: undefined;
const messageTextStyle: TextStyle | undefined = withCustomStyles
? {
fontSize: 12,
color: 'purple',
textAlign: 'right',
}
: undefined;
const containerStyle: ViewStyle | undefined = withCustomStyles
? {
backgroundColor: 'lightgrey',
}
: undefined;
const anchor: number | null = this._anchorRef.current
? findNodeHandle(this._anchorRef.current)
: null;
showActionSheetWithOptions(
{
options,
cancelButtonIndex,
cancelButtonTintColor: withCancelButtonTintColor ? '#D93F0B' : undefined,
destructiveButtonIndex,
disabledButtonIndices,
title,
message,
icons,
//iPad only
anchor: withAnchor && anchor ? anchor : undefined,
// Android only
tintIcons: true,
// Android only; default is true
showSeparators: withSeparators,
// Affects Android only; default is false
textStyle,
// Android only
titleTextStyle,
// Android only
messageTextStyle,
// Android only,
containerStyle,
// Android only,
useModal,
},
(buttonIndex?: number) => {
// Do something here depending on the button index selected
onSelection(buttonIndex);
}
);
};
render() {
const { title } = this.props;
return (
<View
style={{
margin: 6,
}}>
<Entypo.Button
name="code"
backgroundColor="#3e3e3e"
onPress={this._showActionSheet}
ref={this._anchorRef}>
<Text
style={{
fontSize: 15,
color: '#fff',
}}>
{title}
</Text>
</Entypo.Button>
</View>
);
}
}