-
-
Save stubbornella/e97662e4a197eb9a534a to your computer and use it in GitHub Desktop.
| 'use strict'; | |
| var React = require('react/addons'); | |
| var _ = require('lodash'); | |
| var setClass = React.addons.classSet; | |
| var MediaObject = React.createClass({ | |
| render: function () { | |
| var classes = setClass({ | |
| 'media-left': this.props.horizontalAlignment === 'left', | |
| 'media-right': this.props.horizontalAlignment === 'right', | |
| 'media-middle': this.props.verticalAlignment === 'middle', | |
| 'media-bottom': this.props.verticalAlignment === 'bottom', | |
| 'media-object': !this.props.imageHref | |
| }); | |
| var paddingClasses = setClass({ | |
| 'prs': this.props.leftMediaSpacing === 'small', | |
| 'prm': this.props.leftMediaSpacing === 'medium', | |
| 'prl': this.props.leftMediaSpacing === 'large', | |
| 'prxl': this.props.leftMediaSpacing === 'xlarge', | |
| 'pls': this.props.rightMediaSpacing === 'small', | |
| 'plm': this.props.rightMediaSpacing === 'medium', | |
| 'pll': this.props.rightMediaSpacing === 'large', | |
| 'plxl': this.props.rightMediaSpacing === 'xlarge' | |
| }); | |
| var mediaClasses = [classes, paddingClasses].join(' '); | |
| if (this.props.imageHref) { | |
| return ( | |
| <a className={mediaClasses} href={this.props.imageHref} > | |
| <img alt={this.props.alt} className="media-object" src={this.props.imageSource} height={this.props.height} width={this.props.width} /> | |
| </a> | |
| ); | |
| } else { | |
| return ( | |
| <img alt={this.props.alt} className={mediaClasses} src={this.props.imageSource} height={this.props.height} width={this.props.width} /> | |
| ); | |
| } | |
| } | |
| }); | |
| var Media = React.createClass({ | |
| render: function () { | |
| var leftMedia, | |
| rightMedia = ''; | |
| var classes = setClass({ | |
| 'media': true, | |
| 'media-stackable-xs': this.props.stackSize === 'xsmall', | |
| 'media-stackable-sm': this.props.stackSize === 'small', | |
| 'media-stackable-md': this.props.stackSize === 'medium', | |
| 'media-stackable-lg': this.props.stackSize === 'large' | |
| }); | |
| var bodyClasses = setClass({ | |
| 'media-body': true, | |
| 'media-middle': this.props.bodyAlignment === 'middle', | |
| 'media-bottom': this.props.bodyAlignment === 'bottom' | |
| }); | |
| if (this.props.leftImageSource) { | |
| leftMedia = ( | |
| <MediaObject | |
| horizontalAlignment='left' | |
| verticalAlignment={this.props.leftImageAlignment} | |
| imageHref={this.props.leftImageHref} | |
| imageSource={this.props.leftImageSource} | |
| leftMediaSpacing={this.props.leftMediaSpacing} | |
| alt={this.props.leftAlt} | |
| height={this.props.leftImageHeight} | |
| width={this.props.leftImageWidth}> | |
| </MediaObject> | |
| ); | |
| } | |
| if (this.props.rightImageSource) { | |
| rightMedia = ( | |
| <MediaObject | |
| horizontalAlignment='right' | |
| verticalAlignment={this.props.rightImageAlignment} | |
| imageHref={this.props.rightImageHref} | |
| imageSource={this.props.rightImageSource} | |
| rightMediaSpacing={this.props.rightMediaSpacing} | |
| alt={this.props.rightAlt} | |
| height={this.props.rightImageHeight} | |
| width={this.props.rightImageWidth}> | |
| </MediaObject> | |
| ); | |
| } | |
| return ( | |
| <div {...this.props} className={classes}> | |
| {leftMedia} | |
| <div className={bodyClasses}> | |
| {this.props.children} | |
| </div> | |
| {rightMedia} | |
| </div> | |
| ); | |
| } | |
| }); | |
| module.exports = { | |
| Media: Media | |
| }; |
To check if a ReactElement has a particular type, check its .type property:
var img = <Image />;
img.type === Image // true
There's no built-in PropTypes validator to check for an element with a particular type.
You need to be really careful when checking against the type field of a ReactElement. When done the naive way, it prevents you from writing abstractions.
For example, if you only accept a prop img that satisfies img.type === Image, then it only accepts a literal <Image ...> component. But it won't accept a higher level component like <ProfilePicture> that eventually renders an <Image> component.
@spicyj @vjeux I'm having difficulty getting a ReactElement to return its type the way you specified... my reproduction produced the following:
var img = <Image />
img.type === Image // false
console.log(img.type)
outputs...
function (props) {
// This constructor is overridden by mocks. The argument is used
// by mocks to assert on what gets mounted. This will later be used
// by the stand-alone class implementation.
}
which comes from ReactClass.js. Would love any feedback to clarify my understanding of how to use the type field of ReactElements!
We think the validations will be simpler with an
<Image />component, something like:But yeah, that will have to wait...