Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(components): [scrollbar] switch to script-setup syntax #7986

Merged
merged 3 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 5 additions & 8 deletions packages/components/scrollbar/src/bar.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { buildProps } from '@element-plus/utils'
import type { ExtractPropTypes } from 'vue'
import type Bar from './bar.vue'

export const barProps = buildProps({
always: {
type: Boolean,
default: true,
},
width: {
type: String,
default: '',
},
height: {
type: String,
default: '',
},
width: String,
height: String,
ratioX: {
type: Number,
default: 1,
Expand All @@ -24,3 +19,5 @@ export const barProps = buildProps({
},
} as const)
export type BarProps = ExtractPropTypes<typeof barProps>

export type BarInstance = InstanceType<typeof Bar>
42 changes: 17 additions & 25 deletions packages/components/scrollbar/src/bar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,28 @@
:always="always"
/>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
<script lang="ts" setup>
import { ref } from 'vue'
import { GAP } from './util'
import Thumb from './thumb.vue'
import { barProps } from './bar'

export default defineComponent({
components: {
Thumb,
},
props: barProps,
setup(props) {
const moveX = ref(0)
const moveY = ref(0)
const GAP = 4 // top 2 + bottom 2 of bar instance
const props = defineProps(barProps)

const handleScroll = (wrap: HTMLDivElement) => {
if (wrap) {
const offsetHeight = wrap.offsetHeight - GAP
const offsetWidth = wrap.offsetWidth - GAP
const moveX = ref(0)
const moveY = ref(0)

moveY.value = ((wrap.scrollTop * 100) / offsetHeight) * props.ratioY
moveX.value = ((wrap.scrollLeft * 100) / offsetWidth) * props.ratioX
}
}
const handleScroll = (wrap: HTMLDivElement) => {
if (wrap) {
const offsetHeight = wrap.offsetHeight - GAP
const offsetWidth = wrap.offsetWidth - GAP

return {
handleScroll,
moveX,
moveY,
}
},
moveY.value = ((wrap.scrollTop * 100) / offsetHeight) * props.ratioY
moveX.value = ((wrap.scrollLeft * 100) / offsetWidth) * props.ratioX
}
}

defineExpose({
handleScroll,
})
</script>
14 changes: 5 additions & 9 deletions packages/components/scrollbar/src/scrollbar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { buildProps, definePropType, isNumber } from '@element-plus/utils'
import type { ExtractPropTypes, StyleValue } from 'vue'
import type Scrollbar from './scrollbar.vue'

export const scrollbarProps = buildProps({
height: {
Expand All @@ -10,10 +11,7 @@ export const scrollbarProps = buildProps({
type: [String, Number],
default: '',
},
native: {
type: Boolean,
default: false,
},
native: Boolean,
sxzz marked this conversation as resolved.
Show resolved Hide resolved
wrapStyle: {
type: definePropType<StyleValue>([String, Object, Array]),
default: '',
Expand All @@ -35,16 +33,12 @@ export const scrollbarProps = buildProps({
type: String,
default: 'div',
},
always: {
type: Boolean,
default: false,
},
always: Boolean,
sxzz marked this conversation as resolved.
Show resolved Hide resolved
minSize: {
type: Number,
default: 20,
},
} as const)

export type ScrollbarProps = ExtractPropTypes<typeof scrollbarProps>

export const scrollbarEmits = {
Expand All @@ -57,3 +51,5 @@ export const scrollbarEmits = {
}) => isNumber(scrollTop) && isNumber(scrollLeft),
}
export type ScrollbarEmits = typeof scrollbarEmits

export type ScrollbarInstance = InstanceType<typeof Scrollbar>