-
Notifications
You must be signed in to change notification settings - Fork 18
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
* inventoryTabs, class to function, hooks * productView, productId from hook
- Loading branch information
Showing
9 changed files
with
156 additions
and
101 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
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
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,105 +1,123 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Title } from '@patternfly/react-core'; | ||
import { connect, reduxTypes, store } from '../../redux'; | ||
import { reduxTypes, storeHooks } from '../../redux'; | ||
import { useProduct } from '../productView/productViewContext'; | ||
import { Tabs } from '../tabs/tabs'; | ||
import { helpers } from '../../common'; | ||
import { translate } from '../i18n/i18n'; | ||
import { InventoryTab } from './inventoryTab'; | ||
|
||
/** | ||
* A system inventory tabs component. | ||
* Update tab state. | ||
* | ||
* @augments React.Component | ||
* @fires onTab | ||
* @param {object} options | ||
* @param {Function} options.useDispatch | ||
* @param {Function} options.useProduct | ||
* @returns {Function} | ||
*/ | ||
class InventoryTabs extends React.Component { | ||
/** | ||
* On tab update state. | ||
* | ||
* @event onTab | ||
* @param {object} params | ||
* @param {string} params.index tab index | ||
*/ | ||
onTab = ({ index }) => { | ||
const { productId } = this.props; | ||
const useOnTab = ({ | ||
useDispatch: useAliasDispatch = storeHooks.reactRedux.useDispatch, | ||
useProduct: useAliasProduct = useProduct | ||
} = {}) => { | ||
const { productId } = useAliasProduct(); | ||
const dispatch = useAliasDispatch(); | ||
|
||
store.dispatch({ | ||
return ({ index } = {}) => { | ||
dispatch({ | ||
type: reduxTypes.inventory.SET_INVENTORY_TAB, | ||
tabs: { | ||
[productId]: index | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
/** | ||
* Render inventory tabs using Inventory tab passed props only. A parallel outcome can be | ||
* achieved by passing an array of objects through a prop. | ||
* | ||
* @returns {Node} | ||
*/ | ||
render() { | ||
const { activeTab, children, defaultActiveTab, isDisabled, t } = this.props; | ||
/** | ||
* An inventory tabs component. | ||
* Render inventory tabs using Inventory tab passed props only. | ||
* | ||
* @fires onTab | ||
* @param {object} props | ||
* @param {number} props.activeTab | ||
* @param {React.ReactNode} props.children | ||
* @param {number} props.defaultActiveTab | ||
* @param {boolean} props.isDisabled | ||
* @param {Function} props.t | ||
* @param {Function} props.useOnTab | ||
* @param {Function} props.useProduct | ||
* @param {Function} props.useSelector | ||
* @returns {React.ReactNode|null} | ||
*/ | ||
const InventoryTabs = ({ | ||
activeTab, | ||
children, | ||
defaultActiveTab, | ||
isDisabled, | ||
t, | ||
useOnTab: useAliasOnTab, | ||
useProduct: useAliasProduct, | ||
useSelector: useAliasSelector | ||
}) => { | ||
const { productId } = useAliasProduct(); | ||
const updatedActiveTab = useAliasSelector(({ inventory }) => inventory.tabs?.[productId], activeTab); | ||
const onTab = useAliasOnTab(); | ||
|
||
if (isDisabled) { | ||
return null; | ||
} | ||
if (isDisabled) { | ||
return null; | ||
} | ||
|
||
const updatedChildren = React.Children.toArray(children).map((child, index) => { | ||
const { props = {} } = child; | ||
const updatedChildren = React.Children.toArray(children).map((child, index) => { | ||
const { props: childProps = {} } = child; | ||
|
||
return { | ||
active: props.active || false, | ||
content: props.children || child, | ||
title: props.title || t('curiosity-inventory.tabSubHeading', { count: index }) | ||
}; | ||
}); | ||
return { | ||
active: childProps.active || false, | ||
content: childProps.children || child, | ||
title: childProps.title || t('curiosity-inventory.tabSubHeading', { count: index }) | ||
}; | ||
}); | ||
|
||
return ( | ||
<React.Fragment> | ||
<Title headingLevel="h2" className="sr-only"> | ||
{t('curiosity-inventory.tabHeading', { count: updatedChildren.length })} | ||
</Title> | ||
<Tabs activeTab={activeTab} defaultActiveTab={defaultActiveTab} onTab={this.onTab} tabs={updatedChildren} /> | ||
</React.Fragment> | ||
); | ||
} | ||
} | ||
return ( | ||
<React.Fragment> | ||
<Title headingLevel="h2" className="sr-only"> | ||
{t('curiosity-inventory.tabHeading', { count: updatedChildren.length })} | ||
</Title> | ||
<Tabs activeTab={updatedActiveTab} defaultActiveTab={defaultActiveTab} onTab={onTab} tabs={updatedChildren} /> | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
/** | ||
* Prop types. | ||
* | ||
* @type {{productId: string, t: Function, children: Node, defaultActiveTab: number, isDisabled: boolean, | ||
* activeTab: number}} | ||
* @type {{useOnTab: Function, useProduct: Function, t: Function, children: React.ReactNode, | ||
* useSelector: Function, defaultActiveTab: number, isDisabled: boolean, activeTab: number}} | ||
*/ | ||
InventoryTabs.propTypes = { | ||
activeTab: PropTypes.number, | ||
children: PropTypes.node.isRequired, | ||
defaultActiveTab: PropTypes.number, | ||
isDisabled: PropTypes.bool, | ||
productId: PropTypes.string.isRequired, | ||
t: PropTypes.func | ||
t: PropTypes.func, | ||
useOnTab: PropTypes.func, | ||
useProduct: PropTypes.func, | ||
useSelector: PropTypes.func | ||
}; | ||
|
||
/** | ||
* Default props. | ||
* | ||
* @type {{t: translate, defaultActiveTab: number, isDisabled: boolean, activeTab: number}} | ||
* @type {{useOnTab: Function, useProduct: Function, t: translate, useSelector: Function, | ||
* defaultActiveTab: number, isDisabled: boolean, activeTab: number}} | ||
*/ | ||
InventoryTabs.defaultProps = { | ||
activeTab: 0, | ||
defaultActiveTab: 0, | ||
isDisabled: helpers.UI_DISABLED_TABLE, | ||
t: translate | ||
t: translate, | ||
useOnTab, | ||
useProduct, | ||
useSelector: storeHooks.reactRedux.useSelector | ||
}; | ||
|
||
/** | ||
* Create a selector from applied state, props. | ||
* | ||
* @type {Function} | ||
*/ | ||
const mapStateToProps = ({ inventory }, { productId }) => ({ activeTab: inventory.tabs?.[productId] }); | ||
|
||
const ConnectedInventoryTabs = connect(mapStateToProps)(InventoryTabs); | ||
|
||
export { ConnectedInventoryTabs as default, ConnectedInventoryTabs, InventoryTabs, InventoryTab }; | ||
export { InventoryTabs as default, InventoryTabs, InventoryTab, useOnTab }; |
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
Oops, something went wrong.