Skip to content

Commit

Permalink
fix(ui/picker): 设计了基本的样式和基本dom结构 完成了基本的滚动
Browse files Browse the repository at this point in the history
affects: @varlet/ui
  • Loading branch information
齐皓 authored and 齐皓 committed Jan 31, 2021
1 parent 32f61b2 commit 9d9f1b9
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 0 deletions.
101 changes: 101 additions & 0 deletions packages/varlet-ui/src/picker/Picker.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<template>
<div class="var-picker" v-bind="$attrs">
<div class="var-picker__columns" :style="{ height: `${optionCount * optionHeight}px` }">
<div
class="var-picker__column"
v-for="c in scrollColumns"
:key="c"
@touchstart="handleTouchstart($event, c)"
@touchmove="handleTouchmove($event, c)"
@touchend="handleTouchend($event, c)"
>
<div class="var-picker__scroller" :style="{ transform: `translateY(${c.translate}px)` }">
<div class="var-picker__option" :style="{ height: `${optionHeight}px` }" v-for="t in c.column.texts" :key="t">
{{ t }}
</div>
</div>
</div>
<div
class="var-picker__picked"
:style="{
top: `${center}px`,
height: `${optionHeight}px`,
}"
></div>
</div>
</div>
</template>

<script lang="ts">
import { defineComponent, watch, ref, Ref, computed, ComputedRef } from 'vue'
import { NormalColumn, props } from './props'
interface ScrollColumn {
touching: boolean
prevY: number | undefined
column: NormalColumn
translate: number
}
export default defineComponent({
name: 'VarPicker',
inheritAttrs: false,
props,
setup(props) {
const scrollColumns: Ref<ScrollColumn[]> = ref([])
const center: ComputedRef<number> = computed(
() => (props.optionCount * props.optionHeight) / 2 - props.optionCount / 2
)
const handleTouchstart = (event: TouchEvent, scrollColumn: ScrollColumn) => {
scrollColumn.touching = true
}
const handleTouchmove = (event: TouchEvent, scrollColumn: ScrollColumn) => {
if (!scrollColumn.touching) {
return
}
const { clientY } = event.touches[0]
const moveY = scrollColumn.prevY !== undefined ? clientY - scrollColumn.prevY : 0
scrollColumn.prevY = clientY
scrollColumn.translate += moveY
}
const handleTouchend = () => {
scrollColumns.value.forEach((scrollColumn: ScrollColumn) => {
scrollColumn.touching = false
scrollColumn.prevY = undefined
})
}
watch(
() => props.columns,
(newValue: any) => {
scrollColumns.value = newValue.map((column: NormalColumn) => {
return {
prevY: undefined,
touching: false,
translate: center.value,
column,
}
})
},
{ immediate: true }
)
return {
scrollColumns,
center,
handleTouchstart,
handleTouchmove,
handleTouchend,
}
},
})
</script>

<style lang="less">
@import './picker';
</style>
7 changes: 7 additions & 0 deletions packages/varlet-ui/src/picker/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Picker = require('../../../cjs/picker').default
const { render } = require('@testing-library/vue')

test('test picker', async () => {
const wrapper = render(Picker)
console.log(wrapper)
})
Empty file.
Empty file.
35 changes: 35 additions & 0 deletions packages/varlet-ui/src/picker/example/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<template>
<div class="example">
<var-picker :columns="columns" />
</div>
</template>

<script>
import { defineComponent, ref } from 'vue'
import Picker from '..'
export default defineComponent({
name: 'PickerExample',
components: {
[Picker.name]: Picker,
},
setup() {
return {
columns: ref([
{
texts: ['1', '2', '3'],
},
]),
}
},
})
</script>

<style scoped>
.example {
background: #ddd;
width: 100%;
min-height: 80vh;
padding-top: 50px;
}
</style>
8 changes: 8 additions & 0 deletions packages/varlet-ui/src/picker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { App } from 'vue'
import Picker from './Picker.vue'

Picker.install = function (app: App) {
app.component(Picker.name, Picker)
}

export default Picker
38 changes: 38 additions & 0 deletions packages/varlet-ui/src/picker/picker.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@picker-background: #fff;
@picker-picked-border: 1px solid #ddd;

.var-picker {
width: 100%;
background: @picker-background;

&__columns {
position: relative;
display: flex;
width: 100%;
}

&__column {
flex-grow: 1;
overflow: hidden;
}

&__scroller {
width: 100%;
}

&__option {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}

&__picked {
position: absolute;
pointer-events: none;
width: 100%;
left: 0;
border-top: @picker-picked-border;
border-bottom: @picker-picked-border;
}
}
45 changes: 45 additions & 0 deletions packages/varlet-ui/src/picker/props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { PropType } from 'vue'

export interface NormalColumn {
texts: any[]
initialIndex?: number
}

export interface CascadeColumn {
[textKey: string]: any
children: CascadeColumn[]
}

export const props = {
columns: {
type: Array as PropType<NormalColumn[] | CascadeColumn[]>,
default: [],
},
title: {
type: String,
},
textKey: {
type: String,
default: 'text',
},
toolbar: {
type: Boolean,
default: true,
},
optionHeight: {
type: Number,
default: 44,
},
optionCount: {
type: Number,
default: 6,
},
confirmButtonText: {
type: String,
default: '确认',
},
cancelButtonText: {
type: String,
default: '取消',
},
}

0 comments on commit 9d9f1b9

Please sign in to comment.