1
1
import * as React from 'react'
2
2
import { DateTime } from 'luxon'
3
-
import numbro from 'numbro'
4
3
import * as DateTimePicker from 'react-widgets/lib/DateTimePicker'
5
4
import { 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'
7
6
import { LineBaseController, LineBaseProps, useController } from '../Lines/LineBase'
8
7
import { FormGroup } from '../Lines/FormGroup'
9
8
import { FormControlReadonly } from '../Lines/FormControlReadonly'
@@ -12,6 +11,7 @@ import TextArea from '../Components/TextArea';
12
11
import 'react-widgets/dist/css/react-widgets.css';
13
12
import { KeyCodes } from '../Components/Basic';
14
13
import { format } from 'd3';
14
+
import { isPrefix } from '../FindOptions'
15
15
16
16
export interface ValueLineProps extends LineBaseProps {
17
17
valueLineType?: ValueLineType;
@@ -455,14 +455,14 @@ ValueLineRenderers.renderers["Decimal" as ValueLineType] = (vl) => {
455
455
function numericTextBox(vl: ValueLineController, validateKey: (e: React.KeyboardEvent<any>) => boolean) {
456
456
const s = vl.props
457
457
458
-
const numbroFormat = toNumbroFormat(s.formatText);
458
+
const numberFormat = toNumberFormat(s.formatText);
459
459
460
460
if (s.ctx.readOnly)
461
461
return (
462
462
<FormGroup ctx={s.ctx} labelText={s.labelText} helpText={s.helpText} htmlAttributes={{ ...vl.baseHtmlAttributes(), ...s.formGroupHtmlAttributes }} labelHtmlAttributes={s.labelHtmlAttributes}>
463
463
{vl.withItemGroup(
464
464
<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)}
466
466
</FormControlReadonly>)}
467
467
</FormGroup>
468
468
);
@@ -498,7 +498,7 @@ function numericTextBox(vl: ValueLineController, validateKey: (e: React.Keyboard
498
498
onChange={handleOnChange}
499
499
formControlClass={classes(s.ctx.formControlClass, vl.mandatoryClass)}
500
500
validateKey={validateKey}
501
-
format={numbroFormat}
501
+
format={numberFormat}
502
502
innerRef={vl.inputElement as React.RefObject<HTMLInputElement>}
503
503
/>
504
504
)}
@@ -510,7 +510,7 @@ export interface NumericTextBoxProps {
510
510
value: number | null;
511
511
onChange: (newValue: number | null) => void;
512
512
validateKey: (e: React.KeyboardEvent<any>) => boolean;
513
-
format?: string;
513
+
format: Intl.NumberFormat;
514
514
formControlClass?: string;
515
515
htmlAttributes?: React.HTMLAttributes<HTMLInputElement>;
516
516
innerRef?: ((ta: HTMLInputElement | null) => void) | React.RefObject<HTMLInputElement>;
@@ -522,7 +522,7 @@ export function NumericTextBox(p: NumericTextBoxProps) {
522
522
523
523
524
524
const value = text != undefined ? text :
525
-
p.value != undefined ? numbro(p.value).format(p.format) :
525
+
p.value != undefined ? p.format?.format(p.value) :
526
526
"";
527
527
528
528
return <input ref={p.innerRef} {...p.htmlAttributes}
@@ -552,15 +552,10 @@ export function NumericTextBox(p: NumericTextBoxProps) {
552
552
553
553
let value = ValueLineController.autoFixString(input.value, false);
554
554
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";
562
557
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);
564
559
setText(undefined);
565
560
if (result != p.value)
566
561
p.onChange(result);
@@ -569,6 +564,29 @@ export function NumericTextBox(p: NumericTextBoxProps) {
569
564
p.htmlAttributes.onBlur(e);
570
565
}
571
566
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
+
572
590
function handleOnChange(e: React.SyntheticEvent<any>) {
573
591
const input = e.currentTarget as HTMLInputElement;
574
592
setText(input.value);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4