|
| 1 | +/** |
| 2 | + * @fileoverview Note item component. |
| 3 | + */ |
| 4 | +import React, { PropTypes } from 'react' |
| 5 | +import { isArray } from 'lodash' |
| 6 | +import CSSModules from 'browser/lib/CSSModules' |
| 7 | +import styles from './NoteList.styl' |
| 8 | + |
| 9 | +/** |
| 10 | + * @description Tag element component. |
| 11 | + * @param {string} tagName |
| 12 | + * @return {React.Component} |
| 13 | + */ |
| 14 | +const TagElement = ({ tagName }) => ( |
| 15 | + <span styleName='item-bottom-tagList-item' key={tagName}> |
| 16 | + {tagName} |
| 17 | + </span> |
| 18 | +) |
| 19 | + |
| 20 | +/** |
| 21 | + * @description Tag element list component. |
| 22 | + * @param {Array|null} tags |
| 23 | + * @return {React.Component} |
| 24 | + */ |
| 25 | +const TagElementList = (tags) => { |
| 26 | + if (!isArray(tags)) { |
| 27 | + return [] |
| 28 | + } |
| 29 | + |
| 30 | + const tagElements = tags.map(tag => ( |
| 31 | + TagElement({tagName: tag}) |
| 32 | + )) |
| 33 | + |
| 34 | + return tagElements |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * @description Note item component when using normal display mode. |
| 39 | + * @param {boolean} isActive |
| 40 | + * @param {Object} note |
| 41 | + * @param {Function} handleNoteClick |
| 42 | + * @param {Function} handleNoteContextMenu |
| 43 | + * @param {string} dateDisplay |
| 44 | + */ |
| 45 | +const NoteItem = ({ isActive, note, dateDisplay, handleNoteClick, handleNoteContextMenu }) => ( |
| 46 | + <div styleName={isActive |
| 47 | + ? 'item--active' |
| 48 | + : 'item' |
| 49 | + } |
| 50 | + key={`${note.storage}-${note.key}`} |
| 51 | + onClick={e => handleNoteClick(e, `${note.storage}-${note.key}`)} |
| 52 | + onContextMenu={e => handleNoteContextMenu(e, `${note.storage}-${note.key}`)} |
| 53 | + > |
| 54 | + <div styleName='item-bottom-time'>{dateDisplay}</div> |
| 55 | + |
| 56 | + <div styleName='item-title'> |
| 57 | + {note.title.trim().length > 0 |
| 58 | + ? note.title |
| 59 | + : <span styleName='item-title-empty'>Empty</span> |
| 60 | + } |
| 61 | + </div> |
| 62 | + |
| 63 | + <div styleName='item-bottom'> |
| 64 | + <div styleName='item-bottom-tagList'> |
| 65 | + {note.tags.length > 0 |
| 66 | + ? TagElementList(note.tags) |
| 67 | + : '' |
| 68 | + } |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + |
| 72 | + <i styleName='item-star' |
| 73 | + className={note.isStarred |
| 74 | + ? 'fa fa-star' |
| 75 | + : 'fa fa-star-o' |
| 76 | + } |
| 77 | + /> |
| 78 | + </div> |
| 79 | +) |
| 80 | + |
| 81 | +NoteItem.propTypes = { |
| 82 | + isActive: PropTypes.bool.isRequired, |
| 83 | + dateDisplay: PropTypes.string.isRequired, |
| 84 | + note: PropTypes.shape({ |
| 85 | + storage: PropTypes.string.isRequired, |
| 86 | + key: PropTypes.string.isRequired, |
| 87 | + title: PropTypes.string.isrequired, |
| 88 | + tags: PropTypes.array, |
| 89 | + isStarred: PropTypes.bool.isRequired, |
| 90 | + }), |
| 91 | + handleNoteClick: PropTypes.func.isRequired, |
| 92 | + handleNoteContextMenu: PropTypes.func.isRequired, |
| 93 | +} |
| 94 | + |
| 95 | +export default CSSModules(NoteItem, styles) |
0 commit comments