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

Fix Input elements ignore variant prop #4142

Merged
merged 2 commits into from
Dec 11, 2019
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
4 changes: 2 additions & 2 deletions packages/ra-ui-materialui/src/form/FormTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class FormTab extends Component {
input={input}
record={record}
resource={resource}
variant={variant}
margin={margin}
variant={input.props.variant || variant}
margin={input.props.margin || margin}
/>
)
)}
Expand Down
79 changes: 79 additions & 0 deletions packages/ra-ui-materialui/src/form/FormTab.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { cleanup } from '@testing-library/react';
import React from 'react';
import expect from 'expect';
import { renderWithRedux } from 'ra-core';

import TabbedForm from './TabbedForm';
import FormTab from './FormTab';
import TextInput from '../input/TextInput';

describe('<FormTab label="foo" />', () => {
afterEach(cleanup);

it('should display <Toolbar />', () => {
const { queryByLabelText } = renderWithRedux(
<TabbedForm>
<FormTab label="foo">
<TextInput source="name" />
<TextInput source="city" />
</FormTab>
</TabbedForm>
);
expect(queryByLabelText('ra.action.save')).not.toBeNull();
});

it('should not alter default margin or variant', () => {
const { queryByLabelText } = renderWithRedux(
<TabbedForm>
<FormTab label="foo">
<TextInput source="name" />
</FormTab>
</TabbedForm>
);
const inputElement = queryByLabelText(
'resources.undefined.fields.name'
);
expect(inputElement.classList).toContain('MuiFilledInput-input');
expect(inputElement.parentElement.parentElement.classList).toContain(
'MuiFormControl-marginDense'
);
});

it('should pass variant and margin to child inputs', () => {
const { queryByLabelText } = renderWithRedux(
<TabbedForm>
<FormTab label="foo" variant="outlined" margin="normal">
<TextInput source="name" />
</FormTab>
</TabbedForm>
);
const inputElement = queryByLabelText(
'resources.undefined.fields.name'
);
expect(inputElement.classList).toContain('MuiOutlinedInput-input');
expect(inputElement.parentElement.parentElement.classList).toContain(
'MuiFormControl-marginNormal'
);
});

it('should allow input children to override variant and margin', () => {
const { queryByLabelText } = renderWithRedux(
<TabbedForm>
<FormTab label="foo" variant="standard" margin="none">
<TextInput
source="name"
variant="outlined"
margin="normal"
/>
</FormTab>
</TabbedForm>
);
const inputElement = queryByLabelText(
'resources.undefined.fields.name'
);
expect(inputElement.classList).toContain('MuiOutlinedInput-input');
expect(inputElement.parentElement.parentElement.classList).toContain(
'MuiFormControl-marginNormal'
);
});
});
24 changes: 14 additions & 10 deletions packages/ra-ui-materialui/src/form/SimpleForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,20 @@ const SimpleFormView = ({
{...sanitizeRestProps(rest)}
>
<CardContentInner key={version}>
{Children.map(children, input => (
<FormInput
basePath={basePath}
input={input}
record={record}
resource={resource}
variant={variant}
margin={margin}
/>
))}
{Children.map(
children,
input =>
input && (
<FormInput
basePath={basePath}
input={input}
record={record}
resource={resource}
variant={input.props.variant || variant}
margin={input.props.margin || margin}
/>
)
)}
</CardContentInner>
{toolbar &&
React.cloneElement(toolbar, {
Expand Down
46 changes: 46 additions & 0 deletions packages/ra-ui-materialui/src/form/SimpleForm.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cleanup } from '@testing-library/react';
import React from 'react';
import expect from 'expect';
import { renderWithRedux } from 'ra-core';

import SimpleForm from './SimpleForm';
Expand Down Expand Up @@ -59,4 +60,49 @@ describe('<SimpleForm />', () => {

expect(queryByText('submitOnEnter: true')).not.toBeNull();
});

it('should not alter default margin or variant', () => {
const { queryByLabelText } = renderWithRedux(
<SimpleForm>
<TextInput source="name" />
</SimpleForm>
);
const inputElement = queryByLabelText(
'resources.undefined.fields.name'
);
expect(inputElement.classList).toContain('MuiFilledInput-input');
expect(inputElement.parentElement.parentElement.classList).toContain(
'MuiFormControl-marginDense'
);
});

it('should pass variant and margin to child inputs', () => {
const { queryByLabelText } = renderWithRedux(
<SimpleForm variant="outlined" margin="normal">
<TextInput source="name" />
</SimpleForm>
);
const inputElement = queryByLabelText(
'resources.undefined.fields.name'
);
expect(inputElement.classList).toContain('MuiOutlinedInput-input');
expect(inputElement.parentElement.parentElement.classList).toContain(
'MuiFormControl-marginNormal'
);
});

it('should allow input children to override variant and margin', () => {
const { queryByLabelText } = renderWithRedux(
<SimpleForm variant="standard" margin="none">
<TextInput source="name" variant="outlined" margin="normal" />
</SimpleForm>
);
const inputElement = queryByLabelText(
'resources.undefined.fields.name'
);
expect(inputElement.classList).toContain('MuiOutlinedInput-input');
expect(inputElement.parentElement.parentElement.classList).toContain(
'MuiFormControl-marginNormal'
);
});
});
6 changes: 4 additions & 2 deletions packages/ra-ui-materialui/src/form/TabbedForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ export const TabbedFormView = ({
record,
basePath,
hidden: !routeProps.match,
variant,
margin,
variant:
tab.props.variant || variant,
margin:
tab.props.margin || margin,
})
: null
}
Expand Down