11import React , { useEffect , useState } from 'react' ;
2- import RCInputNumber from 'rc-input-number ' ;
2+ import { NumberInput } from '@patternfly/react-core ' ;
33import PropTypes from 'prop-types' ;
4- import { translate as __ } from '../../../../common/I18n' ;
4+ import { translate as __ , sprintf } from '../../../../common/I18n' ;
55import { noop } from '../../../../common/helpers' ;
66
77const CounterInput = ( {
@@ -16,37 +16,87 @@ const CounterInput = ({
1616 onChange,
1717 setError,
1818 setWarning,
19+ widthChars,
20+ unit,
21+ handlePlus,
22+ handleMinus,
1923} ) => {
2024 const [ innerValue , setInnerValue ] = useState ( value ) ;
25+ const [ validated , setValidated ] = useState ( 'default' ) ;
26+
27+ useEffect ( ( ) => {
28+ setInnerValue ( value ) ;
29+ } , [ value ] ) ;
30+
2131 useEffect ( ( ) => {
2232 if ( max && innerValue > max ) {
2333 setWarning ( null ) ;
24- setError ( __ ( 'Specified value is higher than maximum value' ) ) ;
34+ setError (
35+ sprintf (
36+ __ ( 'Specified value is higher than maximum value %s %s' ) ,
37+ max ,
38+ unit
39+ )
40+ ) ;
41+ setValidated ( 'error' ) ;
2542 } else if ( recommendedMaxValue && innerValue > recommendedMaxValue ) {
2643 setError ( null ) ;
27- setWarning ( __ ( 'Specified value is higher than recommended maximum' ) ) ;
44+ setWarning (
45+ sprintf (
46+ __ ( 'Specified value is higher than recommended maximum %s %s' ) ,
47+ recommendedMaxValue ,
48+ unit
49+ )
50+ ) ;
51+ setValidated ( 'warning' ) ;
2852 } else {
2953 setError ( null ) ;
3054 setWarning ( null ) ;
55+ setValidated ( 'default' ) ;
3156 }
32- // eslint-disable-next-line react-hooks/exhaustive-deps
33- } , [ recommendedMaxValue , max , innerValue ] ) ;
57+ } , [ recommendedMaxValue , max , innerValue , setError , setWarning , unit ] ) ;
58+ const setValue = newValue => {
59+ setInnerValue ( newValue ) ;
60+ onChange ( newValue ) ;
61+ } ;
62+ const handleChange = event => {
63+ const inputValue = event . target . value ;
64+ const numValue = inputValue === '' ? '' : parseInt ( inputValue , 10 ) || min ;
65+ setValue ( numValue ) ;
66+ } ;
3467
35- const handleChange = v => {
36- setInnerValue ( v ) ;
37- onChange ( v ) ;
68+ const defaultHandlePlus = ( ) => {
69+ if ( handlePlus ) {
70+ handlePlus ( innerValue ) ;
71+ } else {
72+ const newValue = ( innerValue || 0 ) + ( step || 1 ) ;
73+ setValue ( newValue ) ;
74+ }
75+ } ;
76+
77+ const defaultHandleMinus = ( ) => {
78+ if ( handleMinus ) {
79+ handleMinus ( innerValue ) ;
80+ } else {
81+ const newValue = ( innerValue || 0 ) - ( step || 1 ) ;
82+ setValue ( newValue ) ;
83+ }
3884 } ;
3985
4086 return (
41- < RCInputNumber
42- value = { innerValue }
43- name = { name }
44- id = { id }
87+ < NumberInput
88+ value = { innerValue === null || innerValue === undefined ? 0 : innerValue }
89+ inputName = { name }
90+ inputProps = { { id , name } }
4591 min = { min }
46- disabled = { disabled }
92+ max = { max }
93+ isDisabled = { disabled }
4794 onChange = { handleChange }
48- step = { step }
49- prefixCls = "foreman-numeric-input"
95+ onPlus = { defaultHandlePlus }
96+ onMinus = { defaultHandleMinus }
97+ validated = { validated }
98+ widthChars = { widthChars }
99+ unit = { unit }
50100 />
51101 ) ;
52102} ;
@@ -60,20 +110,28 @@ CounterInput.propTypes = {
60110 recommendedMaxValue : PropTypes . number ,
61111 /** Set the max value of the numeric input */
62112 max : PropTypes . number ,
63- /** Set the min value of the numeric input */
113+ /** Set the min value of the numeric input, undefined will be defaulted to 0 */
64114 min : PropTypes . number ,
65115 /** Set whether the numeric input will be disabled or not */
66116 disabled : PropTypes . bool ,
67117 /** Set the onChange function of the numeric input */
68118 onChange : PropTypes . func ,
69119 /** Set the default value of the numeric input */
70- value : PropTypes . number ,
120+ value : PropTypes . oneOfType ( [ PropTypes . number , PropTypes . string ] ) ,
71121 /** Set the step, the counter will increase and decrease by */
72122 step : PropTypes . number ,
73123 /** Component passes the validation error to this function */
74124 setError : PropTypes . func ,
75125 /** Component passes the validation warning to this function */
76126 setWarning : PropTypes . func ,
127+ /** Set the width of the numeric input in characters */
128+ widthChars : PropTypes . number ,
129+ /** Set the unit of the numeric input */
130+ unit : PropTypes . string ,
131+ /** Override the default handlePlus function */
132+ handlePlus : PropTypes . func ,
133+ /** Override the default handleMinus function */
134+ handleMinus : PropTypes . func ,
77135} ;
78136
79137CounterInput . defaultProps = {
@@ -83,11 +141,15 @@ CounterInput.defaultProps = {
83141 value : 1 ,
84142 step : 1 ,
85143 min : 1 ,
86- max : null ,
144+ max : undefined ,
87145 recommendedMaxValue : null ,
88146 onChange : noop ,
89147 setError : noop ,
90148 setWarning : noop ,
149+ widthChars : 10 ,
150+ unit : '' ,
151+ handlePlus : null ,
152+ handleMinus : null ,
91153} ;
92154
93155export default CounterInput ;
0 commit comments