-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f493067
commit dda6b66
Showing
9 changed files
with
218 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const checkForFormChanges = () => { | ||
let type = 'old'; // 'old' | 'angular' | 'tagging' | 'react' | ||
let dirty = false; | ||
const ignore = !!document.getElementById('ignore_form_changes'); | ||
|
||
if (ManageIQ.redux.store.getState().FormButtons && ManageIQ.redux.store.getState().FormButtons.in_a_form) { | ||
type = 'react'; | ||
} | ||
|
||
switch (type) { | ||
case 'react': | ||
dirty = !ManageIQ.redux.store.getState().FormButtons.pristine; | ||
break; | ||
|
||
default: | ||
dirty = $('#buttons_on').is(':visible'); | ||
break; | ||
} | ||
|
||
if (dirty && !ignore) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}; |
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,105 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Tab, Tabs, Modal } from 'carbon-components-react'; | ||
import { checkForFormChanges } from './helper'; | ||
|
||
const CustomURLTabs = ({ | ||
tabs, path, currentTab, checkForChanges, | ||
}) => { | ||
const [{ selectedTab, showConfirm, url }, setState] = useState({ selectedTab: 0, showConfirm: false }); | ||
const activeTabClassName = 'bx--tabs--scrollable__nav-item--selected'; | ||
|
||
const onTabClick = (id) => { | ||
if (currentTab !== id) { | ||
if (checkForChanges && checkForFormChanges()) { | ||
setState((state) => ({ | ||
...state, | ||
showConfirm: true, | ||
url: `${path}${id}?uib-tab=${id}`, | ||
})); | ||
document.getElementById(id).parentElement.classList.add(activeTabClassName); | ||
} else { | ||
window.location.replace(`${path}${id}?uib-tab=${id}`); | ||
} | ||
} | ||
}; | ||
|
||
const fixTabStyling = () => { | ||
const selectedTabs = []; | ||
document.getElementsByClassName(activeTabClassName).forEach((element) => { | ||
selectedTabs.push(element); | ||
}); | ||
selectedTabs.forEach((tab) => { | ||
tab.classList.remove(activeTabClassName); | ||
}); | ||
document.getElementsByClassName('selected')[0].classList.add(activeTabClassName); | ||
}; | ||
|
||
useEffect(() => { | ||
const currentTabIndex = tabs.findIndex((tab) => tab[0] === currentTab); | ||
if (currentTabIndex) { | ||
setState((state) => ({ | ||
...state, | ||
selectedTab: currentTabIndex, | ||
})); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (showConfirm) { | ||
document.getElementsByClassName('selected')[0].classList.remove(activeTabClassName); | ||
} else if (document.getElementsByClassName('selected').length > 0) { | ||
fixTabStyling(); | ||
} | ||
}, [showConfirm]); | ||
|
||
return ( | ||
<div className='custom-url-tabs'> | ||
<Modal | ||
open={showConfirm} | ||
primaryButtonText={__('OK')} | ||
secondaryButtonText={__('Cancel')} | ||
onRequestClose={() => { setState((state) => ({ | ||
...state, | ||
showConfirm: false, | ||
})); | ||
}} | ||
onRequestSubmit={() => window.location.replace(url)} | ||
onSecondarySubmit={() => { | ||
setState((state) => ({ | ||
...state, | ||
showConfirm: false, | ||
})); | ||
fixTabStyling(); | ||
}} | ||
> | ||
{__('Abandon changes?')} | ||
</Modal> | ||
<Tabs selected={selectedTab}> | ||
{tabs.map((tab) => ( | ||
<Tab | ||
key={tab[0]} | ||
id={tab[0]} | ||
className={`${tab[0]}` === currentTab ? 'selected' : 'not-selected'} | ||
label={__(tab[1])} | ||
onClick={() => onTabClick(tab[0])} | ||
/> | ||
))} | ||
</Tabs> | ||
</div> | ||
); | ||
}; | ||
|
||
CustomURLTabs.propTypes = { | ||
tabs: PropTypes.arrayOf(String).isRequired, | ||
path: PropTypes.string.isRequired, | ||
currentTab: PropTypes.string, | ||
checkForChanges: PropTypes.bool, | ||
}; | ||
|
||
CustomURLTabs.defaultProps = { | ||
currentTab: '0', | ||
checkForChanges: false, | ||
}; | ||
|
||
export default CustomURLTabs; |
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
14 changes: 13 additions & 1 deletion
14
app/javascript/spec/visual-settings-form/__snapshots__/visual-settings-form.spec.js.snap
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 |
---|---|---|
@@ -1,3 +1,15 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`visual settings form matches the snapshot 1`] = `""`; | ||
exports[`visual settings form matches the snapshot 1`] = ` | ||
<div | ||
className="loadSettings" | ||
> | ||
<Loading | ||
active={true} | ||
className="loading" | ||
description="Active loading indicator" | ||
small={true} | ||
withOverlay={false} | ||
/> | ||
</div> | ||
`; |
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 |
---|---|---|
|
@@ -417,3 +417,8 @@ | |
flex-grow: 1; | ||
} | ||
} | ||
|
||
.loadSettings { | ||
margin-top: 3rem; | ||
margin-left: 15rem; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -632,6 +632,7 @@ | |
], | ||
:post => %w[ | ||
button | ||
change_tab | ||
filters_field_changed | ||
theme_changed | ||
timeprofile_delete | ||
|