|
| 1 | +/* |
| 2 | +Copyright (c) 2015, salesforce.com, inc. All rights reserved. |
| 3 | +
|
| 4 | +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 5 | +Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 6 | +Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 7 | +Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 8 | +
|
| 9 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 10 | +*/ |
| 11 | + |
| 12 | +import React, { Component } from 'react'; |
| 13 | +import classnames from 'classnames'; |
| 14 | +import omit from 'lodash.omit'; |
| 15 | + |
| 16 | +const displayName = 'SLDSPageHeaderDetailRow'; |
| 17 | +const propTypes = { |
| 18 | + /** |
| 19 | + * Optional class name |
| 20 | + */ |
| 21 | + className: React.PropTypes.string, |
| 22 | + /** |
| 23 | + * label |
| 24 | + */ |
| 25 | + label: React.PropTypes.oneOfType([ |
| 26 | + React.PropTypes.string, |
| 27 | + React.PropTypes.element, |
| 28 | + ]), |
| 29 | + /** |
| 30 | + * The content property can be a string or a React element |
| 31 | + */ |
| 32 | + content: React.PropTypes.oneOfType([ |
| 33 | + React.PropTypes.string, |
| 34 | + React.PropTypes.element, |
| 35 | + ]), |
| 36 | + /** |
| 37 | + * Sets whether the fields truncate |
| 38 | + */ |
| 39 | + truncate: React.PropTypes.bool.isRequired, |
| 40 | + flavor: React.PropTypes.string, |
| 41 | +}; |
| 42 | +const defaultProps = { |
| 43 | + label: '', |
| 44 | + content: '', |
| 45 | + truncate: true, |
| 46 | + flavor: '1-of-4', |
| 47 | +}; |
| 48 | + |
| 49 | +class DetailBlock extends Component { |
| 50 | + render() { |
| 51 | + const { |
| 52 | + className, |
| 53 | + label, |
| 54 | + content, |
| 55 | + truncate, |
| 56 | + flavor, |
| 57 | + } = this.props; |
| 58 | + const attr = omit([ |
| 59 | + 'className', |
| 60 | + 'label', |
| 61 | + 'content', |
| 62 | + 'truncate', |
| 63 | + 'flavor', |
| 64 | + ], this.props); |
| 65 | + const classes = this._getClassNames(className, flavor); |
| 66 | + let labelElement; |
| 67 | + let contentElement; |
| 68 | + |
| 69 | + /** |
| 70 | + * Render the label |
| 71 | + */ |
| 72 | + const renderLabel = () => { |
| 73 | + const type = typeof label; |
| 74 | + |
| 75 | + if (type === 'string') { |
| 76 | + const labelClasses = classnames('slds-text-heading--label-normal', { |
| 77 | + 'slds-truncate': truncate, |
| 78 | + }); |
| 79 | + |
| 80 | + return ( |
| 81 | + <p className={labelClasses} title={label}>{label}</p> |
| 82 | + ); |
| 83 | + } else { |
| 84 | + return label; |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + /** |
| 89 | + * Render the title |
| 90 | + */ |
| 91 | + const renderContent = () => { |
| 92 | + const type = typeof content; |
| 93 | + |
| 94 | + if (type === 'string') { |
| 95 | + const labelClasses = classnames('slds-text-body--regular', { |
| 96 | + 'slds-truncate': truncate, |
| 97 | + }); |
| 98 | + |
| 99 | + return ( |
| 100 | + <p className={labelClasses} content={content}>{content}</p> |
| 101 | + ); |
| 102 | + } else { |
| 103 | + return content; |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + labelElement = renderLabel(); |
| 108 | + contentElement = renderContent(); |
| 109 | + |
| 110 | + return ( |
| 111 | + <li className={classes} {...attr}> |
| 112 | + {labelElement} |
| 113 | + {contentElement} |
| 114 | + </li> |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + _getClassNames(className, flavor) { |
| 119 | + return classnames('slds-page-header__detail-block', className, { |
| 120 | + [`slds-size--${flavor}`]: flavor, |
| 121 | + }); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +DetailBlock.displayName = displayName; |
| 126 | +DetailBlock.propTypes = propTypes; |
| 127 | +DetailBlock.defaultProps = defaultProps; |
| 128 | + |
| 129 | +module.exports = DetailBlock; |
0 commit comments