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

Commit 3d21fb4

Browse files
Added better support for floating point numbers in DatNumber
1 parent bce95dd commit 3d21fb4

File tree

8 files changed

+33
-8
lines changed

8 files changed

+33
-8
lines changed

dev/src/App.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class App extends Component {
4949
string: 'Preset A',
5050
minMaxNumber: 33,
5151
number: 40,
52+
float: 0.001,
5253
boolean: false,
5354
select: 'one',
5455
color: '#e61d5f',
@@ -81,6 +82,9 @@ class App extends Component {
8182
<div>
8283
<b>Number value:</b> {data.number}
8384
</div>
85+
<div>
86+
<b>Float value:</b> {data.float}
87+
</div>
8488
<div>
8589
<b>Checkbox value:</b> {(data.boolean) ? 'true' : 'false'}
8690
</div>
@@ -102,6 +106,8 @@ class App extends Component {
102106
<DatString path="string" label="String" />
103107
<DatNumber path="minMaxNumber" label="Number" min={0} max={100} step={1} />
104108
<DatNumber path="number" label="Number" />
109+
<DatNumber path="float" label="Float" min={0} max={100} step={0.001} />
110+
<DatNumber path="float" label="Another Float" min={0} max={100} step={0.01} />
105111
<DatBoolean path="boolean" label="Boolean" />
106112
<DatButton label="Button" onClick={this.handleClick} />
107113
<DatSelect label="Select" path='select' options={['two', 'three', 'four']}/>

dev/src/react-dat-gui/components/DatNumber.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, { Component } from 'react';
2+
import { isInteger, toNumber } from './utils';
23

34
import PropTypes from 'prop-types';
45
import Slider from './Slider';
56
import isFinite from 'lodash.isfinite';
67
import isString from 'lodash.isstring';
78
import result from 'lodash.result';
8-
import { toNumber } from './utils';
99

1010
export default class DatNumber extends Component {
1111
static propTypes = {
@@ -16,6 +16,7 @@ export default class DatNumber extends Component {
1616
path: PropTypes.string,
1717
label: PropTypes.string,
1818
labelWidth: PropTypes.number,
19+
customLabelWidth: PropTypes.number,
1920
liveUpdate: PropTypes.bool,
2021
onUpdate: PropTypes.func,
2122
_onUpdateValue: PropTypes.func,
@@ -38,6 +39,7 @@ export default class DatNumber extends Component {
3839
applyConstraints(value) {
3940
const { min, max, step } = this.props;
4041
const [ hasMin, hasMax, hasStep ] = [ isFinite(min), isFinite(max), isFinite(step) ];
42+
const decimalPlaces = (hasStep && !isInteger(step)) ? step.toString().split('.')[1].length : 0;
4143
let [ isMin, isMax ] = [ false, false ];
4244

4345
value = toNumber(value);
@@ -58,7 +60,7 @@ export default class DatNumber extends Component {
5860
}
5961
}
6062

61-
return value;
63+
return value.toFixed(decimalPlaces);
6264
}
6365

6466
handleChange = event => {
@@ -122,7 +124,7 @@ export default class DatNumber extends Component {
122124
}
123125

124126
render() {
125-
const { min, max, path, label, labelWidth } = this.props;
127+
const { min, max, path, label, labelWidth, step } = this.props;
126128
const labelText = isString(label) ? label : path;
127129
const hasSlider = isFinite(min) && isFinite(max);
128130
const controlsWidth = 100 - labelWidth;
@@ -136,6 +138,7 @@ export default class DatNumber extends Component {
136138
{hasSlider ? this.renderSlider(sliderWidth) : null}
137139
<input
138140
type="number"
141+
step={step}
139142
inputMode="numeric"
140143
value={this.state.value}
141144
style={{ width: `${inputWidth}%` }}

dev/src/react-dat-gui/components/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,16 @@ export function toNumber(value) {
22
const float = parseFloat(value);
33
return isNaN(float) ? 0 : float;
44
}
5+
6+
/**
7+
* Polyfill for isInteger.
8+
*
9+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger#Polyfill
10+
* @param {number} value
11+
* @return {bool}
12+
*/
13+
export const isInteger = Number.isInteger || function (value) {
14+
return typeof value === 'number' &&
15+
isFinite(value) &&
16+
Math.floor(value) === value;
17+
};

dev/src/react-dat-gui/style/_main.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
background: $input-color;
7979
border: 3px solid $background-color;
8080
border-radius: 0;
81-
padding: 2px 5px;
8281
margin: 0;
8382
outline: none;
8483
font-size: inherit;

dev/src/react-dat-gui/style/_number.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
input[type=text], input[type=number] {
55
color: $number-color;
6+
height: $number-height;
67
}
78

89
.slider {
@@ -15,6 +16,6 @@
1516
background-size: 0% 100%;
1617
background-repeat: no-repeat;
1718
cursor: ew-resize;
18-
height: 25px;
19+
height: $number-height;
1920
}
2021
}

dev/src/react-dat-gui/style/_string.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
input[type=text] {
55
color: $string-color;
6+
padding: 2px 5px;
67
}
78
}

dev/src/react-dat-gui/style/_vars.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ $folder-closed: #000 url(data:image/gif;base64,R0lGODlhBQAFAJEAAP////Pz8////////
2323
$folder-open: #000 url(data:image/gif;base64,R0lGODlhBQAFAJEAAP////Pz8////////yH5BAEAAAIALAAAAAAFAAUAAAIIlI+hKgFxoCgAOw==) 6px 48% no-repeat;
2424

2525
$nest-margin: 4px;
26+
$number-height: 25px;

dev/src/react-dat-gui/style/dat.css

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
background: #303030;
6666
border: 3px solid #1a1a1a;
6767
border-radius: 0;
68-
padding: 2px 5px;
6968
margin: 0;
7069
outline: none;
7170
font-size: inherit; }
@@ -132,7 +131,8 @@
132131
border-left: 5px solid #2FA1D6; }
133132

134133
.react-dat-gui .cr.number input[type=text], .react-dat-gui .cr.number input[type=number] {
135-
color: #2FA1D6; }
134+
color: #2FA1D6;
135+
height: 25px; }
136136

137137
.react-dat-gui .cr.number .slider {
138138
display: block;
@@ -150,7 +150,8 @@
150150
border-left: 5px solid #1ed36f; }
151151

152152
.react-dat-gui .cr.string input[type=text] {
153-
color: #1ed36f; }
153+
color: #1ed36f;
154+
padding: 2px 5px; }
154155

155156
.react-dat-gui .cr.select {
156157
border-left: 5px solid #F4D450; }

0 commit comments

Comments
 (0)