forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[added] 'Responsive embed' component
- Loading branch information
Showing
10 changed files
with
627 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
"PanelGroup", | ||
"Popover", | ||
"ProgressBar", | ||
"ResponsiveEmbed", | ||
"Row", | ||
"SplitButton", | ||
"Tab", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const responsiveEmbedInstance = ( | ||
<div style={{width: 660, height: 'auto'}}> | ||
<ResponsiveEmbed a16by9> | ||
<embed type="image/svg+xml" src="/assets/TheresaKnott_castle.svg" /> | ||
</ResponsiveEmbed> | ||
</div> | ||
); | ||
|
||
React.render(responsiveEmbedInstance, mountNode); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { PropTypes, cloneElement } from 'react'; | ||
import warning from 'react/lib/warning'; | ||
import classNames from 'classnames'; | ||
|
||
class ResponsiveEmbed extends React.Component { | ||
render() { | ||
const { bsClass, className, a16by9, a4by3, ...props } = this.props; | ||
warning(!(!a16by9 && !a4by3), '`a16by9` or `a4by3` attribute must be set.'); | ||
warning(!(a16by9 && a4by3), 'Either `a16by9` or `a4by3` attribute can be set. Not both.'); | ||
|
||
const aspectRatio = { | ||
'embed-responsive-16by9': a16by9, | ||
'embed-responsive-4by3': a4by3 | ||
}; | ||
|
||
return ( | ||
<div className={classNames(bsClass, aspectRatio)}> | ||
{cloneElement( | ||
this.props.children, | ||
{ | ||
...props, | ||
className: classNames(className, 'embed-responsive-item') | ||
} | ||
)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
ResponsiveEmbed.defaultProps = { | ||
bsClass: 'embed-responsive', | ||
a16by9: false, | ||
a4by3: false | ||
}; | ||
|
||
ResponsiveEmbed.propTypes = { | ||
/** | ||
* bootstrap className | ||
* @private | ||
*/ | ||
bsClass: PropTypes.string, | ||
/** | ||
* This component accepts only one child element | ||
*/ | ||
children: PropTypes.element.isRequired, | ||
/** | ||
* 16by9 aspect ratio | ||
*/ | ||
a16by9: PropTypes.bool, | ||
/** | ||
* 4by3 aspect ratio | ||
*/ | ||
a4by3: PropTypes.bool | ||
}; | ||
|
||
export default ResponsiveEmbed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React from 'react'; | ||
import ReactTestUtils from 'react/lib/ReactTestUtils'; | ||
import ResponsiveEmbed from '../src/ResponsiveEmbed'; | ||
import { shouldWarn } from './helpers'; | ||
|
||
describe('ResponsiveEmbed', () => { | ||
it('should contain `embed-responsive` class', () => { | ||
let instance = ReactTestUtils.renderIntoDocument( | ||
<ResponsiveEmbed a16by9> | ||
<div /> | ||
</ResponsiveEmbed> | ||
); | ||
|
||
let instanceClassName = React.findDOMNode(instance).className; | ||
assert.ok(instanceClassName, 'embed-responsive'); | ||
}); | ||
|
||
it('should warn if neither `a16by9` nor `a4by3` attribute is set', () => { | ||
ReactTestUtils.renderIntoDocument( | ||
<ResponsiveEmbed> | ||
<div /> | ||
</ResponsiveEmbed> | ||
); | ||
|
||
shouldWarn('`a16by9` or `a4by3` attribute must be set.'); | ||
}); | ||
|
||
it('should warn about both `a16by9` or `a4by3` attributes set', () => { | ||
ReactTestUtils.renderIntoDocument( | ||
<ResponsiveEmbed a16by9 a4by3> | ||
<div /> | ||
</ResponsiveEmbed> | ||
); | ||
|
||
shouldWarn('Either `a16by9` or `a4by3` attribute can be set. Not both.'); | ||
}); | ||
|
||
it('should add `embed-responsive-item` class to child element', () => { | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<ResponsiveEmbed a16by9> | ||
<div /> | ||
</ResponsiveEmbed> | ||
); | ||
|
||
let child = React.findDOMNode(instance).firstChild; | ||
assert.ok(child.className.match(/\bembed-responsive-item\b/)); | ||
}); | ||
|
||
it('should add custom classes to child element', () => { | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<ResponsiveEmbed a16by9 className='custom-class'> | ||
<div /> | ||
</ResponsiveEmbed> | ||
); | ||
|
||
let child = React.findDOMNode(instance).firstChild; | ||
assert.ok(child.className.match(/\bcustom-class\b/)); | ||
}); | ||
|
||
it('should pass custom attributes to child element', () => { | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<ResponsiveEmbed a16by9 style={{color: 'white'}}> | ||
<div /> | ||
</ResponsiveEmbed> | ||
); | ||
|
||
let child = React.findDOMNode(instance).firstChild; | ||
assert.equal(child.style.color, 'white'); | ||
}); | ||
|
||
it('should add `embed-responsive-16by9` class with `a16by9` attribute set', () => { | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<ResponsiveEmbed a16by9> | ||
<div /> | ||
</ResponsiveEmbed> | ||
); | ||
|
||
let wrapper = React.findDOMNode(instance); | ||
assert.ok(wrapper.className.match(/\bembed-responsive-16by9\b/)); | ||
}); | ||
|
||
it('should add `embed-responsive-4by3` class with `a4by3` attribute set', () => { | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<ResponsiveEmbed a4by3> | ||
<div /> | ||
</ResponsiveEmbed> | ||
); | ||
|
||
let wrapper = React.findDOMNode(instance); | ||
assert.ok(wrapper.className.match(/\bembed-responsive-4by3\b/)); | ||
}); | ||
}); |