Skip to content

Instantly share code, notes, and snippets.

@Aetet
Created August 6, 2015 14:10
Show Gist options
  • Save Aetet/848984fba9a73f42df64 to your computer and use it in GitHub Desktop.
Save Aetet/848984fba9a73f42df64 to your computer and use it in GitHub Desktop.
Some switch logic
import React from 'react'
export default class WidgetMap extends React.Component {
render() {
const {mapBy, children, tagName, mapKeyName, ...props} = this.props
let defaultChild
const mk = mapKeyName || 'mapKey'
const widgets = (Array.isArray(children) ? children : [children]).filter(child => {
const mapKey = child.props[mk]
if (!mapKey) {
defaultChild = child
}
return Array.isArray(mapKey) ? mapKey.indexOf(mapBy) !== -1 : mapKey === mapBy
})
const widget = widgets.length ? widgets[0] : defaultChild
return React.createElement(tagName || 'div', props || null, widget)
}
}
<WidgetMap mapBy={selectedSource.id}>
<div className="checkout-text" mapKey={PAYMENT_METHOD_TYPE.TERMINAL}>
<T>Some text</T>
<br/>
<T>Another text</T>
</div>
<T>Default text</T>
</WidgetMap>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment