Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Number Type Textfied error #44963 #44967

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading