-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk:utils): add zIndex manager (#1015)
fix #998
- Loading branch information
1 parent
32e48d8
commit 03e6aa5
Showing
19 changed files
with
169 additions
and
30 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { mount } from '@vue/test-utils' | ||
import { computed, defineComponent, ref, toRef } from 'vue' | ||
|
||
import { useZIndex } from '../src/zIndex' | ||
|
||
describe('zIndex.ts', () => { | ||
const TestComp = defineComponent({ | ||
props: { | ||
visible: { type: Boolean, required: true }, | ||
zIndex: { type: Number, default: undefined }, | ||
}, | ||
setup(props) { | ||
const visible = toRef(props, 'visible') | ||
const { currentZIndex } = useZIndex(toRef(props, 'zIndex'), ref(3000), visible) | ||
const style = computed(() => ({ zIndex: currentZIndex.value })) | ||
|
||
return { | ||
style, | ||
} | ||
}, | ||
template: ` | ||
<div utid="test-el" :style="style"></div> | ||
`, | ||
}) | ||
|
||
test('custdom zIndex work', async () => { | ||
const wrapper = mount(TestComp, { | ||
props: { | ||
visible: false, | ||
zIndex: 2022, | ||
}, | ||
}) | ||
|
||
expect(wrapper.find('[utid="test-el"]').attributes('style')).toContain('z-index: 2022') | ||
|
||
await wrapper.setProps({ visible: true }) | ||
expect(wrapper.find('[utid="test-el"]').attributes('style')).toContain('z-index: 2022') | ||
}) | ||
|
||
test('increment zIndex work', async () => { | ||
const wrapper = mount(TestComp, { | ||
props: { | ||
visible: false, | ||
}, | ||
}) | ||
|
||
expect(wrapper.find('[utid="test-el"]').attributes('style')).toContain('z-index: 3000') | ||
|
||
await wrapper.setProps({ visible: true }) | ||
expect(wrapper.find('[utid="test-el"]').attributes('style')).toContain('z-index: 3000') | ||
|
||
const wrapper2 = mount(TestComp, { | ||
props: { | ||
visible: false, | ||
}, | ||
}) | ||
await wrapper2.setProps({ visible: true }) | ||
expect(wrapper2.find('[utid="test-el"]').attributes('style')).toContain('z-index: 3001') | ||
|
||
const wrapper3 = mount(TestComp, { | ||
props: { | ||
visible: true, | ||
}, | ||
}) | ||
expect(wrapper3.find('[utid="test-el"]').attributes('style')).toContain('z-index: 3002') | ||
|
||
expect(wrapper.find('[utid="test-el"]').attributes('style')).toContain('z-index: 3000') | ||
await wrapper.setProps({ visible: false }) | ||
await wrapper.setProps({ visible: true }) | ||
expect(wrapper.find('[utid="test-el"]').attributes('style')).toContain('z-index: 3003') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @license | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE | ||
*/ | ||
|
||
import { type ComputedRef, type Ref, computed, ref, watch } from 'vue' | ||
|
||
import { isNil } from 'lodash-es' | ||
|
||
const zIndexCount = ref(0) | ||
|
||
type UseZIndex = ( | ||
controlZIndex: Ref<number | undefined>, | ||
commonZIndex: Ref<number>, | ||
visible: Ref<boolean>, | ||
) => { | ||
currentZIndex: ComputedRef<number> | ||
nextZIndex: () => number | ||
} | ||
export const useZIndex: UseZIndex = (controlZIndex, commonZIndex, visible) => { | ||
const innerZIndex = ref(commonZIndex.value + zIndexCount.value) | ||
const currentZIndex = computed(() => controlZIndex.value ?? innerZIndex.value) | ||
const nextZIndex = () => { | ||
if (isNil(controlZIndex.value)) { | ||
innerZIndex.value = commonZIndex.value + zIndexCount.value | ||
zIndexCount.value++ | ||
} | ||
return currentZIndex.value | ||
} | ||
|
||
watch( | ||
visible, | ||
newVisible => { | ||
if (newVisible) { | ||
nextZIndex() | ||
} | ||
}, | ||
{ immediate: true }, | ||
) | ||
return { currentZIndex, nextZIndex } | ||
} |
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
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
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
Oops, something went wrong.