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

184 lines
4.6 KiB
React
Raw Normal View History

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