Skip to content

Commit 20530d8

Browse files
李钿李钿
authored andcommitted
添加打开和关闭组件代码
1 parent 99187b5 commit 20530d8

File tree

4 files changed

+37
-90
lines changed

4 files changed

+37
-90
lines changed

example/App.jsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,46 @@ class App extends Component {
1414
require('./assets/0.jpg'),
1515
require('./assets/3.jpg'),
1616
require('./assets/4.jpg'),
17-
]
17+
],
18+
index: 0,
19+
isOpen: false
1820
};
1921

22+
onClose = () =>{
23+
this.setState({
24+
isOpen: false
25+
})
26+
}
27+
28+
openViewer (index){
29+
this.setState({
30+
index,
31+
isOpen: true
32+
})
33+
}
34+
2035
render() {
36+
const {
37+
imags,
38+
index,
39+
isOpen
40+
} = this.state;
41+
2142
return (
2243
<div className="app">
2344
<div className="img-list">
2445
{
25-
this.state.imags.map(item => {
46+
this.state.imags.map((item, index) => {
2647
console.info("111",item);
2748
return <div className="img" key={item}>
28-
<img src={item} alt="" width="100%" height="auto" className=""/>
49+
<img src={item} alt="" onClick={this.openViewer.bind(this, index)} width="100%" height="auto" className=""/>
2950
</div>
3051
})
3152
}
3253
</div>
33-
<WxImageViewer urls={this.state.imags} index={2}/>
54+
{
55+
isOpen ? <WxImageViewer onClose={this.onClose} urls={this.state.imags} index={index}/> : ""
56+
}
3457
</div>
3558
)
3659
}

src/components/ListContainer.jsx

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,6 @@ import {Motion, spring} from 'react-motion';
44

55
import ImageContainer from './ImageContainer'
66

7-
class HandleWrapContainer extends PureComponent {
8-
9-
state = {
10-
left: 0,
11-
isNeedSpring: false
12-
}
13-
14-
handleTouchStart = (event) =>{
15-
console.info("handleTouchStart")
16-
let targetEvent = event.changedTouches[0];
17-
this.startX = targetEvent.clientX;
18-
this.startY = targetEvent.clientY;
19-
20-
this.startLeft = this.state.left;
21-
this.setState({
22-
isNeedSpring: false
23-
})
24-
event.preventDefault();
25-
}
26-
27-
handleTouchMove = (event) =>{
28-
// console.info("handleTouchMove")
29-
let targetEvent = event.changedTouches[0],
30-
diffX = targetEvent.clientX - this.startX,
31-
left = this.startLeft + diffX;
32-
33-
this.setState({left})
34-
event.preventDefault();
35-
}
36-
37-
handleTouchEnd = (event) =>{
38-
console.info("handleTouchEnd")
39-
40-
let left = Math.round(this.state.left/this.props.diff)*this.props.diff
41-
if(left > 0){ left = 0}
42-
if(left < -this.props.maxW){ left = -this.props.maxW}
43-
this.setState({
44-
left,
45-
isNeedSpring: true
46-
})
47-
event.preventDefault();
48-
}
49-
50-
render(){
51-
const {
52-
children,
53-
style,
54-
} = this.props
55-
56-
const {
57-
left,
58-
isNeedSpring
59-
} = this.state
60-
61-
return (
62-
<Motion style={{x: isNeedSpring ? spring(left) : left}}>
63-
{
64-
({x}) => {
65-
let defaultStyle = {
66-
WebkitTransform: `translate3d(${x}px, 0, 0)`,
67-
transform: `translate3d(${x}px, 0, 0)`,
68-
}
69-
70-
return (
71-
<div
72-
className="viewer-list-container"
73-
onTouchStart={this.handleTouchStart}
74-
onTouchMove={this.handleTouchMove}
75-
onTouchEnd={this.handleTouchEnd}
76-
style={defaultStyle}
77-
>
78-
{ children }
79-
</div>
80-
)
81-
}
82-
}
83-
</Motion>
84-
)
85-
}
86-
}
87-
887
class ListContainer extends PureComponent {
898
static propTypes = {
909
gap: PropTypes.number,

src/components/WrapViewer.jsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ class WrapViewer extends Component {
1919
static defaultProps = {
2020
zIndex: 100,
2121
}
22-
23-
componentWillMount() {
24-
25-
}
2622

2723
render() {
2824
const {
2925
zIndex,
3026
urls,
31-
index
27+
index,
28+
onClose
3229
} = this.props
3330

3431
defaultStyle.zIndex = zIndex;

src/components/WxImageViewer.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ class WxImageViewer extends Component {
2828
zIndex: PropTypes.number,
2929
urls: PropTypes.array.isRequired, // 需要预览的图片http链接列表
3030
index: PropTypes.number, // 当前显示图片的http链接
31+
onClose: PropTypes.func.isRequired
3132
}
33+
static childContextTypes = {
34+
onClose: PropTypes.func
35+
};
3236

3337
static defaultProps = {
3438
zIndex: 100,
@@ -52,6 +56,10 @@ class WxImageViewer extends Component {
5256
this.removePortal();
5357
}
5458

59+
getChildContext() {
60+
return {onClose: this.props.onClose};
61+
}
62+
5563
removePortal(){
5664
ReactDOM.unmountComponentAtNode(this.node);
5765

0 commit comments

Comments
 (0)