@@ -7,7 +7,12 @@ import { TextInput } from '../TextInput';
77import { Tooltip , TooltipProps } from '../Tooltip' ;
88import cssSliderValue from '@patternfly/react-tokens/dist/esm/c_slider_value' ;
99import cssFormControlWidthChars from '@patternfly/react-tokens/dist/esm/c_slider__value_c_form_control_width_chars' ;
10- import { formatLocalizedDecimal , getLanguageDirection , parseLocalizedDecimal } from '../../helpers/util' ;
10+ import {
11+ formatLocalizedDecimal ,
12+ getLanguageDirection ,
13+ getLocalizedInputWidthChars ,
14+ parseLocalizedDecimal
15+ } from '../../helpers/util' ;
1116
1217/** Properties for creating custom steps in a slider. These properties should be passed in as
1318 * an object within an array to the slider component's customSteps property.
@@ -134,12 +139,17 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
134139 const sliderRailRef = useRef < HTMLDivElement > ( undefined ) ;
135140 const thumbRef = useRef < HTMLDivElement > ( undefined ) ;
136141 const isInputFocusedRef = useRef ( false ) ;
137- let formatter : Intl . NumberFormat ;
138- try {
139- formatter = new Intl . NumberFormat ( locale ) ;
140- } catch {
141- formatter = new Intl . NumberFormat ( ) ; // grabs default locale
142- }
142+ const formatter = useMemo ( ( ) => {
143+ try {
144+ return new Intl . NumberFormat ( locale , { useGrouping : false } ) ;
145+ } catch ( error ) {
146+ if ( error instanceof RangeError ) {
147+ // this is what happens if a bad locale is passed in
148+ return new Intl . NumberFormat ( undefined , { useGrouping : false } ) ;
149+ }
150+ throw error ;
151+ }
152+ } , [ locale ] ) ;
143153
144154 const [ localValue , setValue ] = useState ( value ) ;
145155 const [ localInputValue , setLocalInputValue ] = useState ( inputValue ) ;
@@ -155,9 +165,12 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
155165 setValue ( value ) ;
156166 } , [ value ] ) ;
157167
158- const updateInputDisplay = useCallback ( ( numericValue : number ) => {
159- setInputDisplayValue ( formatLocalizedDecimal ( numericValue , formatter ) ) ;
160- } , [ ] ) ;
168+ const updateInputDisplay = useCallback (
169+ ( numericValue : number ) => {
170+ setInputDisplayValue ( formatLocalizedDecimal ( numericValue , formatter ) ) ;
171+ } ,
172+ [ formatter ]
173+ ) ;
161174
162175 const updateInputFromSliderValue = useCallback (
163176 ( numericValue : number ) => {
@@ -201,8 +214,15 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
201214 // calculate style value percentage
202215 const stylePercent = ( ( localValue - min ) * 100 ) / ( max - min ) ;
203216 const style = { [ cssSliderValue . name ] : `${ stylePercent } %` } as React . CSSProperties ;
204- const widthChars = useMemo ( ( ) => inputDisplayValue . length || 1 , [ inputDisplayValue ] ) ;
205- const inputStyle = { [ cssFormControlWidthChars . name ] : widthChars } as React . CSSProperties ;
217+ const inputStyle = useMemo ( ( ) => {
218+ if ( ! isInputVisible ) {
219+ return undefined ;
220+ }
221+
222+ const widthChars = getLocalizedInputWidthChars ( formatter , min , max , inputDisplayValue ) ;
223+
224+ return { [ cssFormControlWidthChars . name ] : widthChars } as React . CSSProperties ;
225+ } , [ isInputVisible , formatter , min , max , inputDisplayValue ] ) ;
206226
207227 const onChangeHandler = ( event : React . FormEvent < HTMLInputElement > , value : string ) => {
208228 setInputDisplayValue ( value ) ;
@@ -312,9 +332,10 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
312332 if ( snapValue && ! areCustomStepsContinuous ) {
313333 thumbRef . current . style . setProperty ( cssSliderValue . name , `${ snapValue } %` ) ;
314334 setValue ( snapValue ) ;
315- updateInputFromSliderValue ( snapValue ) ;
316335 if ( onChange ) {
317336 onChange ( e , snapValue , undefined , setLocalInputValueWithDisplay ) ;
337+ } else {
338+ updateInputFromSliderValue ( snapValue ) ;
318339 }
319340 }
320341 } ;
@@ -382,10 +403,11 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
382403
383404 // Call onchange callback
384405 const resolvedValue = snapValue !== undefined ? snapValue : newValue ;
385- updateInputFromSliderValue ( resolvedValue ) ;
386406
387407 if ( onChange ) {
388408 onChange ( e , resolvedValue , undefined , setLocalInputValueWithDisplay ) ;
409+ } else {
410+ updateInputFromSliderValue ( resolvedValue ) ;
389411 }
390412 } ;
391413
@@ -452,9 +474,10 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
452474 if ( newValue !== localValue ) {
453475 thumbRef . current . style . setProperty ( cssSliderValue . name , `${ newValue } %` ) ;
454476 setValue ( newValue ) ;
455- updateInputFromSliderValue ( newValue ) ;
456477 if ( onChange ) {
457478 onChange ( e , newValue , undefined , setLocalInputValueWithDisplay ) ;
479+ } else {
480+ updateInputFromSliderValue ( newValue ) ;
458481 }
459482 }
460483 } ;
0 commit comments