Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit d361ac3

Browse files
Merge pull request #381 from plotly/graph_default_id
Refactor Graph.defaultProps.id code into function
2 parents a8ebbf8 + 46cd4a6 commit d361ac3

9 files changed

+62
-51
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.38.1] - 2018-11-21
6+
### Fixed
7+
- Refactored the way the Graph component would generate an unique id if none provided.
8+
59
## [0.38.0] - 2018-11-07
610
### Fixed
711
- Changed the way the default CSS files for some components are loaded to being loaded with webpack instead of as dependencies.

dash_core_components/dash_core_components.dev.js

+2-2
Large diffs are not rendered by default.

dash_core_components/dash_core_components.min.js

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dash_core_components/metadata.json

+3-27
Original file line numberDiff line numberDiff line change
@@ -1120,39 +1120,15 @@
11201120
},
11211121
"src/components/Graph.react.js": {
11221122
"description": "",
1123-
"displayName": "PlotlyGraph",
1124-
"methods": [
1125-
{
1126-
"name": "plot",
1127-
"docblock": null,
1128-
"modifiers": [],
1129-
"params": [
1130-
{
1131-
"name": "props",
1132-
"type": null
1133-
}
1134-
],
1135-
"returns": null
1136-
},
1137-
{
1138-
"name": "bindEvents",
1139-
"docblock": null,
1140-
"modifiers": [],
1141-
"params": [],
1142-
"returns": null
1143-
}
1144-
],
1123+
"displayName": "GraphWithDefaults",
1124+
"methods": [],
11451125
"props": {
11461126
"id": {
11471127
"type": {
11481128
"name": "string"
11491129
},
11501130
"required": false,
1151-
"description": "The ID of this component, used to identify dash components\nin callbacks. The ID needs to be unique across all of the\ncomponents in an app.",
1152-
"defaultValue": {
1153-
"value": "'graph-' +\nMath.random()\n .toString(36)\n .substring(2, 7)",
1154-
"computed": false
1155-
}
1131+
"description": "The ID of this component, used to identify dash components\nin callbacks. The ID needs to be unique across all of the\ncomponents in an app."
11561132
},
11571133
"clickData": {
11581134
"type": {

dash_core_components/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dash-core-components",
3-
"version": "0.38.0",
3+
"version": "0.38.1",
44
"description": "Core component suite for Dash",
55
"repository": {
66
"type": "git",
@@ -55,7 +55,7 @@
5555
"react-select-fast-filter-options": "^0.2.2",
5656
"react-syntax-highlighter": "^5.0.0",
5757
"react-virtualized-select": "^3.1.0",
58-
"style-loader": "^0.21.0",
58+
"style-loader": "^0.23.1",
5959
"styled-jsx": "^3.1.0",
6060
"webpack": "^4.8.3",
6161
"webpack-cli": "^2.1.3",

dash_core_components/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.38.0'
1+
__version__ = '0.38.1'

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dash-core-components",
3-
"version": "0.38.0",
3+
"version": "0.38.1",
44
"description": "Core component suite for Dash",
55
"repository": {
66
"type": "git",

src/components/Graph.react.js

+29-12
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,31 @@ const filterEventData = (gd, eventData, event) => {
6464
return filteredEventData;
6565
};
6666

67-
export default class PlotlyGraph extends Component {
67+
function generateId() {
68+
const charAmount = 36;
69+
const length = 7;
70+
return (
71+
'graph-' +
72+
Math.random()
73+
.toString(charAmount)
74+
.substring(2, length)
75+
);
76+
}
77+
78+
const GraphWithDefaults = props => {
79+
const id = props.id ? props.id : generateId();
80+
return <PlotlyGraph {...props} id={id} />;
81+
};
82+
83+
class PlotlyGraph extends Component {
6884
constructor(props) {
6985
super(props);
7086
this.bindEvents = this.bindEvents.bind(this);
7187
this._hasPlotted = false;
7288
}
7389

7490
plot(props) {
75-
const {id, figure, animate, animation_options, config} = props;
91+
const {figure, id, animate, animation_options, config} = props;
7692
const gd = document.getElementById(id);
7793

7894
if (
@@ -94,7 +110,7 @@ export default class PlotlyGraph extends Component {
94110
}
95111

96112
bindEvents() {
97-
const {id, fireEvent, setProps, clear_on_unhover} = this.props;
113+
const {fireEvent, id, setProps, clear_on_unhover} = this.props;
98114

99115
const gd = document.getElementById(id);
100116

@@ -225,7 +241,7 @@ export default class PlotlyGraph extends Component {
225241
}
226242
}
227243

228-
PlotlyGraph.propTypes = {
244+
const graphPropTypes = {
229245
/**
230246
* The ID of this component, used to identify dash components
231247
* in callbacks. The ID needs to be unique across all of the
@@ -493,14 +509,7 @@ PlotlyGraph.propTypes = {
493509
fireEvent: PropTypes.func,
494510
};
495511

496-
PlotlyGraph.defaultProps = {
497-
/* eslint-disable no-magic-numbers */
498-
id:
499-
'graph-' +
500-
Math.random()
501-
.toString(36)
502-
.substring(2, 7),
503-
/* eslint-enable no-magic-numbers */
512+
const graphDefaultProps = {
504513
clickData: null,
505514
clickAnnotationData: null,
506515
hoverData: null,
@@ -556,3 +565,11 @@ PlotlyGraph.defaultProps = {
556565
mapboxAccessToken: null,
557566
},
558567
};
568+
569+
GraphWithDefaults.propTypes = graphPropTypes;
570+
PlotlyGraph.propTypes = graphPropTypes;
571+
572+
GraphWithDefaults.defaultProps = graphDefaultProps;
573+
PlotlyGraph.defaultProps = graphDefaultProps;
574+
575+
export default GraphWithDefaults;

test/test_integration.py

+14
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,20 @@ def test_graphs_with_different_figures(self):
900900

901901
self.snapshot('2 graphs with different figures')
902902

903+
def test_graphs_without_ids(self):
904+
app = dash.Dash(__name__)
905+
app.layout = html.Div([
906+
dcc.Graph(className='graph-no-id-1'),
907+
dcc.Graph(className='graph-no-id-2'),
908+
])
909+
910+
self.startServer(app=app)
911+
912+
graph_1 = self.wait_for_element_by_css_selector('.graph-no-id-1')
913+
graph_2 = self.wait_for_element_by_css_selector('.graph-no-id-2')
914+
915+
self.assertNotEqual(graph_1.get_attribute('id'), graph_2.get_attribute('id'))
916+
903917
def test_datepickerrange_updatemodes(self):
904918
app = dash.Dash(__name__)
905919

0 commit comments

Comments
 (0)