-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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 <TextInput>
missing placeholder with MUI v5.4
#8439
Changes from all commits
cbb47c6
ae9418b
9b7670a
41c54b0
11036ef
a30786c
3e0554e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { testDataProvider } from 'ra-core'; | ||
|
||
import { AdminContext } from '../AdminContext'; | ||
import { SearchInput } from '.'; | ||
import { FilterForm } from '../list'; | ||
|
||
describe('<SearchInput />', () => { | ||
it('should not render label if passed explicit `undefined` value', async () => { | ||
const source = 'test'; | ||
|
||
const filters = [<SearchInput source={source} label={undefined} />]; | ||
const displayedFilters = { | ||
[source]: true, | ||
}; | ||
|
||
const { container } = render( | ||
<AdminContext dataProvider={testDataProvider()}> | ||
<FilterForm | ||
setFilters={jest.fn()} | ||
filters={filters} | ||
displayedFilters={displayedFilters} | ||
/> | ||
</AdminContext> | ||
); | ||
|
||
expect(container.querySelector(`label`)).toBeNull(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,11 @@ import { CommonInputProps } from './CommonInputProps'; | |
import { TextInput, TextInputProps } from './TextInput'; | ||
|
||
export const SearchInput = (props: SearchInputProps) => { | ||
const { label, ...rest } = props; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why you made this change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To handle the case when specifying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also noticed, that it's possible to use SearchInput with <SearchInput source="q" alwaysOn label={0} /> I think, it would be better to revert all changes in this PR, that related to the SearchInput's label overwriting logic and open a new issue to discuss, what and how to do about this case (IMHO it's better to prevent users from providing a |
||
|
||
const translate = useTranslate(); | ||
|
||
if (props.label) { | ||
if (label) { | ||
throw new Error( | ||
"<SearchInput> isn't designed to be used with a label prop. Use <TextInput> if you need a label." | ||
); | ||
|
@@ -30,7 +32,7 @@ export const SearchInput = (props: SearchInputProps) => { | |
), | ||
}} | ||
size="small" | ||
{...props} | ||
{...rest} | ||
/> | ||
); | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,4 +166,66 @@ describe('<TextInput />', () => { | |
expect(input.value).toEqual('bar'); | ||
}); | ||
}); | ||
|
||
describe('label', () => { | ||
it('should render label when `label` prop not specified', () => { | ||
const { container } = render( | ||
<AdminContext dataProvider={testDataProvider()}> | ||
<SimpleForm | ||
defaultValues={{ title: 'hello' }} | ||
onSubmit={jest.fn} | ||
> | ||
<TextInput {...defaultProps} /> | ||
</SimpleForm> | ||
</AdminContext> | ||
); | ||
|
||
expect(container.querySelector(`label`)).not.toBeNull(); | ||
}); | ||
|
||
it('should render label when `label` prop is non-empty string', () => { | ||
const { container } = render( | ||
<AdminContext dataProvider={testDataProvider()}> | ||
<SimpleForm | ||
defaultValues={{ title: 'hello' }} | ||
onSubmit={jest.fn} | ||
> | ||
<TextInput {...defaultProps} label="label" /> | ||
</SimpleForm> | ||
</AdminContext> | ||
); | ||
|
||
expect(container.querySelector(`label`)).not.toBeNull(); | ||
}); | ||
|
||
it('should not render label when `label` prop is `false`', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're missing a test showing that a proper label prop renders a label. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added two more tests: when |
||
const { container } = render( | ||
<AdminContext dataProvider={testDataProvider()}> | ||
<SimpleForm | ||
defaultValues={{ title: 'hello' }} | ||
onSubmit={jest.fn} | ||
> | ||
<TextInput {...defaultProps} label={false} /> | ||
</SimpleForm> | ||
</AdminContext> | ||
); | ||
|
||
expect(container.querySelector(`label`)).toBeNull(); | ||
}); | ||
|
||
it('should not render label when `label` prop is empty string', () => { | ||
const { container } = render( | ||
<AdminContext dataProvider={testDataProvider()}> | ||
<SimpleForm | ||
defaultValues={{ title: 'hello' }} | ||
onSubmit={jest.fn} | ||
> | ||
<TextInput {...defaultProps} label="" /> | ||
</SimpleForm> | ||
</AdminContext> | ||
); | ||
|
||
expect(container.querySelector(`label`)).toBeNull(); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added to cover the case of passing explicit
undefined
for the SearchInput's label property (more info here)