Skip to content

Instantly share code, notes, and snippets.

@stubbornella
Created December 18, 2014 15:29
Show Gist options
  • Save stubbornella/e97662e4a197eb9a534a to your computer and use it in GitHub Desktop.
Save stubbornella/e97662e4a197eb9a534a to your computer and use it in GitHub Desktop.
Media Object from OOCSS and now Bootstrap written in React/jsx
'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
};
@stubbornella
Copy link
Author

We think the validations will be simpler with an <Image /> component, something like:

  optionalUnion: React.PropTypes.oneOfType([
      React.PropTypes.instanceOf(Image),
      React.PropTypes.instanceOf(Svg),
      React.PropTypes.instanceOf(Icon)
    ]),

But yeah, that will have to wait...

@sophiebits
Copy link

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.

@vjeux
Copy link

vjeux commented Dec 28, 2014

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.

@pmeskers
Copy link

pmeskers commented Jan 2, 2015

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment