1 | import React, { Component } from 'react'
|
2 | import { View, Modal } from 'react-native'
|
3 | import ImageCropPicker from 'react-native-image-crop-picker'
|
4 | import ImageViewer from 'react-native-image-zoom-viewer'
|
5 | import { ImagePicker, WingBlank, WhiteSpace } from '@sishuguojixuefu/antd-mobile-rn'
|
6 | import ErrorTip from './helper/ErrorTip'
|
7 | import getFieldDecorator from '../utils/getFieldDecorator'
|
8 | import Label from './helper/Label'
|
9 | import { ImagePickerProps } from '../utils/PropTypes'
|
10 |
|
11 | export class SsImagePickerView extends Component<any, any> {
|
12 | constructor(props) {
|
13 | super(props)
|
14 | this.state = {
|
15 | value: [],
|
16 | modalVisible: false,
|
17 | }
|
18 | }
|
19 |
|
20 | setModalVisible = (visible: boolean) => {
|
21 | this.setState({ modalVisible: visible })
|
22 | }
|
23 |
|
24 | setImgs(imgs: never[]) {
|
25 | this.setState({
|
26 | value: imgs,
|
27 | })
|
28 | }
|
29 |
|
30 | dismissModal = () => {
|
31 | this.setModalVisible(false)
|
32 | }
|
33 |
|
34 | private _onChange = value => {
|
35 | const { onChange } = this.props
|
36 | this.setState({ value })
|
37 | onChange(value)
|
38 | }
|
39 |
|
40 | private _clickImgItem = (index?: number) => {
|
41 | this.setModalVisible(true)
|
42 | }
|
43 |
|
44 | private _onAddImageClick = () => {
|
45 | const { value } = this.state
|
46 | ImageCropPicker.openPicker({
|
47 | multiple: true,
|
48 | waitAnimationEnd: false,
|
49 | includeExif: true,
|
50 | forceJpg: true,
|
51 | maxFiles: 9 - value.length,
|
52 | compressImageQuality: 0.5,
|
53 | })
|
54 | .then(images => {
|
55 | let files: any = []
|
56 | if (Array.isArray(images)) {
|
57 | files = images.map((item, index) => ({
|
58 | url: item.path,
|
59 | id: index,
|
60 | meta: { ...item },
|
61 | }))
|
62 | } else {
|
63 | files = [
|
64 | {
|
65 | url: images.path,
|
66 | id: 1,
|
67 | meta: { ...images },
|
68 | },
|
69 | ]
|
70 | }
|
71 |
|
72 | this.setImgs(this.state.value.concat(files))
|
73 | })
|
74 | .catch(e => {
|
75 | console.log(e)
|
76 | })
|
77 | }
|
78 |
|
79 | render() {
|
80 | const { value } = this.state
|
81 | const { label, required } = this.props
|
82 | const imgUrls = this.state.value.map((item, index) => {
|
83 | return {
|
84 | url: item.url,
|
85 | }
|
86 | })
|
87 | return (
|
88 | <View style={{ paddingVertical: 10 }}>
|
89 | <Label required={required} label={label} />
|
90 | <WhiteSpace />
|
91 | <WingBlank>
|
92 | <ImagePicker
|
93 | files={value}
|
94 | onChange={this._onChange}
|
95 | onImageClick={index => this._clickImgItem(index)}
|
96 | selectable={value.length < 9}
|
97 | onAddImageClick={this._onAddImageClick}
|
98 | />
|
99 | </WingBlank>
|
100 |
|
101 | {
|
102 | <Modal visible={this.state.modalVisible} transparent onRequestClose={this.dismissModal}>
|
103 | <ImageViewer
|
104 | imageUrls={imgUrls}
|
105 | onSwipeDown={() => {
|
106 | console.log('onSwipeDown')
|
107 | }}
|
108 | enableSwipeDown
|
109 | onCancel={this.dismissModal}
|
110 | saveToLocalByLongPress={false}
|
111 | />
|
112 | </Modal>
|
113 | }
|
114 | </View>
|
115 | )
|
116 | }
|
117 | }
|
118 |
|
119 | export default class SsImagePicker extends Component<ImagePickerProps, any> {
|
120 | private fieldDecorator: any
|
121 | static defaultProps = {
|
122 | required: false,
|
123 | }
|
124 |
|
125 | componentWillMount() {
|
126 | const { form, id, initialValue, rules } = this.props
|
127 | this.fieldDecorator = getFieldDecorator(form, id, initialValue, rules)
|
128 | }
|
129 |
|
130 | render() {
|
131 | const { label, required, form, id, onChange } = this.props
|
132 | return (
|
133 | <ErrorTip error={form.getFieldError(id)}>
|
134 | {this.fieldDecorator(<SsImagePickerView label={label} required={required} onChange={onChange} />)}
|
135 | </ErrorTip>
|
136 | )
|
137 | }
|
138 | }
|