Skip to content

Commit

Permalink
refactor: move state for widget-tabs to app
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippovAM committed Apr 9, 2020
1 parent ec8c1e9 commit 0e7efeb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
/*.log
*.lock

package-lock.json
package-lock.json
.firebase
26 changes: 23 additions & 3 deletions src/components/app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import { Component } from 'preact'
import Button from './button'
import WidgetModal from './widget-modal'
import { useContext, useState } from 'preact/compat'
import { useContext, useEffect, useState } from 'preact/compat'
import { LocaleStore } from '../store/locale'
import LanguageSelect from './language-select'
import WidgetTabs from './widget-tabs'
import { TabsEnum } from '../constants/tabs'

const DEFAULT_ACTIVE_TAB = TabsEnum.FILE
const DEFAULT_TABS = Object.values(TabsEnum)

export default class App extends Component {
render(props) {
const [isOpen, setIsOpen] = useState()
const {state: {translate: t}} = useContext(LocaleStore)
const [activeTab, setActiveTab] = useState(props.activeTab || DEFAULT_ACTIVE_TAB)
const [tabs, setTabs] = useState(props.tabs || DEFAULT_TABS)

useEffect(() => {
props.tabs && setTabs(props.tabs)
}, [props.tabs])

useEffect(() => {
props.activeTab && setActiveTab(props.activeTab)
}, [props.activeTab])

const onClickTab = tab => e => {
e.preventDefault()
setActiveTab(tab)
}

return (
<div id="app">
Expand All @@ -18,8 +37,9 @@ export default class App extends Component {

<WidgetModal isOpen={isOpen} onClose={() => setIsOpen(false)}>
<WidgetTabs
activeTab={props.activeTab}
tabs={props.tabs}
activeTab={activeTab}
tabs={tabs}
onClick={onClickTab}
/>
</WidgetModal>
</div>
Expand Down
49 changes: 12 additions & 37 deletions src/components/widget-tabs/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,19 @@
import { useEffect, useState } from 'preact/compat'
import Tabs from '../tabs'
import TabList from '../tab-list'
import TabButton from '../tab-button'
import TabPanel from '../tab-panel'
import { TabsEnum } from '../../constants/tabs'

const DEFAULT_ACTIVE_TAB = TabsEnum.FILE
const DEFAULT_TABS = Object.values(TabsEnum)

const WidgetTabs = props => {
const [activeTab, setActiveTab] = useState(props.activeTab || DEFAULT_ACTIVE_TAB)
const [tabs, setTabs] = useState(props.tabs || DEFAULT_TABS)

useEffect(() => {
setTabs(props.tabs)
}, [props.tabs])

useEffect(() => {
setActiveTab(props.activeTab)
}, [props.activeTab])

const onClickTab = (tab) => {
return e => {
e.preventDefault()
setActiveTab(tab)
}
}

return (
<Tabs>
<TabList>
{tabs.map((tab) => (
<TabButton tab={tab} onClick={onClickTab(tab)} isActive={tab === activeTab}>
{tab}
</TabButton>
))}
</TabList>
<TabPanel activeTab={activeTab} />
</Tabs>
)
}
const WidgetTabs = ({tabs, activeTab, onClick}) => (
<Tabs>
<TabList>
{tabs.map((tab) => (
<TabButton tab={tab} onClick={onClick(tab)} isActive={tab === activeTab}>
{tab}
</TabButton>
))}
</TabList>
<TabPanel activeTab={activeTab} />
</Tabs>
)

export default WidgetTabs

0 comments on commit 0e7efeb

Please sign in to comment.