Skip to content

Commit

Permalink
feat(upload): fix upload-trigger and upload-file-list bug (tusen-…
Browse files Browse the repository at this point in the history
  • Loading branch information
kev1nzh committed Sep 18, 2021
1 parent f8fe3b3 commit 02a49b9
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 217 deletions.
13 changes: 4 additions & 9 deletions src/upload/src/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,7 @@ const uploadProps = {
},
onPreview: Function as PropType<OnPreview>,
createThumbnailUrl: Function as PropType<CreateThumbnailUrl>,
abstract: {
type: Boolean,
default: false
}
abstract: Boolean
} as const

export type UploadProps = ExtractPublicPropTypes<typeof uploadProps>
Expand Down Expand Up @@ -551,12 +548,10 @@ export default defineComponent({
multiple={this.multiple}
onChange={this.handleFileInputChange}
/>
{this.listType !== 'image-card' ? (
{this.listType !== 'image-card' && (
<NUploadTrigger>{this.$slots}</NUploadTrigger>
) : null}
{this.showFileList ? (
<NUploadFileList>{this.$slots}</NUploadFileList>
) : null}
)}
{this.showFileList && <NUploadFileList>{this.$slots}</NUploadFileList>}
</div>
)
}
Expand Down
42 changes: 28 additions & 14 deletions src/upload/src/UploadFileList.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { h, defineComponent, inject, VNode, CSSProperties } from 'vue'
import {
h,
defineComponent,
inject,
VNode,
CSSProperties,
computed,
ComputedRef
} from 'vue'
import { throwError } from '../../_utils'
import { uploadInjectionKey } from './interface'
import NUploadFile from './UploadFile'
import { NImageGroup } from '../../image'
import { NFadeInExpandTransition } from '../../_internal'
import NUploadTrigger from './UploadTrigger'

export default defineComponent({
name: 'UploadFileList',
setup (_, { slots }) {
Expand All @@ -23,7 +32,9 @@ export default defineComponent({
fileListStyle,
cssVarsRef
} = NUpload
const isImageCardType = listTypeRef.value === 'image-card'
const isImageCardTypeRef = computed(
() => listTypeRef.value === 'image-card'
)

const createFileList = (): VNode[] =>
mergedFileListRef.value.map((file) => (
Expand All @@ -35,27 +46,30 @@ export default defineComponent({
/>
))

const uploadFileList = isImageCardType ? (
<NImageGroup>{{ default: createFileList }}</NImageGroup>
) : (
<NFadeInExpandTransition group>
{{
default: createFileList
}}
</NFadeInExpandTransition>
)
const createUploadFileList = (
isImageCardTypeRef: ComputedRef<boolean>
): VNode =>
isImageCardTypeRef.value ? (
<NImageGroup>{{ default: createFileList }}</NImageGroup>
) : (
<NFadeInExpandTransition group>
{{
default: createFileList
}}
</NFadeInExpandTransition>
)

return () => (
<div
class={[
`${mergedClsPrefixRef.value}-upload-file-list`,
isImageCardType &&
isImageCardTypeRef.value &&
`${mergedClsPrefixRef.value}-upload-file-list--grid`
]}
style={[cssVarsRef.value, fileListStyle as CSSProperties]}
>
{uploadFileList}
{isImageCardType && <NUploadTrigger>{slots}</NUploadTrigger>}
{createUploadFileList(isImageCardTypeRef)}
{isImageCardTypeRef.value && <NUploadTrigger>{slots}</NUploadTrigger>}
</div>
)
}
Expand Down
44 changes: 18 additions & 26 deletions src/upload/src/UploadTrigger.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { h, defineComponent, inject, computed, renderSlot } from 'vue'
import { ExtractPublicPropTypes, throwError } from '../../_utils'
import { h, defineComponent, inject, renderSlot, computed } from 'vue'
import { throwError } from '../../_utils'
import { uploadInjectionKey } from './interface'
import NUploadDragger from './UploadDragger'

const uploadTriggerProps = {
abstract: {
type: Boolean,
default: false
}
} as const
export type UploadTriggerProps = ExtractPublicPropTypes<
typeof uploadTriggerProps
>

export default defineComponent({
name: 'UploadTrigger',
props: uploadTriggerProps,
setup (props, { slots }) {
setup (_, { slots }) {
const NUpload = inject(uploadInjectionKey, null)
if (!NUpload) {
throwError(
Expand All @@ -33,12 +22,11 @@ export default defineComponent({
openFileDialog,
draggerInsideRef,
handleFileAddition,
abstractRef: parentAbstract
abstractRef
} = NUpload

const isImageCardType = listTypeRef.value === 'image-card'
const mergedAbstractRef = computed(
() => parentAbstract.value && props.abstract
const isImageCardTypeRef = computed(
() => listTypeRef.value === 'image-card'
)

function handleTriggerClick (): void {
Expand Down Expand Up @@ -69,19 +57,19 @@ export default defineComponent({
}

return () =>
mergedAbstractRef.value ? (
abstractRef.value ? (
renderSlot(slots, 'default', {
handleTriggerClick,
handleTriggerDrop,
handleTriggerDragOver,
handleTriggerDragEnter,
handleTriggerDragLeave
handleClick: handleTriggerClick,
handleDrop: handleTriggerDrop,
handleDragOver: handleTriggerDragOver,
handleDragEnter: handleTriggerDragEnter,
handleDragLeave: handleTriggerDragLeave
})
) : (
<div
class={[
`${mergedClsPrefixRef.value}-upload__trigger`,
isImageCardType &&
isImageCardTypeRef.value &&
`${mergedClsPrefixRef.value}-upload__trigger--image-card`
]}
onClick={handleTriggerClick}
Expand All @@ -90,7 +78,11 @@ export default defineComponent({
onDragenter={handleTriggerDragEnter}
onDragleave={handleTriggerDragLeave}
>
{isImageCardType ? <NUploadDragger>{slots}</NUploadDragger> : slots}
{isImageCardTypeRef.value ? (
<NUploadDragger>{slots}</NUploadDragger>
) : (
slots
)}
</div>
)
}
Expand Down
Loading

0 comments on commit 02a49b9

Please sign in to comment.