Skip to content
This repository was archived by the owner on May 15, 2022. It is now read-only.

Commit b4b4679

Browse files
committed
make ids friendly for server rendering
1 parent adbe7b6 commit b4b4679

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

src/Dat.jsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import cloneDeep from 'lodash.clonedeep';
44
import isUndefined from 'lodash.isundefined';
55
import React, { PropTypes, cloneElement } from 'react';
66

7-
function uuid(a) {
8-
// https://gist.github.com/jed/982883
9-
return a ? (a^Math.random()*16>>a/4).toString(16) : ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, uuid);
10-
}
11-
127
class Dat extends React.Component {
138

149
static propTypes = {
@@ -25,6 +20,7 @@ class Dat extends React.Component {
2520
liveUpdate: true,
2621
};
2722

23+
2824
constructor(props, context) {
2925
super(props, context);
3026
this.handleUpdateValue = this.handleUpdateValue.bind(this);
@@ -41,7 +37,6 @@ class Dat extends React.Component {
4137
return React.Children.toArray(children).map((child, i) => {
4238
const liveUpdate = isUndefined(child.props.liveUpdate) ? this.props.liveUpdate : child.props.liveUpdate;
4339
return cloneElement(child, {
44-
id: uuid(),
4540
key: i,
4641
data,
4742
liveUpdate,

src/components/DatBoolean.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import result from 'lodash.result';
22
import isFinite from 'lodash.isfinite';
33
import isString from 'lodash.isstring';
44
import React, { PropTypes } from 'react';
5+
import { createId } from '../utils';
56

67
class DatBoolean extends React.Component {
78

89
static propTypes = {
9-
id: PropTypes.string,
1010
data: PropTypes.object,
1111
path: PropTypes.string,
1212
label: PropTypes.string,
@@ -25,6 +25,7 @@ class DatBoolean extends React.Component {
2525

2626
componentWillMount() {
2727
this.setState({
28+
id: createId(),
2829
value: this.getValue()
2930
});
3031
}
@@ -52,8 +53,7 @@ class DatBoolean extends React.Component {
5253
}
5354

5455
render() {
55-
const { id } = this.props;
56-
const { value } = this.state;
56+
const { value, id } = this.state;
5757
const label = isString(this.props.label) ? this.props.label : this.props.path;
5858
return (
5959
<li className="cr boolean">

src/components/DatNumber.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import result from 'lodash.result';
44
import isFinite from 'lodash.isfinite';
55
import isString from 'lodash.isstring';
66
import React, { PropTypes } from 'react';
7+
import { createId } from '../utils';
78

89
class DatNumber extends React.Component {
910

1011
static propTypes = {
11-
id: PropTypes.string,
1212
data: PropTypes.object,
1313
path: PropTypes.string,
1414
label: PropTypes.string,
@@ -31,13 +31,13 @@ class DatNumber extends React.Component {
3131
this.handleMouseMove = this.handleMouseMove.bind(this);
3232
this.handleMouseUp = this.handleMouseUp.bind(this);
3333
this.state = {
34-
value: 0,
3534
isSliderActive: false,
3635
};
3736
}
3837

3938
componentWillMount() {
4039
this.setState({
40+
id : createId(),
4141
value: this.getValue()
4242
});
4343
}
@@ -144,8 +144,8 @@ class DatNumber extends React.Component {
144144
}
145145

146146
render() {
147-
const { id, min, max, _labelWidth } = this.props;
148-
const { value, isSliderActive } = this.state;
147+
const { min, max, _labelWidth } = this.props;
148+
const { value, isSliderActive, id } = this.state;
149149
const label = isString(this.props.label) ? this.props.label : this.props.path;
150150
const hasSlider = isFinite(min) && isFinite(min);
151151
const labelStyle = _labelWidth ? { width: `${_labelWidth}%` } : {};

src/components/DatString.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import result from 'lodash.result';
22
import isString from 'lodash.isstring';
33
import React, { PropTypes } from 'react';
4+
import { createId } from '../utils';
45

56
class DatString extends React.Component {
67

78
static propTypes = {
8-
id: PropTypes.string,
99
data: PropTypes.object,
1010
path: PropTypes.string,
1111
label: PropTypes.string,
@@ -23,6 +23,7 @@ class DatString extends React.Component {
2323

2424
componentWillMount() {
2525
this.setState({
26+
id: createId(),
2627
value: this.getValue()
2728
});
2829
}
@@ -56,8 +57,8 @@ class DatString extends React.Component {
5657
}
5758

5859
render() {
59-
const { id, _labelWidth } = this.props;
60-
const { value } = this.state;
60+
const { _labelWidth } = this.props;
61+
const { value, id } = this.state;
6162
const label = isString(this.props.label) ? this.props.label : this.props.path;
6263
const labelStyle = _labelWidth ? { width: `${_labelWidth}%` } : {};
6364
const inputStyle = _labelWidth ? { width: `${100 - _labelWidth}%` } : {};

src/utils/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let currentId = 0;
2+
3+
export const createId = () => `__react_dat_gui_${currentId++}__`;

0 commit comments

Comments
 (0)