@@ -26,7 +26,7 @@ import { Icon } from "@design-system/widgets-old";
2626import type { InputType } from "widgets/InputWidget/constants" ;
2727import { getBaseWidgetClassName } from "constants/componentClassNameConstants" ;
2828import { LabelPosition } from "components/constants" ;
29- import { lightenColor } from "widgets/WidgetUtils" ;
29+ import { getLocaleDecimalSeperator , lightenColor } from "widgets/WidgetUtils" ;
3030import LabelWithTooltip , {
3131 labelLayoutStyles ,
3232 LABEL_CONTAINER_CLASS ,
@@ -424,6 +424,82 @@ export const isNumberInputType = (inputHTMLType: InputHTMLType = "TEXT") => {
424424 return inputHTMLType === "NUMBER" ;
425425} ;
426426
427+ const getExpectedDecimalSeparator = ( shouldUseLocale ?: boolean ) : string => {
428+ if ( ! shouldUseLocale ) {
429+ return "." ;
430+ }
431+
432+ return getLocaleDecimalSeperator ( ) || "." ;
433+ } ;
434+
435+ const getAllowedNumericChars = (
436+ expectedDecimalSeparator : string ,
437+ locale ?: string ,
438+ ) : Set < string > => {
439+ const allowed = new Set < string > ( [
440+ "e" ,
441+ "E" ,
442+ "+" ,
443+ "-" ,
444+ expectedDecimalSeparator ,
445+ ] ) ;
446+
447+ for ( let digit = 0 ; digit <= 9 ; digit ++ ) {
448+ allowed . add ( String ( digit ) ) ;
449+
450+ if ( locale ) {
451+ allowed . add ( digit . toLocaleString ( locale ) ) ;
452+ }
453+ }
454+
455+ return allowed ;
456+ } ;
457+
458+ /**
459+ * Normalizes alternate decimal separators (`.` / `,`) typed by regional mobile
460+ * keypads to `expectedDecimalSeparator` and strips non-numeric characters.
461+ */
462+ export const normalizeNumericInputValue = (
463+ value : string ,
464+ expectedDecimalSeparator : string ,
465+ locale ?: string ,
466+ ) : string => {
467+ if ( ! value ) {
468+ return value ;
469+ }
470+
471+ // Convert full-width digits (e.g. Japanese IME) to ASCII, matching Blueprint's NumericInput.
472+ let normalized = value . replace ( / [ \uFF10 - \uFF19 ] / g, ( ch ) =>
473+ String . fromCharCode ( ch . charCodeAt ( 0 ) - 0xfee0 ) ,
474+ ) ;
475+
476+ if ( ! normalized . includes ( expectedDecimalSeparator ) ) {
477+ const alternates = [ "." , "," ] . filter (
478+ ( sep ) => sep !== expectedDecimalSeparator && normalized . includes ( sep ) ,
479+ ) ;
480+
481+ if ( alternates . length === 1 ) {
482+ const alt = alternates [ 0 ] ;
483+ const firstIdx = normalized . indexOf ( alt ) ;
484+
485+ // Only treat a single alternate separator as a decimal point (multiple occurrences are thousand separators).
486+ if ( firstIdx !== - 1 && normalized . indexOf ( alt , firstIdx + 1 ) === - 1 ) {
487+ normalized =
488+ normalized . slice ( 0 , firstIdx ) +
489+ expectedDecimalSeparator +
490+ normalized . slice ( firstIdx + 1 ) ;
491+ }
492+ }
493+ }
494+
495+ const allowed = getAllowedNumericChars ( expectedDecimalSeparator , locale ) ;
496+
497+ return normalized
498+ . split ( "" )
499+ . filter ( ( ch ) => allowed . has ( ch ) )
500+ . join ( "" ) ;
501+ } ;
502+
427503class BaseInputComponent extends React . Component <
428504 BaseInputComponentProps ,
429505 InputComponentState
@@ -483,8 +559,35 @@ class BaseInputComponent extends React.Component<
483559 this . props . onValueChange ( event . target . value ) ;
484560 } ;
485561
486- onNumberChange = ( valueAsNum : number , valueAsString : string ) => {
487- this . props . onValueChange ( valueAsString ) ;
562+ onNumberChange = (
563+ valueAsNum : number ,
564+ valueAsString : string ,
565+ inputElement ?: HTMLInputElement | null ,
566+ ) => {
567+ let nextValue = valueAsString ;
568+
569+ if ( this . props . allowNumericCharactersOnly !== false ) {
570+ const locale = this . props . shouldUseLocale ? getLocale ( ) : undefined ;
571+ const expectedDecimalSeparator = getExpectedDecimalSeparator (
572+ this . props . shouldUseLocale ,
573+ ) ;
574+
575+ nextValue = normalizeNumericInputValue (
576+ valueAsString ,
577+ expectedDecimalSeparator ,
578+ locale ,
579+ ) ;
580+
581+ if (
582+ inputElement &&
583+ nextValue !== valueAsString &&
584+ nextValue === ( this . props . value ?? "" )
585+ ) {
586+ inputElement . value = nextValue ;
587+ }
588+ }
589+
590+ this . props . onValueChange ( nextValue ) ;
488591 } ;
489592
490593 getType ( inputType : InputHTMLType = "TEXT" ) {
@@ -518,6 +621,49 @@ class BaseInputComponent extends React.Component<
518621 }
519622 } ;
520623
624+ onNumericKeyPress = ( e : React . KeyboardEvent < HTMLInputElement > ) => {
625+ if ( this . props . allowNumericCharactersOnly !== false ) {
626+ const { key } = e ;
627+
628+ if (
629+ key != null &&
630+ ! e . ctrlKey &&
631+ ! e . altKey &&
632+ ! e . metaKey &&
633+ key . length === 1
634+ ) {
635+ const locale = this . props . shouldUseLocale ? getLocale ( ) : undefined ;
636+ const expectedDecimalSeparator = getExpectedDecimalSeparator (
637+ this . props . shouldUseLocale ,
638+ ) ;
639+ const allowed = getAllowedNumericChars (
640+ expectedDecimalSeparator ,
641+ locale ,
642+ ) ;
643+ const isAlternateSeparator =
644+ ( key === "." || key === "," ) && key !== expectedDecimalSeparator ;
645+
646+ if ( ! allowed . has ( key ) && ! isAlternateSeparator ) {
647+ e . preventDefault ( ) ;
648+
649+ return ;
650+ }
651+
652+ if ( isAlternateSeparator ) {
653+ const currentVal = e . currentTarget . value ?? "" ;
654+ const start = e . currentTarget . selectionStart ?? currentVal . length ;
655+ const end = e . currentTarget . selectionEnd ?? start ;
656+ const unselectedVal =
657+ currentVal . slice ( 0 , start ) + currentVal . slice ( end ) ;
658+
659+ if ( unselectedVal . includes ( expectedDecimalSeparator ) ) {
660+ e . preventDefault ( ) ;
661+ }
662+ }
663+ }
664+ }
665+ } ;
666+
521667 onKeyUp = (
522668 e :
523669 | React . KeyboardEvent < HTMLTextAreaElement >
@@ -542,7 +688,7 @@ class BaseInputComponent extends React.Component<
542688
543689 return (
544690 < StyledNumericInput
545- allowNumericCharactersOnly
691+ allowNumericCharactersOnly = { false }
546692 autoFocus = { this . props . autoFocus }
547693 buttonPosition = { this . props . buttonPosition }
548694 className = {
@@ -564,6 +710,7 @@ class BaseInputComponent extends React.Component<
564710 onBlur = { ( ) => this . setFocusState ( false ) }
565711 onFocus = { ( ) => this . setFocusState ( true ) }
566712 onKeyDown = { this . onKeyDown }
713+ onKeyPress = { this . onNumericKeyPress }
567714 onKeyUp = { this . onKeyUp }
568715 onValueChange = { this . onNumberChange }
569716 placeholder = { this . props . placeholder }
0 commit comments