-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change(tabs): use default edit for accordion tab edit
- Within AccordionEdit inherit the default Edit adding only extra options classes coming from the form data - modified Default tabs text to be inside a span within the tab link instead of a p tag.
- Loading branch information
1 parent
ceb8ab7
commit 7ed5f4e
Showing
7 changed files
with
33 additions
and
215 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,214 +1,25 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import { isEmpty } from 'lodash'; | ||
import { useIntl } from 'react-intl'; | ||
import Tabs from 'react-responsive-tabs'; | ||
import EditBlockWrapper from '@eeacms/volto-tabs-block/components/EditBlockWrapper'; | ||
import { MenuItem } from '@eeacms/volto-tabs-block/components/templates/default/Edit'; | ||
import { emptyBlocksForm } from '@plone/volto/helpers'; | ||
import { Icon } from 'semantic-ui-react'; | ||
import { Icon as VoltoIcon, BlocksForm } from '@plone/volto/components'; | ||
import config from '@plone/volto/registry'; | ||
import { TABS_BLOCK } from '@eeacms/volto-tabs-block/constants'; | ||
import { getParentTabFromHash } from '@eeacms/volto-tabs-block/helpers'; | ||
import noop from 'lodash/noop'; | ||
|
||
import '@eeacms/volto-tabs-block/less/menu.less'; | ||
import Edit from '@eeacms/volto-tabs-block/components/templates/default/Edit'; | ||
import { accordionSchemaEnhancer } from '@eeacms/volto-tabs-block/components/templates/accordion/schema'; | ||
import cx from 'classnames'; | ||
|
||
class Tab extends React.Component { | ||
render() { | ||
return this.props.children; | ||
} | ||
} | ||
|
||
const Edit = (props) => { | ||
const intl = useIntl(); | ||
const { | ||
data = {}, | ||
tabsList = [], | ||
tabs = {}, | ||
activeTabIndex = 0, | ||
setActiveTab = noop, | ||
editingTab = null, | ||
activeBlock = null, | ||
activeTab = null, | ||
block = null, | ||
setEditingTab = noop, | ||
multiSelected = [], | ||
manage = false, | ||
metadata = null, | ||
selected = false, | ||
onChangeBlock = noop, | ||
onChangeTabData = noop, | ||
onSelectBlock = noop, | ||
emptyTab = noop, | ||
tabsData = {}, | ||
schema, | ||
} = props; | ||
|
||
const { menuInverted, menuSecondary, menuPointing } = data; | ||
const accordionConfig = config.blocks.blocksConfig[ | ||
TABS_BLOCK | ||
].variations.filter((v, _i) => v.id === data.variation); | ||
const { icons, semanticIcon, transformWidth = 800 } = | ||
accordionConfig?.[0] || {}; | ||
|
||
const tabsContainer = React.useRef(); | ||
const [mounted, setMounted] = React.useState(false); | ||
const [hashTab, setHashTab] = React.useState(false); | ||
const [initialWidth, setInitialWidth] = React.useState(transformWidth); | ||
|
||
const items = tabsList.map((tab, index) => { | ||
const active = activeTabIndex === index; | ||
|
||
return { | ||
title: ( | ||
<> | ||
{semanticIcon ? ( | ||
<div className="tabs-icon"> | ||
<Icon | ||
className={active ? semanticIcon.opened : semanticIcon.closed} | ||
/> | ||
</div> | ||
) : ( | ||
<div className="tabs-icon"> | ||
<VoltoIcon | ||
name={active ? icons.opened : icons.closed} | ||
size={icons.size} | ||
className="tabs-icon" | ||
/> | ||
</div> | ||
)} | ||
<MenuItem | ||
{...props} | ||
key={tab} | ||
editingTab={editingTab} | ||
index={index} | ||
setEditingTab={setEditingTab} | ||
tab={tab} | ||
schema={schema} | ||
/> | ||
</> | ||
), | ||
content: ( | ||
<Tab {...props} tab={tab} content={tabs[tab]} aria-hidden={false}> | ||
<BlocksForm | ||
allowedBlocks={data?.allowedBlocks} | ||
description={data?.instructions?.data} | ||
manage={manage} | ||
metadata={metadata} | ||
pathname={props.pathname} | ||
properties={isEmpty(tabs[tab]) ? emptyBlocksForm() : tabs[tab]} | ||
selected={selected && activeTab === tab && activeBlock} | ||
selectedBlock={ | ||
selected && activeTab === tab && activeBlock ? activeBlock : null | ||
} | ||
title={data?.placeholder} | ||
onChangeField={onChangeTabData} | ||
onChangeFormData={(newFormData) => { | ||
onChangeBlock(block, { | ||
...data, | ||
data: { | ||
...tabsData, | ||
blocks: { | ||
...tabsData.blocks, | ||
[activeTab]: { | ||
...(newFormData.blocks_layout.items.length > 0 | ||
? newFormData | ||
: emptyTab({ | ||
schema: schema?.properties?.data?.schema || {}, | ||
intl, | ||
})), | ||
}, | ||
}, | ||
}, | ||
}); | ||
}} | ||
onSelectBlock={(id, selected, e) => { | ||
const isMultipleSelection = e | ||
? e.shiftKey || e.ctrlKey || e.metaKey | ||
: false; | ||
onSelectBlock( | ||
id, | ||
activeBlock === id ? false : isMultipleSelection, | ||
e, | ||
); | ||
setEditingTab(null); | ||
}} | ||
> | ||
{({ draginfo }, editBlock, blockProps) => { | ||
return ( | ||
<EditBlockWrapper | ||
blockProps={blockProps} | ||
draginfo={draginfo} | ||
multiSelected={multiSelected.includes(blockProps.block)} | ||
> | ||
{editBlock} | ||
</EditBlockWrapper> | ||
); | ||
}} | ||
</BlocksForm> | ||
</Tab> | ||
), | ||
key: tab, | ||
tabClassName: cx('ui button item title', { active }, 'remove-menu'), | ||
panelClassName: cx('ui bottom attached segment tab', { | ||
active, | ||
}), | ||
}; | ||
}); | ||
|
||
React.useEffect(() => { | ||
setMounted(true); | ||
}, []); | ||
React.useEffect(() => { | ||
if (!mounted) return; | ||
const { blockWidth, tabsTotalWidth } = tabsContainer.current?.state || {}; | ||
setInitialWidth( | ||
tabsTotalWidth < blockWidth ? tabsTotalWidth + 1 : blockWidth + 1, | ||
); | ||
}, [mounted]); | ||
|
||
const AccordionEdit = (props) => { | ||
return ( | ||
<Tabs | ||
ref={tabsContainer} | ||
transformWidth={initialWidth} | ||
selectedTabKey={tabsList[activeTabIndex]} | ||
unmountOnExit={false} | ||
items={items} | ||
onChange={() => { | ||
const { blockWidth } = tabsContainer.current?.state || {}; | ||
const tabWithHash = getParentTabFromHash( | ||
data, | ||
props?.location?.hash.substring(1), | ||
); | ||
if (tabWithHash === tabsList[activeTabIndex] && !hashTab) { | ||
setHashTab(true); | ||
} else if (blockWidth <= initialWidth) { | ||
setActiveTab(null); | ||
} | ||
<Edit | ||
skipColorOption={true} | ||
{...props} | ||
addTabsOptions={(data) => { | ||
return cx(data?.theme ? `${data?.theme}` : '', { | ||
RRT__tabs: true, | ||
'tabs-accordion-icon-right': data.accordionIconRight, | ||
'tabs-accessibility': true, | ||
inverted: data.menuInverted, | ||
pointing: data.menuPointing, | ||
}); | ||
}} | ||
tabsWrapperClass={cx( | ||
props?.data?.accordionIconRight ? 'tabs-accordion-icon-right' : '', | ||
'ui fluid menu tabs-secondary-variant', | ||
'tabs-accessibility', | ||
data?.theme ? `${data?.theme}` : '', | ||
{ | ||
inverted: menuInverted, | ||
}, | ||
{ | ||
pointing: menuPointing, | ||
}, | ||
{ | ||
secondary: menuSecondary, | ||
}, | ||
)} | ||
showMore={false} | ||
/> | ||
); | ||
}; | ||
|
||
Edit.schemaEnhancer = accordionSchemaEnhancer; | ||
AccordionEdit.schemaEnhancer = accordionSchemaEnhancer; | ||
|
||
export default Edit; | ||
export default AccordionEdit; |
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,3 +1,4 @@ | ||
import AccordionView from './View'; | ||
import AccordionEdit from './Edit'; | ||
|
||
export { AccordionView }; | ||
export { AccordionView, AccordionEdit }; |
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