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

Revert "Fix NumberInput not passing format and parse props to useInput call" #8442

Merged
merged 1 commit into from
Nov 29, 2022
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
28 changes: 1 addition & 27 deletions packages/ra-ui-materialui/src/input/NumberInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { AdminContext } from '../AdminContext';
import { SaveButton } from '../button';
import { SimpleForm, Toolbar } from '../form';
import { required } from 'ra-core';
import { Format, Parse } from './NumberInput.stories';

describe('<NumberInput />', () => {
const defaultProps = {
Expand Down Expand Up @@ -199,7 +198,7 @@ describe('<NumberInput />', () => {
});

describe('format and parse', () => {
it('should get the same value as injected value', async () => {
it('should get the same value as injected value ', async () => {
const onSubmit = jest.fn();

render(
Expand Down Expand Up @@ -291,31 +290,6 @@ describe('<NumberInput />', () => {
});
expect(onSubmit.mock.calls[0][0].views).toBeNull();
});

it('should use custom format function prop', async () => {
render(<Format />);

const input = screen.getByLabelText('resources.posts.fields.views');
fireEvent.change(input, { target: { value: 5.6356487 } });

await waitFor(() => {
expect((input as HTMLInputElement).value).toEqual('5.6356487');
expect(
(screen.getByDisplayValue('5.64') as HTMLInputElement).value
).toEqual('5.64');
});
});

it('should use custom parse function prop', async () => {
render(<Parse />);

const input = screen.getByLabelText('Parse to two decimal');
fireEvent.change(input, { target: { value: 5.6356487 } });

await waitFor(() => {
expect((input as HTMLInputElement).value).toEqual('5.64');
});
});
});

describe('onChange event', () => {
Expand Down
46 changes: 0 additions & 46 deletions packages/ra-ui-materialui/src/input/NumberInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,49 +313,3 @@ export const FieldState = () => (
</Create>
</AdminContext>
);

export const Format = () => (
<AdminContext>
<Create
resource="posts"
record={{ id: 123, views: 3.7594157 }}
sx={{ width: 600 }}
>
<SimpleForm>
<NumberInput source="views" />
<NumberInput
source="views"
label="Two decimals"
format={v =>
isNaN(v) ? 0 : Number(parseFloat(v).toFixed(2))
}
disabled
/>
<FormInspector name="views" />
</SimpleForm>
</Create>
</AdminContext>
);

export const Parse = () => (
<AdminContext>
<Create
resource="posts"
record={{ id: 123, views: 3.77 }}
sx={{ width: 600 }}
>
<SimpleForm>
<NumberInput
source="views"
label="Parse to two decimal"
format={v => (v ? String(v) : '0')}
parse={v => {
const float = Number(parseFloat(v).toFixed(2));
return isNaN(float) ? null : float;
}}
/>
<FormInspector name="views" />
</SimpleForm>
</Create>
</AdminContext>
);
4 changes: 1 addition & 3 deletions packages/ra-ui-materialui/src/input/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export const NumberInput = ({
isRequired,
} = useInput({
defaultValue,
format,
parse,
onBlur,
resource,
source,
Expand All @@ -60,7 +58,7 @@ export const NumberInput = ({

// This is a controlled input that renders directly the string typed by the user.
// This string is converted to a number on change, and stored in the form state,
// but that number is not displayed.
// but that number is not not displayed.
// This is to allow transitory values like '1.0' that will lead to '1.02'

// text typed by the user and displayed in the input, unparsed
Expand Down