akkoma-fe/src/components/tab_switcher/tab_switcher.jsx

184 lines
4.7 KiB
React
Raw Normal View History

2021-04-25 10:23:16 +00:00
// eslint-disable-next-line no-unused
2022-03-21 19:29:51 +00:00
import { h, Fragment } from 'vue'
import { mapState } from 'vuex'
2020-10-20 19:54:43 +00:00
import { FontAwesomeIcon as FAIcon } from '@fortawesome/vue-fontawesome'
import './tab_switcher.scss'
2021-04-25 10:23:16 +00:00
const findFirstUsable = (slots) => slots.findIndex(_ => _.props)
export default {
name: 'TabSwitcher',
props: {
renderOnlyFocused: {
required: false,
type: Boolean,
default: false
},
onSwitch: {
required: false,
2019-11-08 16:14:01 +00:00
type: Function,
default: undefined
},
activeTab: {
required: false,
2019-11-08 16:14:01 +00:00
type: String,
default: undefined
},
scrollableTabs: {
required: false,
type: Boolean,
default: false
2020-05-03 14:36:12 +00:00
},
sideTabBar: {
required: false,
type: Boolean,
default: false
},
bodyScrollLock: {
required: false,
type: Boolean,
default: false
}
},
data () {
return {
2022-03-21 19:29:51 +00:00
active: findFirstUsable(this.slots())
}
},
2019-08-10 18:48:05 +00:00
computed: {
activeIndex () {
// In case of controlled component
if (this.activeTab) {
return this.slots().findIndex(slot => slot && slot.props && this.activeTab === slot.props.key)
2019-08-10 18:48:05 +00:00
} else {
return this.active
}
},
isActive () {
return tabName => {
const isWanted = slot => slot.props && slot.props['data-tab-name'] === tabName
2022-03-27 09:58:28 +00:00
return this.$slots.default().findIndex(isWanted) === this.activeIndex
}
},
settingsModalVisible () {
return this.settingsModalState === 'visible'
},
...mapState({
settingsModalState: state => state.interface.settingsModalState
})
2019-08-10 18:48:05 +00:00
},
2019-07-05 07:02:14 +00:00
beforeUpdate () {
2021-04-18 12:03:28 +00:00
const currentSlot = this.slots()[this.active]
2021-04-25 10:23:16 +00:00
if (!currentSlot.props) {
2021-04-18 12:03:28 +00:00
this.active = findFirstUsable(this.slots())
2019-07-05 07:02:14 +00:00
}
},
methods: {
clickTab (index) {
return (e) => {
e.preventDefault()
this.setTab(index)
}
},
2021-04-18 12:03:28 +00:00
// DO NOT put it to computed, it doesn't work (caching?)
slots () {
2022-03-21 19:29:51 +00:00
if (this.$slots.default()[0].type === Fragment) {
return this.$slots.default()[0].children
}
2022-03-18 11:32:36 +00:00
return this.$slots.default()
2021-04-18 12:03:28 +00:00
},
setTab (index) {
if (typeof this.onSwitch === 'function') {
2021-04-18 12:03:28 +00:00
this.onSwitch.call(null, this.slots()[index].key)
}
this.active = index
if (this.scrollableTabs) {
this.$refs.contents.scrollTop = 0
}
}
},
2021-04-25 10:23:16 +00:00
render () {
2021-04-18 12:03:28 +00:00
const tabs = this.slots()
2019-07-05 07:02:14 +00:00
.map((slot, index) => {
2021-04-25 10:23:16 +00:00
const props = slot.props
if (!props) return
const classesTab = ['tab', 'button-default']
2019-07-05 07:02:14 +00:00
const classesWrapper = ['tab-wrapper']
2019-08-10 18:48:05 +00:00
if (this.activeIndex === index) {
2019-07-05 07:02:14 +00:00
classesTab.push('active')
classesWrapper.push('active')
}
if (props.image) {
return (
2019-08-10 04:26:29 +00:00
<div class={classesWrapper.join(' ')}>
<button
disabled={props.disabled}
onClick={this.clickTab(index)}
class={classesTab.join(' ')}
type="button"
>
<img src={props.image} title={props['image-tooltip']}/>
{props.label ? '' : props.label}
</button>
</div>
)
}
2019-07-05 07:02:14 +00:00
return (
2019-08-10 04:26:29 +00:00
<div class={classesWrapper.join(' ')}>
2019-07-05 07:02:14 +00:00
<button
disabled={props.disabled}
onClick={this.clickTab(index)}
2020-05-25 13:10:14 +00:00
class={classesTab.join(' ')}
type="button"
2020-05-25 13:10:14 +00:00
>
{!props.icon ? '' : (<FAIcon class="tab-icon" size="2x" fixed-width icon={props.icon}/>)}
<span class="text">
{props.label}
</span>
2020-05-25 13:10:14 +00:00
</button>
2019-07-05 07:02:14 +00:00
</div>
)
})
2021-04-18 12:03:28 +00:00
const contents = this.slots().map((slot, index) => {
2021-04-25 10:23:16 +00:00
const props = slot.props
if (!props) return
2019-08-10 18:48:05 +00:00
const active = this.activeIndex === index
const classes = [ active ? 'active' : 'hidden' ]
if (props.fullHeight) {
classes.push('full-height')
}
2020-05-28 18:26:33 +00:00
const renderSlot = (!this.renderOnlyFocused || active)
? slot
: ''
return (
<div class={classes}>
{
this.sideTabBar
? <h1 class="mobile-label">{props.label}</h1>
: ''
}
2020-05-28 18:26:33 +00:00
{renderSlot}
</div>
)
})
return (
2020-05-03 14:36:12 +00:00
<div class={'tab-switcher ' + (this.sideTabBar ? 'side-tabs' : 'top-tabs')}>
2019-01-17 20:05:58 +00:00
<div class="tabs">
{tabs}
</div>
<div
ref="contents"
class={'contents' + (this.scrollableTabs ? ' scrollable-tabs' : '')}
v-body-scroll-lock={this.bodyScrollLock}
>
2019-01-17 20:05:58 +00:00
{contents}
</div>
2018-11-21 19:08:27 +00:00
</div>
)
}
}