Skip to content

Commit 1b78794

Browse files
mgueyraudsstrubbergtw15egankodiakhq[bot]
authored
feat(number input): added a prop to disable the wheel functionality (#12358)
* feat(number input): added a prop to disable the wheel functionality * fix(test snapshot): fixed test snapshot in public api test * feat(number input): changed approach (manually disabling it) * chore(contributors): add to list of contributors Co-authored-by: Scott Strubberg <sstrubberg@protonmail.com> Co-authored-by: TJ Egan <tw15egan@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent ad3b045 commit 1b78794

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,15 @@
931931
"contributions": [
932932
"code"
933933
]
934+
},
935+
{
936+
"login": "mgueyraud",
937+
"name": "Mario Gueyraud",
938+
"avatar_url": "https://avatars.githubusercontent.com/u/9916318?v=4",
939+
"profile": "https://github.com/mgueyraud",
940+
"contributions": [
941+
"code"
942+
]
934943
}
935944
],
936945
"commitConvention": "none"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
205205
<td align="center"><a href="http://torresga.github.io/"><img src="https://avatars.githubusercontent.com/u/6892410?v=4?s=100" width="100px;" alt=""/><br /><sub><b>G. Torres</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=torresga" title="Code">💻</a></td>
206206
<td align="center"><a href="https://github.com/FionaDL"><img src="https://avatars.githubusercontent.com/u/28625558?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Fiona</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=FionaDL" title="Code">💻</a></td>
207207
<td align="center"><a href="https://lewisdavanzo.com/"><img src="https://avatars.githubusercontent.com/u/70274722?v=4?s=100" width="100px;" alt=""/><br /><sub><b>kindoflew</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=kindoflew" title="Code">💻</a></td>
208+
<td align="center"><a href="https://github.com/mgueyraud"><img src="https://avatars.githubusercontent.com/u/9916318?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mario Gueyraud</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=mgueyraud" title="Code">💻</a></td>
208209
</tr>
209210
</table>
210211

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4969,6 +4969,9 @@ Map {
49694969
],
49704970
"type": "oneOfType",
49714971
},
4972+
"disableWheel": Object {
4973+
"type": "bool",
4974+
},
49724975
"disabled": Object {
49734976
"type": "bool",
49744977
},

packages/react/src/components/NumberInput/NumberInput.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const NumberInput = React.forwardRef(function NumberInput(props, forwardRef) {
3131
allowEmpty = false,
3232
className: customClassName,
3333
disabled = false,
34+
disableWheel: disableWheelProp = false,
3435
defaultValue,
3536
helperText = '',
3637
hideLabel = false,
@@ -163,6 +164,24 @@ const NumberInput = React.forwardRef(function NumberInput(props, forwardRef) {
163164
onClick={onClick}
164165
onChange={handleOnChange}
165166
onKeyUp={onKeyUp}
167+
onFocus={(e) => {
168+
if (disableWheelProp) {
169+
e.target.addEventListener('wheel', disableWheel);
170+
}
171+
172+
if (rest.onFocus) {
173+
rest.onFocus(e);
174+
}
175+
}}
176+
onBlur={(e) => {
177+
if (disableWheelProp) {
178+
e.target.removeEventListener('wheel', disableWheel);
179+
}
180+
181+
if (rest.onBlur) {
182+
rest.onBlur(e);
183+
}
184+
}}
166185
pattern="[0-9]*"
167186
readOnly={readOnly}
168187
step={step}
@@ -253,6 +272,11 @@ NumberInput.propTypes = {
253272
*/
254273
defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
255274

275+
/**
276+
* Specify if the wheel functionality for the input should be disabled, or not
277+
*/
278+
disableWheel: PropTypes.bool,
279+
256280
/**
257281
* Specify if the control should be disabled, or not
258282
*/
@@ -442,6 +466,24 @@ function getInputValidity({ allowEmpty, invalid, value, max, min }) {
442466
return true;
443467
}
444468

469+
/**
470+
* It prevents any wheel event from emitting.
471+
*
472+
* We want to prevent this input field from changing by the user accidentally
473+
* when the user scrolling up or down in a long form. So we prevent the default
474+
* behavior of wheel events when it is focused.
475+
*
476+
* Because React uses passive event handler by default, we can't just call
477+
* `preventDefault` in the `onWheel` event handler. So we have to get the input
478+
* ref and add our event handler manually.
479+
*
480+
* @see https://github.com/facebook/react/pull/19654
481+
* @param {WheelEvent} e A wheel event.
482+
*/
483+
function disableWheel(e) {
484+
e.preventDefault();
485+
}
486+
445487
/**
446488
* Clamp the given value between the upper bound `max` and the lower bound `min`
447489
* @param {number} max

0 commit comments

Comments
 (0)