Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(Examples): use react-source-render #3065

Merged
merged 3 commits into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { examplePathToHash, getFormattedHash, repoURL, scrollToAnchor } from 'do
import CarbonAdNative from 'docs/src/components/CarbonAd/CarbonAdNative'

import ComponentControls from '../ComponentControls'
import ComponentExampleRenderExample from './ComponentExampleRenderExample'
import ComponentExampleRenderEditor from './ComponentExampleRenderEditor'
import ComponentExampleRenderHtml from './ComponentExampleRenderHtml'
import ComponentExampleRenderSource from './ComponentExampleRenderSource'
import ComponentExampleTitle from './ComponentExampleTitle'
import renderExampleSourceCode from './renderExampleSourceCode'

const childrenStyle = {
paddingTop: 0,
Expand Down Expand Up @@ -41,6 +41,7 @@ class ComponentExample extends PureComponent {
static propTypes = {
children: PropTypes.node,
description: PropTypes.node,
displayName: PropTypes.string.isRequired,
exampleKeys: PropTypes.arrayOf(PropTypes.string).isRequired,
exampleSources: PropTypes.objectOf(PropTypes.string).isRequired,
examplePath: PropTypes.string.isRequired,
Expand All @@ -55,18 +56,11 @@ class ComponentExample extends PureComponent {
const { examplePath } = this.props
this.anchorName = examplePathToHash(examplePath)

const sourceCode = this.getOriginalSourceCode()

const { error, exampleElement, htmlMarkup } = renderExampleSourceCode(examplePath, sourceCode)

this.setState({
handleMouseLeave: this.handleMouseLeave,
handleMouseMove: this.handleMouseMove,
showCode: this.isActiveHash(),
error,
htmlMarkup,
exampleElement,
sourceCode,
sourceCode: this.getOriginalSourceCode(),
})
}

Expand Down Expand Up @@ -110,14 +104,6 @@ class ComponentExample extends PureComponent {
scrollToAnchor()
}

removeHash = () => {
const { history, location } = this.props

history.replace(location.pathname)

this.clearActiveState()
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cruft

handleDirectLinkClick = () => {
this.setHashAndScroll()
copyToClipboard(window && window.location.href)
Expand Down Expand Up @@ -187,25 +173,17 @@ class ComponentExample extends PureComponent {
}

handleChangeCode = _.debounce((sourceCode) => {
const { examplePath } = this.props
const { error, htmlMarkup, exampleElement } = renderExampleSourceCode(examplePath, sourceCode)

const newState = { error, sourceCode }
this.setState({ sourceCode })
}, 30)

// preserve the last good render if there is an error
if (!error) {
newState.htmlMarkup = htmlMarkup
newState.exampleElement = exampleElement
}
handleRenderError = error => this.setState({ error: error.toString() })

this.setState(newState)
}, 30)
handleRenderSuccess = (error, { markup }) => this.setState({ error, htmlMarkup: markup })

render() {
const { children, description, examplePath, suiVersion, title } = this.props
const { children, description, displayName, examplePath, suiVersion, title } = this.props
const {
error,
exampleElement,
handleMouseLeave,
handleMouseMove,
htmlMarkup,
Expand Down Expand Up @@ -276,12 +254,17 @@ class ComponentExample extends PureComponent {
className={`rendered-example ${this.getKebabExamplePath()}`}
style={renderedExampleStyle}
>
{exampleElement}
<ComponentExampleRenderSource
displayName={displayName}
onError={this.handleRenderError}
onSuccess={this.handleRenderSuccess}
sourceCode={sourceCode}
/>
</Grid.Column>
{(showCode || showHTML) && (
<Grid.Column width={16} style={editorStyle}>
{showCode && (
<ComponentExampleRenderExample
<ComponentExampleRenderEditor
editorId={`${this.getKebabExamplePath()}-jsx`}
githubEditHref={this.getGithubEditHref()}
originalValue={this.getOriginalSourceCode()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const menuStyle = {

const disabledStyle = { opacity: 0.5, pointerEvents: 'none' }

class ComponentExampleRenderExample extends PureComponent {
class ComponentExampleRenderEditor extends PureComponent {
state = {}

static propTypes = {
Expand Down Expand Up @@ -145,4 +145,4 @@ class ComponentExampleRenderExample extends PureComponent {
}
}

export default updateForKeys(['value'])(ComponentExampleRenderExample)
export default updateForKeys(['error', 'value'])(ComponentExampleRenderEditor)
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import faker from 'faker'
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import SourceRender from 'react-source-render'
import * as SUIR from 'semantic-ui-react'

import { updateForKeys } from 'docs/src/hoc'
import * as selectCommon from 'docs/src/examples/addons/Select/common'
import Wireframe from 'docs/src/examples/behaviors/Visibility/Wireframe'
import * as dropdownCommon from 'docs/src/examples/modules/Dropdown/common'

const babelConfig = {
presets: [
[
'env',
{
targets: {
browsers: ['last 4 versions', 'not dead'],
},
},
],
['stage-1', { decoratorsLegacy: true }],
],
}

const externals = {
faker,
lodash: require('lodash'),
react: React,
'semantic-ui-react': SUIR,
}
const commons = {
Dropdown: dropdownCommon,
Select: selectCommon,
}

const resolver = (importPath, { displayName }) => {
if (externals[importPath]) return externals[importPath]
if (commons[displayName] && _.endsWith(importPath, '/common')) return commons[displayName]
if (_.endsWith(importPath, '/Wireframe')) return Wireframe

throw new Error(
[
'An error in resolver(), please check that import is defined, details:',
JSON.stringify({ displayName, importPath }),
].join('\n'),
)
}

const ComponentExampleRenderSource = ({ displayName, onError, onSuccess, sourceCode }) => (
<SourceRender
babelConfig={babelConfig}
displayName={displayName}
onError={onError}
onSuccess={onSuccess}
resolver={resolver}
source={sourceCode}
/>
)

ComponentExampleRenderSource.propTypes = {
displayName: PropTypes.string.isRequired,
onError: PropTypes.func.isRequired,
onSuccess: PropTypes.func.isRequired,
sourceCode: PropTypes.string.isRequired,
}

export default updateForKeys(['sourceCode'])(ComponentExampleRenderSource)

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@
"react-ace": "^6.1.4",
"react-docgen": "^2.17.0",
"react-dom": "^16.0.0",
"react-hot-loader": "^4.3.4",
"react-hot-loader": "4.3.4",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-source-render": "^1.1.1",
"react-static": "^5.9.7",
"react-static-routes": "^1.0.0",
"react-test-renderer": "^16.0.0",
Expand Down
31 changes: 24 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3739,6 +3739,10 @@ deep-is@~0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"

deepmerge@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-2.1.1.tgz#e862b4e45ea0555072bf51e7fd0d9845170ae768"

default-compare@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/default-compare/-/default-compare-1.0.0.tgz#cb61131844ad84d84788fb68fd01681ca7781a2f"
Expand Down Expand Up @@ -5015,6 +5019,10 @@ fast-levenshtein@^2.0.6, fast-levenshtein@~2.0.4:
version "2.0.6"
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"

fast-memoize@^2.5.1:
version "2.5.1"
resolved "https://registry.yarnpkg.com/fast-memoize/-/fast-memoize-2.5.1.tgz#c3519241e80552ce395e1a32dcdde8d1fd680f5d"

fastparse@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/fastparse/-/fastparse-1.1.1.tgz#d1e2643b38a94d7583b479060e6c4affc94071f8"
Expand Down Expand Up @@ -9262,7 +9270,7 @@ promisify-call@^2.0.2:
dependencies:
with-callback "^1.0.2"

prop-types@^15.5.10:
prop-types@^15.5.10, prop-types@^15.6.2:
version "15.6.2"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.2.tgz#05d5ca77b4453e985d60fc7ff8c859094a497102"
dependencies:
Expand Down Expand Up @@ -9570,9 +9578,9 @@ react-helmet@^5.2.0:
prop-types "^15.5.4"
react-side-effect "^1.1.0"

react-hot-loader@^4:
version "4.3.3"
resolved "https://registry.yarnpkg.com/react-hot-loader/-/react-hot-loader-4.3.3.tgz#37409a3341c7787563d0972007ba02521f82f5d5"
react-hot-loader@4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/react-hot-loader/-/react-hot-loader-4.3.4.tgz#4f9bdd55bb20d77a6ae8931fa1c187e5f0ce6279"
dependencies:
fast-levenshtein "^2.0.6"
global "^4.3.0"
Expand All @@ -9581,9 +9589,9 @@ react-hot-loader@^4:
react-lifecycles-compat "^3.0.4"
shallowequal "^1.0.2"

react-hot-loader@^4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/react-hot-loader/-/react-hot-loader-4.3.4.tgz#4f9bdd55bb20d77a6ae8931fa1c187e5f0ce6279"
react-hot-loader@^4:
version "4.3.3"
resolved "https://registry.yarnpkg.com/react-hot-loader/-/react-hot-loader-4.3.3.tgz#37409a3341c7787563d0972007ba02521f82f5d5"
dependencies:
fast-levenshtein "^2.0.6"
global "^4.3.0"
Expand Down Expand Up @@ -9635,6 +9643,15 @@ react-side-effect@^1.1.0:
exenv "^1.2.1"
shallowequal "^1.0.1"

react-source-render@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/react-source-render/-/react-source-render-1.1.1.tgz#58cccc08b27b00bd326e235452ccdb15f96f029e"
dependencies:
deepmerge "^2.1.1"
fast-deep-equal "^2.0.1"
fast-memoize "^2.5.1"
prop-types "^15.6.2"

react-static-routes@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/react-static-routes/-/react-static-routes-1.0.0.tgz#73882f1569498d7ce5b4b1d4057ff642b1eea40c"
Expand Down