Skip to content

Commit e76452f

Browse files
committed
Make <TabbedForm tabs> use the onChange prop
1 parent c3d60d4 commit e76452f

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

packages/ra-ui-materialui/src/form/TabbedForm.spec.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { AdminContext } from '../AdminContext';
2020
import { TabbedForm } from './TabbedForm';
2121
import { TabbedFormClasses } from './TabbedFormView';
2222
import { TextInput } from '../input';
23-
import { EncodedPaths } from './TabbedForm.stories';
23+
import { EncodedPaths, MultipleTabs } from './TabbedForm.stories';
24+
import { TabbedFormTabs } from './TabbedFormTabs';
2425

2526
describe('<TabbedForm />', () => {
2627
it('should display the tabs', () => {
@@ -313,4 +314,15 @@ describe('<TabbedForm />', () => {
313314
expect.stringContaining(`at ${TabbedForm.name}`)
314315
);
315316
});
317+
318+
it('should accept a "onChange" prop on the tabs', async () => {
319+
const onChange = jest.fn();
320+
render(<MultipleTabs tabs={<TabbedFormTabs onChange={onChange} />} />);
321+
322+
await screen.findByDisplayValue('War and Peace');
323+
const tabs = screen.getAllByRole('tab');
324+
fireEvent.click(tabs[1]);
325+
expect(onChange).toBeCalledTimes(1);
326+
expect(onChange).toBeCalledWith(expect.anything(), '1'); // tab index
327+
});
316328
});

packages/ra-ui-materialui/src/form/TabbedForm.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ export const Basic = () => (
7171
</Wrapper>
7272
);
7373

74-
export const MultipleTabs = () => (
74+
export const MultipleTabs = ({ tabs }) => (
7575
<Wrapper>
76-
<TabbedForm>
76+
<TabbedForm tabs={tabs}>
7777
<TabbedForm.Tab label="main">
7878
<TextInput source="title" />
7979
<TextInput source="author" />

packages/ra-ui-materialui/src/form/TabbedFormView.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export const TabbedFormView = (props: TabbedFormViewProps): ReactElement => {
3737
if (!syncWithLocation) {
3838
setTabValue(value);
3939
}
40+
if (tabs.props.onChange) {
41+
tabs.props.onChange(event, value);
42+
}
4043
};
4144

4245
const renderTabHeaders = () =>

0 commit comments

Comments
 (0)