Skip to content

Commit

Permalink
Merge pull request #10549 from marmelab/fix/tabbedform_tabs_should_ac…
Browse files Browse the repository at this point in the history
…cept_onChange

Make `<TabbedForm tabs>` use the `onChange` prop
  • Loading branch information
djhi authored Feb 26, 2025
2 parents 0fcfb7c + e76452f commit 67f534b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/ra-ui-materialui/src/form/TabbedForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { AdminContext } from '../AdminContext';
import { TabbedForm } from './TabbedForm';
import { TabbedFormClasses } from './TabbedFormView';
import { TextInput } from '../input';
import { EncodedPaths } from './TabbedForm.stories';
import { EncodedPaths, MultipleTabs } from './TabbedForm.stories';
import { TabbedFormTabs } from './TabbedFormTabs';

describe('<TabbedForm />', () => {
it('should display the tabs', () => {
Expand Down Expand Up @@ -313,4 +314,15 @@ describe('<TabbedForm />', () => {
expect.stringContaining(`at ${TabbedForm.name}`)
);
});

it('should accept a "onChange" prop on the tabs', async () => {
const onChange = jest.fn();
render(<MultipleTabs tabs={<TabbedFormTabs onChange={onChange} />} />);

await screen.findByDisplayValue('War and Peace');
const tabs = screen.getAllByRole('tab');
fireEvent.click(tabs[1]);
expect(onChange).toBeCalledTimes(1);
expect(onChange).toBeCalledWith(expect.anything(), '1'); // tab index
});
});
4 changes: 2 additions & 2 deletions packages/ra-ui-materialui/src/form/TabbedForm.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ export const Basic = () => (
</Wrapper>
);

export const MultipleTabs = () => (
export const MultipleTabs = ({ tabs }) => (
<Wrapper>
<TabbedForm>
<TabbedForm tabs={tabs}>
<TabbedForm.Tab label="main">
<TextInput source="title" />
<TextInput source="author" />
Expand Down
3 changes: 3 additions & 0 deletions packages/ra-ui-materialui/src/form/TabbedFormView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const TabbedFormView = (props: TabbedFormViewProps): ReactElement => {
if (!syncWithLocation) {
setTabValue(value);
}
if (tabs.props.onChange) {
tabs.props.onChange(event, value);
}
};

const renderTabHeaders = () =>
Expand Down

0 comments on commit 67f534b

Please sign in to comment.