feat(Sticky): add change event (#8374)

This commit is contained in:
neverland 2021-03-18 09:35:31 +08:00 committed by GitHub
parent 062f5c54b4
commit 61b8cf2dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 7 deletions

View File

@ -69,9 +69,10 @@ export default {
### Events ### Events
| Event | Description | Arguments | | Event | Description | Arguments |
| ------ | ---------------------- | ------------------------------ | | --- | --- | --- |
| scroll | Emitted when scrolling | object: { scrollTop, isFixed } | | change `v3.0.10` | Emitted when sticky status changed | _isFixed: boolean_ |
| scroll | Emitted when scrolling | _{ scrollTop: number, isFixed: boolean }_ |
### Less Variables ### Less Variables

View File

@ -81,9 +81,10 @@ export default {
### Events ### Events
| 事件名 | 说明 | 回调参数 | | 事件名 | 说明 | 回调参数 |
| ------ | ---------- | ---------------------------------------------- | | --- | --- | --- |
| scroll | 滚动时触发 | { scrollTop: 距离顶部位置, isFixed: 是否吸顶 } | | change `v3.0.10` | 当吸顶状态改变时触发 | _isFixed: boolean_ |
| scroll | 滚动时触发 | _{ scrollTop: number, isFixed: boolean }_ |
### 样式变量 ### 样式变量

View File

@ -1,5 +1,6 @@
import { import {
ref, ref,
watch,
computed, computed,
PropType, PropType,
reactive, reactive,
@ -44,7 +45,7 @@ export default defineComponent({
}, },
}, },
emits: ['scroll'], emits: ['scroll', 'change'],
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const root = ref<HTMLElement>(); const root = ref<HTMLElement>();
@ -135,6 +136,11 @@ export default defineComponent({
emitScroll(scrollTop); emitScroll(scrollTop);
}; };
watch(
() => state.fixed,
(value) => emit('change', value)
);
useEventListener('scroll', onScroll, { target: scrollParent }); useEventListener('scroll', onScroll, { target: scrollParent });
useVisibilityChange(root, onScroll); useVisibilityChange(root, onScroll);

View File

@ -330,3 +330,22 @@ test('should emit scroll event when visibility changed', async () => {
window.IntersectionObserver = originIntersectionObserver; window.IntersectionObserver = originIntersectionObserver;
}); });
test('should emit change event when sticky status changed', async () => {
const wrapper = mount(Sticky, {
attrs: {
style: 'height: 10px',
},
});
const mockStickyRect = jest
.spyOn(wrapper.element, 'getBoundingClientRect')
.mockReturnValue({
top: -100,
bottom: -90,
});
await mockScrollTop(100);
expect(wrapper.emitted('change')[0]).toEqual([true]);
mockStickyRect.mockRestore();
});