How can I set the 'selected' Tab of the TabStrip to the first tab in Form onSubmit?
i.e. I need to call tabStrip.setSelected(0), but in React methodology I'm not sure how to get a reference to the tabStrip?
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Field, Form, FormElement } from '@progress/kendo-react-form';
import { Button } from '@progress/kendo-react-buttons';
import {
TabStrip,
TabStripSelectEventArguments,
TabStripTab,
} from '@progress/kendo-react-layout';
const App = () => {
const [selected, setSelected] = React.useState<number>(2);
const handleSelect = (e: TabStripSelectEventArguments) => {
setSelected(e.selected);
};
const handleSubmit = (dataItem) => {
alert("Submit OK");
};
return (
<Form
onSubmit={handleSubmit}
render={(formRenderProps) => (
<div>
<TabStrip selected={selected} onSelect={handleSelect}>
<TabStripTab title="Tab 1 Title">
<p>Tab 1 Content</p>
</TabStripTab>
<TabStripTab title="Tab 2 Title">
<p>Tab 2 Content</p>
</TabStripTab>
<TabStripTab title="Tab 3 Title">
<p>Tab 3 Content</p>
</TabStripTab>
</TabStrip>
<span style={{ padding: 10 }}>
<Button type={'submit'} icon="save" onClick={handleSubmit}>
Save
</Button>
</span>
</div>
)}
></Form>
);
};
ReactDOM.render(<App />, document.querySelector('my-app'));
Working example here: https://stackblitz.com/edit/react-mamtrq-6oshyg?file=app%2Fmain.tsx
The
Formcomponent from@progress/kendo-react-form(seen here)is handling the form submission in a particular way, and directly calling thesubmitmethod on the form element is not triggering theonSubmitprop.If the underlying goal is to set the selected tab to the first one on form submission, we can achieve the desired behavior without the need to interact with the
TabStripcomponent directly. We can control the selected tab by managing the state of theselectedvariable.If a direct reference to the
TabStripis required for other purposes, you would typically use theuseRefhook and therefattribute. However, this is generally not needed when you can control the component through props and state, as we can do here.We are directly calling the
handleSubmitfunction from thehandleButtonClickfunction, bypassing the form submission process. That approach make sures that the selected tab is set to the first one when the "Save" button is clicked.stackblitz demo.
If you want a fully native
@progress/kendo-react-formoption:stackblitz demo 2.