Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/frontend/app/events/home.jsx

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting and storing state variable for clicked tab in home.jsx so that it doesn't reset to first one, as we are rendering either the EventForm or the Tabs, and when one of them is selected to be rendered, the other one is unmounted so state of clicked tab is not saved anywhere.

Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default function Home() {
// States
const [ map, setMap ] = useState(null);
const [ preview, setPreview ] = useState(true);
const [ eventsListTab, setEventsListTab ] = useState('active');
const [ event, dispatch ] = useReducer(reducer, getInitialEvent());

// Selectors
Expand Down Expand Up @@ -140,7 +141,7 @@ export default function Home() {
setAlertContext={setAlertContext} />
: <>
<h3>Events</h3>
<Tabs>
<Tabs value={eventsListTab} onChange={setEventsListTab}>
<Tabs.Tab name='active' label='Active'>
<Events dispatch={dispatch} goToFunc={centerMap} map={mapRef.current} current={event} />
</Tabs.Tab>
Expand Down
7 changes: 4 additions & 3 deletions src/frontend/app/shared/Tabs.jsx

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keeping all the same inside tabs, but setting the currentTab with value passed on from parent if any (from Home in this case to Tabs).

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const getDefault = (children) => {

export const TabContext = createContext('');

export default function Tabs({ children, onChange, hideSingleTabHandle }) {
const [currentTab, setCurrentTab] = useState(getDefault(children));
export default function Tabs({ children, value, onChange, hideSingleTabHandle }) {
const [internalTab, setInternalTab] = useState(() => getDefault(children));
const currentTab = value ?? internalTab;

let tabs = Array.isArray(children) ? children : [children];
tabs = tabs.filter((child) => child);
Expand All @@ -40,7 +41,7 @@ export default function Tabs({ children, onChange, hideSingleTabHandle }) {
onClick={(e) => {
e.stopPropagation();
if (currentTab !== name) {
setCurrentTab(name);
setInternalTab(name);
onChange?.(name);
}
}}
Expand Down