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

[NumberInput][base-ui] Warn when changing control mode with useControlled #38757

Merged
merged 6 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,42 @@ describe('useNumberInput', () => {
expect(handleChange.args[0][1]).to.equal(undefined);
});
});

describe('warnings', () => {
it('should warn when switching from uncontrolled to controlled', () => {
const handleChange = spy();
function NumberInput({ value }: { value?: number }) {
const { getInputProps } = useNumberInput({
onChange: handleChange,
value,
});

return <input {...getInputProps()} />;
}
const { setProps } = render(<NumberInput />);
expect(() => {
setProps({ value: 5 });
}).to.toErrorDev(
'MUI: A component is changing the uncontrolled value state of NumberInput to be controlled',
);
});

it('should warn when switching from controlled to uncontrolled', () => {
const handleChange = spy();
function NumberInput({ value }: { value?: number }) {
const { getInputProps } = useNumberInput({
onChange: handleChange,
value,
});

return <input {...getInputProps()} />;
}
const { setProps } = render(<NumberInput value={5} />);
expect(() => {
setProps({ value: undefined });
}).to.toErrorDev(
'MUI: A component is changing the controlled value state of NumberInput to be uncontrolled',
);
});
});
});
13 changes: 11 additions & 2 deletions packages/mui-base/src/unstable_useNumberInput/useNumberInput.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use client';
import * as React from 'react';
import MuiError from '@mui/utils/macros/MuiError.macro';
import { unstable_useForkRef as useForkRef, unstable_useId as useId } from '@mui/utils';
import {
unstable_useForkRef as useForkRef,
unstable_useId as useId,
unstable_useControlled as useControlled,
} from '@mui/utils';
import { FormControlState, useFormControlContext } from '../FormControl';
import {
UseNumberInputParameters,
Expand Down Expand Up @@ -81,7 +85,12 @@ export function useNumberInput(parameters: UseNumberInputParameters): UseNumberI
const [focused, setFocused] = React.useState(false);

// the "final" value
const [value, setValue] = React.useState(valueProp ?? defaultValueProp);
const [value, setValue] = useControlled({
controlled: valueProp,
default: defaultValueProp,
name: 'NumberInput',
});

// the (potentially) dirty or invalid input value
const [dirtyValue, setDirtyValue] = React.useState<string | undefined>(
value ? String(value) : undefined,
Expand Down