forked from geosolutions-it/geonode-mapstore-client
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
139 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
geonode_mapstore_client/client/js/components/Tabs/Tabs.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2023, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import React from 'react'; | ||
import PropTypes from "prop-types"; | ||
import isNil from 'lodash/isNil'; | ||
import { Tabs as RTabs, Tab } from 'react-bootstrap'; | ||
import useLocalStorage from '@js/hooks/useLocalStorage'; | ||
|
||
const Tabs = ({ | ||
tabs = [], | ||
identifier, | ||
selectedTabId, | ||
onSelect, | ||
className | ||
}) => { | ||
const [eventKeys, setEventKeys] = useLocalStorage('tabSelected', {}); | ||
const persistSelection = isNil(selectedTabId); | ||
const selectedKey = !persistSelection ? selectedTabId : (eventKeys[identifier] ?? 0); | ||
|
||
const onSelectTab = (key) => { | ||
const updatedEventKeys = { | ||
...eventKeys, | ||
[identifier]: key | ||
}; | ||
setEventKeys(updatedEventKeys); | ||
}; | ||
return ( | ||
<RTabs | ||
bsStyle="pills" | ||
className={className} | ||
key={identifier} | ||
defaultActiveKey={selectedKey} | ||
onSelect={onSelect ? onSelect : onSelectTab} | ||
> | ||
{tabs.map((tab, index)=> { | ||
const eventKey = !isNil(tab.eventKey) ? tab.eventKey : index; | ||
const component = (!onSelect || selectedKey === eventKey) ? tab.component : null; | ||
return ( | ||
<Tab key={`tab-${index}`} eventKey={eventKey} title={tab.title}> | ||
{component} | ||
</Tab>); | ||
})} | ||
</RTabs> | ||
); | ||
}; | ||
|
||
Tabs.propTypes = { | ||
className: PropTypes.string, | ||
tabs: PropTypes.arrayOf(PropTypes.shape({ | ||
title: PropTypes.oneOfType([PropTypes.node, PropTypes.string]), | ||
component: PropTypes.node, | ||
eventKey: PropTypes.string | ||
})), | ||
identifier: PropTypes.string, | ||
selectedTabId: PropTypes.string, | ||
onSelect: PropTypes.func | ||
}; | ||
|
||
Tabs.defaultProps = { | ||
tabs: [], | ||
className: "gn-tabs tabs-underline" | ||
}; | ||
|
||
export default Tabs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright 2023, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
export { default } from '@js/components/Tabs/Tabs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters