11import * as React from 'react'
22import { DateTime } from 'luxon'
3- import numbro from 'numbro'
43import * as DateTimePicker from 'react-widgets/lib/DateTimePicker'
54import { Dic , addClass , classes } from '../Globals'
6- import { MemberInfo , getTypeInfo , TypeReference , toLuxonFormat , toDurationFormat , toNumbroFormat , isTypeEnum , durationToString , TypeInfo } from '../Reflection'
5+ import { MemberInfo , getTypeInfo , TypeReference , toLuxonFormat , toDurationFormat , toNumberFormat , isTypeEnum , durationToString , TypeInfo } from '../Reflection'
76import { LineBaseController , LineBaseProps , useController } from '../Lines/LineBase'
87import { FormGroup } from '../Lines/FormGroup'
98import { FormControlReadonly } from '../Lines/FormControlReadonly'
@@ -12,6 +11,7 @@ import TextArea from '../Components/TextArea';
1211import 'react-widgets/dist/css/react-widgets.css' ;
1312import { KeyCodes } from '../Components/Basic' ;
1413import { format } from 'd3' ;
14+ import { isPrefix } from '../FindOptions'
1515
1616export interface ValueLineProps extends LineBaseProps {
1717 valueLineType ?: ValueLineType ;
@@ -455,14 +455,14 @@ ValueLineRenderers.renderers["Decimal" as ValueLineType] = (vl) => {
455455function numericTextBox ( vl : ValueLineController , validateKey : ( e : React . KeyboardEvent < any > ) => boolean ) {
456456 const s = vl . props
457457
458- const numbroFormat = toNumbroFormat ( s . formatText ) ;
458+ const numberFormat = toNumberFormat ( s . formatText ) ;
459459
460460 if ( s . ctx . readOnly )
461461 return (
462462 < FormGroup ctx = { s . ctx } labelText = { s . labelText } helpText = { s . helpText } htmlAttributes = { { ...vl . baseHtmlAttributes ( ) , ...s . formGroupHtmlAttributes } } labelHtmlAttributes = { s . labelHtmlAttributes } >
463463 { vl . withItemGroup (
464464 < FormControlReadonly htmlAttributes = { vl . props . valueHtmlAttributes } ctx = { s . ctx } className = "numeric" innerRef = { vl . inputElement } >
465- { s . ctx . value == null ? "" : numbro ( s . ctx . value ) . format ( numbroFormat ) }
465+ { s . ctx . value == null ? "" : numberFormat . format ( s . ctx . value ) }
466466 </ FormControlReadonly > ) }
467467 </ FormGroup >
468468 ) ;
@@ -498,7 +498,7 @@ function numericTextBox(vl: ValueLineController, validateKey: (e: React.Keyboard
498498 onChange = { handleOnChange }
499499 formControlClass = { classes ( s . ctx . formControlClass , vl . mandatoryClass ) }
500500 validateKey = { validateKey }
501- format = { numbroFormat }
501+ format = { numberFormat }
502502 innerRef = { vl . inputElement as React . RefObject < HTMLInputElement > }
503503 />
504504 ) }
@@ -510,7 +510,7 @@ export interface NumericTextBoxProps {
510510 value : number | null ;
511511 onChange : ( newValue : number | null ) => void ;
512512 validateKey : ( e : React . KeyboardEvent < any > ) => boolean ;
513- format ?: string ;
513+ format : Intl . NumberFormat ;
514514 formControlClass ?: string ;
515515 htmlAttributes ?: React . HTMLAttributes < HTMLInputElement > ;
516516 innerRef ?: ( ( ta : HTMLInputElement | null ) => void ) | React . RefObject < HTMLInputElement > ;
@@ -522,7 +522,7 @@ export function NumericTextBox(p: NumericTextBoxProps) {
522522
523523
524524 const value = text != undefined ? text :
525- p . value != undefined ? numbro ( p . value ) . format ( p . format ) :
525+ p . value != undefined ? p . format ? .format ( p . value ) :
526526 "" ;
527527
528528 return < input ref = { p . innerRef } { ...p . htmlAttributes }
@@ -552,15 +552,10 @@ export function NumericTextBox(p: NumericTextBoxProps) {
552552
553553 let value = ValueLineController . autoFixString ( input . value , false ) ;
554554
555- if ( numbro . languageData ( ) . delimiters . decimal == ',' && ! value . contains ( "," ) && value . trim ( ) . length > 0 ) //Numbro transforms 1.000 to 1,0 in spanish or german
556- value = value + ",00" ;
557-
558- if ( p . format && p . format . endsWith ( "%" ) ) {
559- if ( value && ! value . endsWith ( "%" ) )
560- value += "%" ;
561- }
555+ //if (numbro.languageData().delimiters.decimal == ',' && !value.contains(",") && value.trim().length > 0) //Numbro transforms 1.000 to 1,0 in spanish or german
556+ // value = value + ",00";
562557
563- const result = value == undefined || value . length == 0 ? null : numbro . unformat ( value , p . format ) ;
558+ const result = value == undefined || value . length == 0 ? null : unformat ( p . format , value ) ;
564559 setText ( undefined ) ;
565560 if ( result != p . value )
566561 p . onChange ( result ) ;
@@ -569,6 +564,29 @@ export function NumericTextBox(p: NumericTextBoxProps) {
569564 p . htmlAttributes . onBlur ( e ) ;
570565 }
571566
567+ function unformat ( format : Intl . NumberFormat , str : string ) : number {
568+ var isPercentage = format . resolvedOptions ( ) . style == "percent" ;
569+ if ( isPercentage ) {
570+ format = new Intl . NumberFormat ( format . resolvedOptions ( ) . locale ) ;
571+ }
572+
573+ const thousandSeparator = format . format ( 1111 ) . replace ( / 1 / g, '' ) ;
574+ const decimalSeparator = format . format ( 1.1 ) . replace ( / 1 / g, '' ) ;
575+
576+ if ( thousandSeparator )
577+ str = str . replace ( new RegExp ( '\\' + thousandSeparator , 'g' ) , '' ) ;
578+
579+ if ( decimalSeparator )
580+ str = str . replace ( new RegExp ( '\\' + decimalSeparator ) , '.' ) ;
581+
582+ var result = parseFloat ( str ) ;
583+
584+ if ( isPercentage )
585+ return result / 100 ;
586+
587+ return result ;
588+ }
589+
572590 function handleOnChange ( e : React . SyntheticEvent < any > ) {
573591 const input = e . currentTarget as HTMLInputElement ;
574592 setText ( input . value ) ;
0 commit comments