Commit 4ac7f41 1 parent 3128d8d commit 4ac7f41 Copy full SHA for 4ac7f41
File tree 2 files changed +37
-5
lines changed
2 files changed +37
-5
lines changed Original file line number Diff line number Diff line change @@ -304,6 +304,8 @@ class NumberPicker extends React.Component {
304
304
let result ;
305
305
if ( typeof val === 'number' ) {
306
306
result = ( precisionFactor * val + precisionFactor * step ) / precisionFactor ;
307
+
308
+ result = this . hackChrome ( result ) ;
307
309
} else {
308
310
result = min === - Infinity ? step : min ;
309
311
}
@@ -317,17 +319,27 @@ class NumberPicker extends React.Component {
317
319
if ( typeof val === 'number' ) {
318
320
result = ( precisionFactor * val - precisionFactor * step ) / precisionFactor ;
319
321
320
- // in chrome browser: 0.3 - 0.2 = 0.09999999999, we should creact to 0.1
321
- const precision = this . getPrecision ( ) ;
322
- if ( precision > 0 ) {
323
- result = Number ( Number ( result ) . toFixed ( precision ) ) ;
324
- }
322
+ result = this . hackChrome ( result ) ;
325
323
} else {
326
324
result = min === - Infinity ? - step : min ;
327
325
}
328
326
return result ;
329
327
}
330
328
329
+ /**
330
+ * fix bug in chrome browser
331
+ * 0.28 + 0.01 = 0.29000000000000004
332
+ * 0.29 - 0.01 = 0.27999999999999997
333
+ * @param {Number } value value
334
+ */
335
+ hackChrome ( value ) {
336
+ const precision = this . getPrecision ( ) ;
337
+ if ( precision > 0 ) {
338
+ return Number ( Number ( value ) . toFixed ( precision ) ) ;
339
+ }
340
+ return value ;
341
+ }
342
+
331
343
step ( type , e ) {
332
344
if ( e ) {
333
345
e . preventDefault ( ) ;
Original file line number Diff line number Diff line change @@ -318,4 +318,24 @@ describe('number-picker', () => {
318
318
done ( ) ;
319
319
} ) ;
320
320
} ) ;
321
+ describe ( 'chrome bug hack' , ( ) => {
322
+ it ( '0.28 + 0.01 should be 0.29 not 0.29000000000000004' , ( done ) => {
323
+ let onChange = ( value ) => {
324
+ assert ( value === 0.29 ) ;
325
+ done ( ) ;
326
+ } ,
327
+ wrapper = mount ( < NumberPicker defaultValue = { 0.28 } onChange = { onChange } step = { 0.01 } precision = { 2 } /> ) ;
328
+
329
+ wrapper . find ( 'button' ) . at ( 0 ) . simulate ( 'click' ) ;
330
+ } ) ;
331
+ it ( '0.29 - 0.01 should be 0.28 not 0.27999999999999997' , ( done ) => {
332
+ let onChange = ( value ) => {
333
+ assert ( value === 0.28 ) ;
334
+ done ( ) ;
335
+ } ,
336
+ wrapper = mount ( < NumberPicker defaultValue = { 0.29 } onChange = { onChange } step = { 0.01 } precision = { 2 } /> ) ;
337
+
338
+ wrapper . find ( 'button' ) . at ( 1 ) . simulate ( 'click' ) ;
339
+ } ) ;
340
+ } ) ;
321
341
} ) ;
You can’t perform that action at this time.
0 commit comments