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,82 @@ 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 ( __ ( 'Specified value is higher than maximum value %s' ) , max )
36+ ) ;
37+ setValidated ( 'error' ) ;
2538 } else if ( recommendedMaxValue && innerValue > recommendedMaxValue ) {
2639 setError ( null ) ;
27- setWarning ( __ ( 'Specified value is higher than recommended maximum' ) ) ;
40+ setWarning (
41+ sprintf (
42+ __ ( 'Specified value is higher than recommended maximum %s' ) ,
43+ recommendedMaxValue
44+ )
45+ ) ;
46+ setValidated ( 'warning' ) ;
2847 } else {
2948 setError ( null ) ;
3049 setWarning ( null ) ;
50+ setValidated ( 'default' ) ;
3151 }
32- // eslint-disable-next-line react-hooks/exhaustive-deps
33- } , [ recommendedMaxValue , max , innerValue ] ) ;
52+ } , [ recommendedMaxValue , max , innerValue , setError , setWarning ] ) ;
53+ const setValue = newValue => {
54+ setInnerValue ( newValue ) ;
55+ onChange ( newValue ) ;
56+ } ;
57+ const handleChange = event => {
58+ const inputValue = event . target . value ;
59+ const numValue = inputValue === '' ? '' : parseInt ( inputValue , 10 ) || min ;
60+ setValue ( numValue ) ;
61+ } ;
3462
35- const handleChange = v => {
36- setInnerValue ( v ) ;
37- onChange ( v ) ;
63+ const defaultHandlePlus = ( ) => {
64+ if ( handlePlus ) {
65+ handlePlus ( innerValue ) ;
66+ } else {
67+ const newValue = ( innerValue || 0 ) + ( step || 1 ) ;
68+ setValue ( newValue ) ;
69+ }
70+ } ;
71+
72+ const defaultHandleMinus = ( ) => {
73+ if ( handleMinus ) {
74+ handleMinus ( innerValue ) ;
75+ } else {
76+ const newValue = ( innerValue || 0 ) - ( step || 1 ) ;
77+ setValue ( newValue ) ;
78+ }
3879 } ;
3980
4081 return (
41- < RCInputNumber
42- value = { innerValue }
43- name = { name }
44- id = { id }
82+ < NumberInput
83+ value = { innerValue === null || innerValue === undefined ? 0 : innerValue }
84+ inputName = { name }
85+ inputProps = { { id , name } }
4586 min = { min }
46- disabled = { disabled }
87+ max = { max }
88+ isDisabled = { disabled }
4789 onChange = { handleChange }
48- step = { step }
49- prefixCls = "foreman-numeric-input"
90+ onPlus = { defaultHandlePlus }
91+ onMinus = { defaultHandleMinus }
92+ validated = { validated }
93+ widthChars = { widthChars }
94+ unit = { unit }
5095 />
5196 ) ;
5297} ;
@@ -60,20 +105,28 @@ CounterInput.propTypes = {
60105 recommendedMaxValue : PropTypes . number ,
61106 /** Set the max value of the numeric input */
62107 max : PropTypes . number ,
63- /** Set the min value of the numeric input */
108+ /** Set the min value of the numeric input, undefined will be defaulted to 0 */
64109 min : PropTypes . number ,
65110 /** Set whether the numeric input will be disabled or not */
66111 disabled : PropTypes . bool ,
67112 /** Set the onChange function of the numeric input */
68113 onChange : PropTypes . func ,
69114 /** Set the default value of the numeric input */
70- value : PropTypes . number ,
115+ value : PropTypes . oneOfType ( [ PropTypes . number , PropTypes . string ] ) ,
71116 /** Set the step, the counter will increase and decrease by */
72117 step : PropTypes . number ,
73118 /** Component passes the validation error to this function */
74119 setError : PropTypes . func ,
75120 /** Component passes the validation warning to this function */
76121 setWarning : PropTypes . func ,
122+ /** Set the width of the numeric input in characters */
123+ widthChars : PropTypes . number ,
124+ /** Set the unit of the numeric input */
125+ unit : PropTypes . string ,
126+ /** Override the default handlePlus function */
127+ handlePlus : PropTypes . func ,
128+ /** Override the default handleMinus function */
129+ handleMinus : PropTypes . func ,
77130} ;
78131
79132CounterInput . defaultProps = {
@@ -83,11 +136,15 @@ CounterInput.defaultProps = {
83136 value : 1 ,
84137 step : 1 ,
85138 min : 1 ,
86- max : null ,
139+ max : undefined ,
87140 recommendedMaxValue : null ,
88141 onChange : noop ,
89142 setError : noop ,
90143 setWarning : noop ,
144+ widthChars : 10 ,
145+ unit : '' ,
146+ handlePlus : null ,
147+ handleMinus : null ,
91148} ;
92149
93150export default CounterInput ;
0 commit comments