-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.tsx
175 lines (165 loc) · 4.1 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as React from "react";
import {
requireNativeComponent,
processColor,
NativeSyntheticEvent,
ImageURISource,
Platform,
View,
ViewProperties
} from "react-native";
import * as PropTypes from "prop-types";
import resolveAssetSource from "react-native/Libraries/Image/resolveAssetSource";
/**
* Photo data
*/
export interface Photo {
/**
* Same as React Native Image source but not support Array
* @see https://github.com/facebook/react-native/blob/master/Libraries/Image/ImageSource.js
*/
source: ImageURISource;
title?: string;
summary?: string;
titleColor?: string | number;
summaryColor?: string | number;
}
export interface MerryPhotoViewPorps {
/**
* Photos for view
*/
data: Photo[];
/**
* Start position
*/
initial: number;
/**
* Hide status bar
*/
hideStatusBar?: boolean;
/**
* Hide close button
*/
hideCloseButton?: boolean;
/**
* Hide share button
*/
hideShareButton?: boolean;
/**
* **Android only**
* Set share text the default text is `SHARE`
*/
shareText?: string;
/**
* Display or hide the viewer
*/
visible: boolean;
/**
* When viewer has dismissed but you still needs to update the visible state
*/
onDismiss: () => void;
onChange?: (data: { index: number; photo: Photo }) => void;
}
class MerryPhotoView extends React.Component<MerryPhotoViewPorps, any> {
static propTypes = {
data: PropTypes.arrayOf(
PropTypes.shape({
source: PropTypes.oneOfType([
PropTypes.shape({
uri: PropTypes.string,
headers: PropTypes.objectOf(PropTypes.string)
}),
// Opaque type returned by require('./image.jpg')
PropTypes.number,
// Multiple sources
PropTypes.arrayOf(
PropTypes.shape({
uri: PropTypes.string,
width: PropTypes.number,
height: PropTypes.number
})
)
]),
title: PropTypes.string,
summary: PropTypes.string,
titleColor: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
summaryColor: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
})
).isRequired,
visible: PropTypes.bool,
initial: PropTypes.number.isRequired,
hideStatusBar: PropTypes.bool,
hideCloseButton: PropTypes.bool,
hideShareButton: PropTypes.bool,
onDismiss: PropTypes.func.isRequired,
onChange: PropTypes.func,
shareText: PropTypes.string,
...View.propTypes
};
static defaultProps = {
visible: false
};
/**
* Handle UIColor conversions
* @param data Photo[]
*/
processor = (data: Photo[]) => {
if (data && data.length) {
return data.map(o => {
const d = { ...o };
if (typeof o.summaryColor === "string") {
d.summaryColor = processColor(o.summaryColor);
}
if (typeof o.titleColor === "string") {
d.titleColor = processColor(o.titleColor);
}
// resolve assets
d.source = resolveAssetSource(o.source);
return d;
});
}
return data;
};
onChange = (event: any) => {
const { onChange } = this.props;
if (onChange) {
const { target, ...rest } = event.nativeEvent;
onChange(rest);
}
};
render() {
// nothing
if (this.props.visible === false) {
return null;
}
const { visible, data, initial, ...props } = this.props;
const dataCopy = [...data];
const transformData = this.processor(dataCopy);
// initial
let startPosition = initial;
if (initial < 0) {
startPosition = 0;
}
if (initial > dataCopy.length) {
startPosition = dataCopy.length;
}
return (
<RNMerryPhotoView
{...(props as any)}
initial={startPosition}
data={transformData}
onChange={this.onChange}
/>
);
}
}
var RNMerryPhotoView = requireNativeComponent(
"MerryPhotoView",
MerryPhotoView,
{
nativeOnly: {
onChange: true
}
}
);
export default MerryPhotoView;