Skip to content

Commit de95bd7

Browse files
authored
Merge pull request #520 from dhis2/fix/add-min-max-step-attr-for-inputs
feat(input): add min/max/step attr for inputs (LIBS-133)
2 parents fc56784 + 8e7e25f commit de95bd7

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

packages/core/src/Input/Input.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ export class Input extends Component {
129129
loading,
130130
value,
131131
tabIndex,
132+
max,
133+
min,
134+
step,
132135
dataTest,
133136
} = this.props
134137

@@ -141,6 +144,9 @@ export class Input extends Component {
141144
ref={this.inputRef}
142145
type={type}
143146
value={value}
147+
max={max}
148+
min={min}
149+
step={step}
144150
disabled={disabled}
145151
readOnly={readOnly}
146152
tabIndex={tabIndex}
@@ -196,6 +202,10 @@ Input.defaultProps = {
196202
* @prop {string} [value]
197203
* @prop {string} [tabIndex]
198204
*
205+
* @prop {string} [max] The native `max` attribute
206+
* @prop {string} [min] The native `min` attribute
207+
* @prop {string} [step] The native `step` attribute
208+
*
199209
* @prop {boolean} [disabled]
200210
* @prop {boolean} [readOnly]
201211
* @prop {boolean} [dense] - Compact mode
@@ -216,9 +226,15 @@ Input.propTypes = {
216226
error: sharedPropTypes.statusPropType,
217227
initialFocus: propTypes.bool,
218228
loading: propTypes.bool,
229+
/** The [native `max` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefmax), for use when `type` is `'number'` */
230+
max: propTypes.string,
231+
/** The [native `min` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefmin), for use when `type` is `'number'` */
232+
min: propTypes.string,
219233
name: propTypes.string,
220234
placeholder: propTypes.string,
221235
readOnly: propTypes.bool,
236+
/** The [native `step` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefstep), for use when `type` is `'number'` */
237+
step: propTypes.string,
222238
tabIndex: propTypes.string,
223239
type: propTypes.oneOf([
224240
'text',
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { shallow } from 'enzyme'
2+
import React from 'react'
3+
import { Input } from '../Input.js'
4+
5+
describe('<Input>', () => {
6+
it('passes min, max, and step props as attributes to the native <input> element', () => {
7+
const testProps = { min: '0', max: '10', step: '0.5' }
8+
const wrapper = shallow(<Input type="number" {...testProps} />)
9+
10+
const inputEl = wrapper.find('input')
11+
expect(inputEl.props()).toMatchObject(testProps)
12+
})
13+
})

packages/widgets/src/InputField/InputField.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import propTypes from '@dhis2/prop-types'
22
import { sharedPropTypes } from '@dhis2/ui-constants'
33
import { Field, Input, Box } from '@dhis2/ui-core'
4+
import PropTypes from 'prop-types'
45
import React from 'react'
56

67
/**
@@ -29,6 +30,9 @@ class InputField extends React.Component {
2930
readOnly,
3031
placeholder,
3132
name,
33+
max,
34+
min,
35+
step,
3236
valid,
3337
error,
3438
warning,
@@ -65,6 +69,9 @@ class InputField extends React.Component {
6569
value={value || ''}
6670
placeholder={placeholder}
6771
disabled={disabled}
72+
max={max}
73+
min={min}
74+
step={step}
6875
valid={valid}
6976
warning={warning}
7077
error={error}
@@ -100,6 +107,10 @@ InputField.defaultProps = {
100107
* @prop {string} [tabIndex]
101108
* @prop {string} [inputWidth]
102109
*
110+
* @prop {string} [max] The native `max` attribute
111+
* @prop {string} [min] The native `min` attribute
112+
* @prop {string} [step] The native `step` attribute
113+
*
103114
* @prop {boolean} [required]
104115
* @prop {boolean} [disabled]
105116
* @prop {boolean} [readOnly]
@@ -127,10 +138,16 @@ InputField.propTypes = {
127138
inputWidth: propTypes.string,
128139
label: propTypes.string,
129140
loading: propTypes.bool,
141+
/** The [native `max` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefmax), for use when `type` is `'number'` */
142+
max: PropTypes.string,
143+
/** The [native `min` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefmin), for use when `type` is `'number'` */
144+
min: PropTypes.string,
130145
name: propTypes.string,
131146
placeholder: propTypes.string,
132147
readOnly: propTypes.bool,
133148
required: propTypes.bool,
149+
/** The [native `step` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefstep), for use when `type` is `'number'` */
150+
step: PropTypes.string,
134151
tabIndex: propTypes.string,
135152
type: Input.propTypes.type,
136153
valid: sharedPropTypes.statusPropType,

0 commit comments

Comments
 (0)