Skip to content

Commit

Permalink
[TextField] Fix number input continuous increment after mouse release (
Browse files Browse the repository at this point in the history
  • Loading branch information
yashdev16 committed Jan 7, 2025
1 parent e78d580 commit c424c72
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions packages/mui-material/src/TextField/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import FormHelperText from '../FormHelperText';
import Select from '../Select';
import { getTextFieldUtilityClass } from './textFieldClasses';
import useSlot from '../utils/useSlot';
import { unstable_mergeRefs as mergeRefs } from '@mui/utils';

const variantComponent = {
standard: Input,
Expand Down Expand Up @@ -199,6 +200,31 @@ const TextField = React.forwardRef(function TextField(inProps, ref) {
ownerState,
});

// Add inputRef to track our specific input element
const numberInputRef = React.useRef(null);

React.useEffect(() => {
if (type === 'number') {
const cleanup = () => {
// Only blur if it's our specific number input that's active
if (document.activeElement === numberInputRef.current) {
numberInputRef.current.blur();
}
};

window.addEventListener('mouseup', cleanup);
return () => {
window.removeEventListener('mouseup', cleanup);
};
}
}, [type]);

// Merge refs to maintain both our internal ref and any provided inputRef
const handleRef = React.useMemo(
() => mergeRefs([numberInputRef, inputRef]),
[inputRef]
);

const InputElement = (
<InputSlot
aria-describedby={helperTextId}
Expand All @@ -214,7 +240,7 @@ const TextField = React.forwardRef(function TextField(inProps, ref) {
type={type}
value={value}
id={id}
inputRef={inputRef}
inputRef={type === 'number' ? handleRef : inputRef}
onBlur={onBlur}
onChange={onChange}
onFocus={onFocus}
Expand Down Expand Up @@ -482,4 +508,4 @@ TextField.propTypes /* remove-proptypes */ = {
variant: PropTypes.oneOf(['filled', 'outlined', 'standard']),
};

export default TextField;
export default TextField;

0 comments on commit c424c72

Please sign in to comment.