-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0d37cd
commit e5f3032
Showing
9 changed files
with
970 additions
and
16 deletions.
There are no files selected for viewing
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, { Component, PropTypes } from 'react'; | ||
import { CodeSnippet } from 'components/test'; | ||
import isString from 'lodash/isString'; | ||
import classNames from 'classnames/bind'; | ||
import styles from './test.css'; | ||
|
||
const cx = classNames.bind(styles); | ||
|
||
const imgRegEx = /https?:\/\/.*\.(?:png|jpg|gif|jpeg)/i; | ||
|
||
class TestContext extends Component { | ||
static displayName = 'TestContext'; | ||
|
||
static propTypes = { | ||
className: PropTypes.string, | ||
context: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.object, | ||
PropTypes.array | ||
]) | ||
}; | ||
|
||
renderImage = (url, title) => ( | ||
<a | ||
href={ url } | ||
className={ cx('image-link') } | ||
rel='noopener noreferrer' | ||
target='_blank' | ||
alt={ title } > | ||
<img src={ url } className={ cx('image') } role='presentation' /> | ||
</a> | ||
); | ||
|
||
renderContext = (ctx, i) => { | ||
const containerProps = { | ||
className: cx('context-item') | ||
}; | ||
if (i !== undefined) { | ||
containerProps.key = i; | ||
} | ||
|
||
// Context is a simple string | ||
if (isString(ctx)) { | ||
return ( | ||
<div { ...containerProps } > | ||
{ imgRegEx.test(ctx) | ||
? this.renderImage(ctx) | ||
: <CodeSnippet className={ cx('code-snippet') } code={ ctx } highlight={ false } /> | ||
} | ||
</div> | ||
); | ||
} | ||
|
||
// Context is an object with title and value | ||
const { title, value } = ctx; | ||
/* istanbul ignore else */ | ||
if (value) { | ||
const val = isString(value) ? value : JSON.stringify(value, null, 2); | ||
return ( | ||
<div { ...containerProps } > | ||
<h4 className={ cx('context-item-title') }>{ title }:</h4> | ||
{ imgRegEx.test(val) | ||
? this.renderImage(val, title) | ||
: <CodeSnippet className={ cx('code-snippet') } code={ val } /> | ||
} | ||
</div> | ||
); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
render() { | ||
const { className, context } = this.props; | ||
|
||
// All context comes in stringified initially so we parse it here | ||
const ctx = JSON.parse(context); | ||
const isContextArray = Array.isArray(ctx); | ||
return ( | ||
<div className={ cx(className, 'context') }> | ||
<h4 className={ cx('context-title') }>Additional Test Context</h4> | ||
{ isContextArray | ||
? ctx.map(this.renderContext) | ||
: this.renderContext(ctx) | ||
} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default TestContext; | ||
|
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as Test } from './test'; | ||
export { default as CodeSnippet } from './code-snippet'; | ||
export { default as TestList } from './list'; | ||
export { default as TestContext } from './context'; |
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
Oops, something went wrong.