Skip to content

Commit

Permalink
Merge pull request #8439 from kosmotema/input-label
Browse files Browse the repository at this point in the history
Fix bugs in SearchInput and TextField related to the `label` property
  • Loading branch information
slax57 authored Dec 8, 2022
2 parents 1d1b691 + 3e0554e commit 833f833
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 5 deletions.
30 changes: 30 additions & 0 deletions packages/ra-ui-materialui/src/input/SearchInput.spec.tsx
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();
});
});
6 changes: 4 additions & 2 deletions packages/ra-ui-materialui/src/input/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import { CommonInputProps } from './CommonInputProps';
import { TextInput, TextInputProps } from './TextInput';

export const SearchInput = (props: SearchInputProps) => {
const { label, ...rest } = props;

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."
);
Expand All @@ -30,7 +32,7 @@ export const SearchInput = (props: SearchInputProps) => {
),
}}
size="small"
{...props}
{...rest}
/>
);
};
Expand Down
62 changes: 62 additions & 0 deletions packages/ra-ui-materialui/src/input/TextInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`', () => {
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();
});
});
});
5 changes: 2 additions & 3 deletions packages/ra-ui-materialui/src/input/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,14 @@ export const TextInput = (props: TextInputProps) => {
{...field}
className={clsx('ra-input', `ra-input-${source}`, className)}
label={
label !== '' &&
label !== false && (
label !== '' && label !== false ? (
<FieldTitle
label={label}
source={source}
resource={resource}
isRequired={isRequired}
/>
)
) : null
}
error={(isTouched || isSubmitted) && invalid}
helperText={
Expand Down

0 comments on commit 833f833

Please sign in to comment.