Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ui/bottom-navigation): handle active boundary #411

Merged
merged 17 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions packages/varlet-ui/src/bottom-navigation/BottomNavigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
</template>

<script lang="ts">
import { defineComponent, ref, computed, onMounted, onUpdated } from 'vue'
import { defineComponent, ref, computed, onMounted, onUpdated, watch } from 'vue'
import type { Ref, ComputedRef } from 'vue'
import { props } from './props'
import VarButton from '../button'
import { useBottomNavigationItems } from './provide'
import type { BottomNavigationProvider } from './provide'
import type { BottomNavigationItemProvider } from '../bottom-navigation-item/provide'
import { createNamespace, call } from '../utils/components'
import { isNumber } from '../utils/shared'

const { n, classes } = createNamespace('bottom-navigation')
const { n: nItem } = createNamespace('bottom-navigation-item')
Expand All @@ -48,6 +50,39 @@ export default defineComponent({
const activeColor: ComputedRef<string | undefined> = computed(() => props.activeColor)
const inactiveColor: ComputedRef<string | undefined> = computed(() => props.inactiveColor)
const fabProps = ref({})
const { length, bottomNavigationItems, bindBottomNavigationItem } = useBottomNavigationItems()

const matchBoundary = (): void => {
if (length.value === 0 || matchName() || matchIndex()) {
return
}

handleActiveIndex()
}

const matchName = (): BottomNavigationItemProvider | undefined => {
return bottomNavigationItems.find(({ name }: BottomNavigationItemProvider) => {
return active.value === name.value
})
}

const matchIndex = (): BottomNavigationItemProvider | undefined => {
return bottomNavigationItems.find(({ index }: BottomNavigationItemProvider) => {
return active.value === index.value
})
}

const handleActiveIndex = () => {
if (!isNumber(active.value)) {
return
}

if (active.value < 0) {
call(props['onUpdate:modelValue'], 0)
} else if (active.value > length.value - 1) {
call(props['onUpdate:modelValue'], length.value - 1)
}
}

const onToggle = (changedValue: number | string) => {
if (props.onBeforeChange) {
Expand All @@ -70,8 +105,6 @@ export default defineComponent({
call(props.onChange, changedValue)
}

const { length, bindBottomNavigationItem } = useBottomNavigationItems()

const removeMarginClass = () => {
const bottomNavigationItems: Element[] = getBottomNavigationItems()
bottomNavigationItems.forEach((dom: Element) => {
Expand Down Expand Up @@ -112,7 +145,6 @@ export default defineComponent({
const handleFabClick = () => {
call(props.onFabClick)
}
fabProps.value = { ...defaultFabProps, ...props.fabProps }

const bottomNavigationProvider: BottomNavigationProvider = {
active,
Expand All @@ -123,6 +155,16 @@ export default defineComponent({

bindBottomNavigationItem(bottomNavigationProvider)

watch(() => length.value, matchBoundary)

watch(
() => props.fabProps,
(newValue) => {
fabProps.value = { ...defaultFabProps, ...newValue }
},
{ immediate: true, deep: true }
)

onMounted(() => {
if (!slots.fab) {
return
Expand Down
2 changes: 1 addition & 1 deletion packages/varlet-ui/src/tabs/Tabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default defineComponent({
components: { VarSticky },
inheritAttrs: false,
props,
setup (props) {
setup(props) {
const indicatorWidth: Ref<string> = ref('0px')
const indicatorHeight: Ref<string> = ref('0px')
const indicatorX: Ref<string> = ref('0px')
Expand Down