UNPKG

2.01 kBJSXView Raw
1/**
2 * Photo component.
3 * @class ApPhoto
4 */
5
6'use strict'
7
8import React, {PropTypes as types} from 'react'
9import classnames from 'classnames'
10import {ApImage} from 'apeman-react-image'
11import {ApTouchable} from 'apeman-react-touchable'
12
13/** @lends ApPhoto */
14const ApPhoto = React.createClass({
15
16 // --------------------
17 // Specs
18 // --------------------
19
20 propTypes: {
21 /** Image source URL */
22 imgSrc: types.string.isRequired,
23 /** Image width */
24 imgWidth: types.number,
25 /** Image height */
26 imgHeight: types.number,
27 /** Image scale policy */
28 imgScale: types.string,
29 /** Handler for tap event */
30 onTap: types.func
31 },
32
33 mixins: [],
34
35 statics: {},
36
37 getInitialState () {
38 return {}
39 },
40
41 getDefaultProps () {
42 return {
43 imgSrc: null,
44 imgWidth: 256,
45 imgHeight: 128,
46 imgScale: 'fit',
47 onTap: null
48 }
49 },
50
51 render () {
52 const s = this
53 let { props } = s
54
55 let { imgWidth, imgHeight } = props
56
57 let className = classnames('ap-photo', {
58 'ap-photo-tappable': s.isTappable()
59 }, props.className)
60 return (
61 <div className={ className }
62 style={ Object.assign({}, props.style) }>
63 <ApTouchable onTap={ s.handleTap }>
64 <div>
65 <ApImage src={ props.imgSrc }
66 width={ imgWidth }
67 height={ imgHeight }
68 scale={ props.imgScale }
69 />
70 { props.children }
71 </div>
72 </ApTouchable>
73 </div>
74 )
75 },
76
77 // --------------------
78 // Lifecycle
79 // --------------------
80
81 // ------------------
82 // Helper
83 // ------------------
84
85 handleTap (e) {
86 const s = this
87 let { props } = s
88 if (!s.isTappable()) {
89 return
90 }
91 if (props.onTap) {
92 props.onTap(e)
93 }
94 },
95
96 isTappable () {
97 const s = this
98 let { props } = s
99 return !!props.onTap
100 }
101
102 // ------------------
103 // Private
104 // ------------------
105})
106
107export default ApPhoto