-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SIP-6] Add reactify function and convert world map to new directory …
…structure. (#5893) * reactify world map * add createAdaptor * fix typo * add schema * move directory * remove unnecessary lines * make setRef a function * convert keys to camelcase * add unit test * update formatting * add check for displayName * pass width and height as separate inputs
- Loading branch information
1 parent
75bc501
commit 8fff0d9
Showing
13 changed files
with
150 additions
and
19 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
29 changes: 29 additions & 0 deletions
29
superset/assets/spec/javascripts/utils/convertKeysToCamelCase_spec.js
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,29 @@ | ||
import { it, describe } from 'mocha'; | ||
import { expect } from 'chai'; | ||
import convertKeysToCamelCase from '../../../src/utils/convertKeysToCamelCase'; | ||
|
||
describe.only('convertKeysToCamelCase(object)', () => { | ||
it('returns undefined for undefined input', () => { | ||
expect(convertKeysToCamelCase(undefined)).to.equal(undefined); | ||
}); | ||
it('returns null for null input', () => { | ||
expect(convertKeysToCamelCase(null)).to.equal(null); | ||
}); | ||
it('returns a new object that has all keys in camelCase', () => { | ||
const input = { | ||
is_happy: true, | ||
'is-angry': false, | ||
isHungry: false, | ||
}; | ||
expect(convertKeysToCamelCase(input)).to.deep.equal({ | ||
isHappy: true, | ||
isAngry: false, | ||
isHungry: false, | ||
}); | ||
}); | ||
it('throws error if input is not a plain object', () => { | ||
expect(() => { convertKeysToCamelCase({}); }).to.not.throw(); | ||
expect(() => { convertKeysToCamelCase(''); }).to.throw(); | ||
expect(() => { convertKeysToCamelCase(new Map()); }).to.throw(); | ||
}); | ||
}); |
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,11 @@ | ||
import { mapKeys, camelCase, isPlainObject } from 'lodash/fp'; | ||
|
||
export default function convertKeysToCamelCase(object) { | ||
if (object === null || object === undefined) { | ||
return object; | ||
} | ||
if (isPlainObject(object)) { | ||
return mapKeys(k => camelCase(k), object); | ||
} | ||
throw new Error(`Cannot convert input that is not a plain object: ${object}`); | ||
} |
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,19 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import BasicChartInput from '../visualizations/models/BasicChartInput'; | ||
|
||
const IDENTITY = x => x; | ||
|
||
export default function createAdaptor(Component, transformProps = IDENTITY) { | ||
return function adaptor(slice, payload, setControlValue) { | ||
const basicChartInput = new BasicChartInput(slice, payload, setControlValue); | ||
ReactDOM.render( | ||
<Component | ||
width={slice.width()} | ||
height={slice.height()} | ||
{...transformProps(basicChartInput)} | ||
/>, | ||
document.querySelector(slice.selector), | ||
); | ||
}; | ||
} |
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,54 @@ | ||
import React from 'react'; | ||
|
||
export default function reactify(renderFn) { | ||
class ReactifiedComponent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.setContainerRef = this.setContainerRef.bind(this); | ||
} | ||
|
||
componentDidMount() { | ||
this.execute(); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.execute(); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.container = null; | ||
} | ||
|
||
setContainerRef(c) { | ||
this.container = c; | ||
} | ||
|
||
execute() { | ||
if (this.container) { | ||
renderFn(this.container, this.props); | ||
} | ||
} | ||
|
||
render() { | ||
const { id, className } = this.props; | ||
return ( | ||
<div | ||
id={id} | ||
className={className} | ||
ref={this.setContainerRef} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
if (renderFn.displayName) { | ||
ReactifiedComponent.displayName = renderFn.displayName; | ||
} | ||
if (renderFn.propTypes) { | ||
ReactifiedComponent.propTypes = renderFn.propTypes; | ||
} | ||
if (renderFn.defaultProps) { | ||
ReactifiedComponent.defaultProps = renderFn.defaultProps; | ||
} | ||
return ReactifiedComponent; | ||
} |
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,4 @@ | ||
import reactify from '../../utils/reactify'; | ||
import WorldMap from './WorldMap'; | ||
|
||
export default reactify(WorldMap); |
File renamed without changes.
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,5 @@ | ||
import createAdaptor from '../../utils/createAdaptor'; | ||
import WorldMap from './ReactWorldMap'; | ||
import transformProps from './transformProps'; | ||
|
||
export default createAdaptor(WorldMap, transformProps); |
10 changes: 10 additions & 0 deletions
10
superset/assets/src/visualizations/WorldMap/transformProps.js
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,10 @@ | ||
export default function transformProps(basicChartInput) { | ||
const { formData, payload } = basicChartInput; | ||
const { maxBubbleSize, showBubbles } = formData; | ||
|
||
return { | ||
data: payload.data, | ||
maxBubbleSize: parseInt(maxBubbleSize, 10), | ||
showBubbles, | ||
}; | ||
} |
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
10 changes: 10 additions & 0 deletions
10
superset/assets/src/visualizations/models/BasicChartInput.js
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,10 @@ | ||
import convertKeysToCamelCase from '../../utils/convertKeysToCamelCase'; | ||
|
||
export default class BasicChartInput { | ||
constructor(slice, payload, setControlValue) { | ||
this.annotationData = slice.annotationData; | ||
this.formData = convertKeysToCamelCase(slice.formData); | ||
this.payload = payload; | ||
this.setControlValue = setControlValue; | ||
} | ||
} |
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