-
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.
- Loading branch information
1 parent
5d023a8
commit 2d31c05
Showing
19 changed files
with
172 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, 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(visible, toRef(props, 'zIndex'), 3000) | ||
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,36 @@ | ||
/** | ||
* @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' | ||
|
||
const zIndexCount = ref(0) | ||
const getZIndex = (commonZIndex: number) => zIndexCount.value + (commonZIndex ?? 1000) | ||
|
||
type UseZIndex = ( | ||
visible: Ref<boolean>, | ||
controlZIndex: Ref<number | undefined>, | ||
commonZIndex: number, | ||
) => { currentZIndex: ComputedRef<number> } | ||
export const useZIndex: UseZIndex = (visible, controlZIndex, commonZIndex) => { | ||
const innerZIndex = ref(getZIndex(commonZIndex)) | ||
const currentZIndex = computed(() => (controlZIndex.value ? controlZIndex.value : innerZIndex.value)) | ||
|
||
watch( | ||
visible, | ||
newVisible => { | ||
if (newVisible && !controlZIndex.value) { | ||
innerZIndex.value = getZIndex(commonZIndex) | ||
zIndexCount.value++ | ||
} | ||
}, | ||
{ immediate: true }, | ||
) | ||
|
||
return { | ||
currentZIndex, | ||
} | ||
} |
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.