Skip to content

Commit

Permalink
refactor(Swipe): refactor with composition api
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Sep 17, 2020
1 parent 06c05ba commit 39c68c9
Show file tree
Hide file tree
Showing 4 changed files with 398 additions and 382 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -64,9 +64,9 @@
"devDependencies": {
"@ls-lint/ls-lint": "^1.8.0",
"@vant/cli": "^3.0.0-alpha.10",
"@vue/compiler-sfc": "3.0.0-rc.10",
"@vue/compiler-sfc": "3.0.0-rc.12",
"prettier": "^2.0.4",
"vue": "3.0.0-rc.10"
"vue": "3.0.0-rc.12"
},
"sideEffects": [
"es/**/style/*",
Expand Down
62 changes: 31 additions & 31 deletions src/swipe-item/index.js
@@ -1,63 +1,63 @@
import { computed, nextTick, onMounted, reactive } from 'vue';
import { SWIPE_KEY } from '../swipe';
import { createNamespace } from '../utils';
import { ChildrenMixin } from '../mixins/relation';
import { useParent } from '../composition/use-relation';

const [createComponent, bem] = createNamespace('swipe-item');

export default createComponent({
mixins: [ChildrenMixin('vanSwipe')],

data() {
return {
setup(props, { slots }) {
const state = reactive({
offset: 0,
mounted: false,
});

const setOffset = (offset) => {
state.offset = offset;
};
},

mounted() {
this.$nextTick(() => {
this.mounted = true;
const { parent, index } = useParent(SWIPE_KEY, { setOffset });

onMounted(() => {
nextTick(() => {
state.mounted = true;
});
});
},

computed: {
style() {
const style = computed(() => {
const style = {};
const { size, vertical } = this.parent;
const { vertical } = parent.props;

style[vertical ? 'height' : 'width'] = `${size}px`;
style[vertical ? 'height' : 'width'] = `${parent.size.value}px`;

if (this.offset) {
style.transform = `translate${vertical ? 'Y' : 'X'}(${this.offset}px)`;
if (state.offset) {
style.transform = `translate${vertical ? 'Y' : 'X'}(${state.offset}px)`;
}

return style;
},

shouldRender() {
const { index, parent, mounted } = this;
});

if (!parent.lazyRender) {
const shouldRender = computed(() => {
if (!parent.props.lazyRender) {
return true;
}

// wait for all item to mount, so we can get the exact count
if (!mounted) {
if (!state.mounted) {
return false;
}

const active = parent.activeIndicator;
const maxActive = parent.count - 1;
const active = parent.activeIndicator.value;
const maxActive = parent.count.value - 1;
const prevActive = active === 0 ? maxActive : active - 1;
const nextActive = active === maxActive ? 0 : active + 1;

return index === active || index === prevActive || index === nextActive;
},
},
return index.value >= prevActive || index.value <= nextActive;
});

render() {
return (
<div class={bem()} style={this.style}>
{this.shouldRender ? this.$slots.default?.() : null}
return () => (
<div class={bem()} style={style.value}>
{shouldRender.value ? slots.default?.() : null}
</div>
);
},
Expand Down

0 comments on commit 39c68c9

Please sign in to comment.