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

fix(upload): 修复 disabled 不生效问题 (#2725) #2727

Merged
merged 1 commit into from
Jan 26, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gorgeous-pants-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

fix(upload): 修复 disabled 不生效问题
5 changes: 5 additions & 0 deletions .changeset/soft-guests-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/upload": patch
---

fix: 修复 disabled 不生效问题
78 changes: 43 additions & 35 deletions packages/ui/upload/src/AvatarUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ export const AvatarUpload = forwardRef<HTMLDivElement | null, UploadProps>(

const uploadSuccessText = i18n.get('upload.uploadSuccess')

const cls = cx(prefixCls, `${prefixCls}--avatar`, className)
const cls = cx(
prefixCls,
`${prefixCls}--avatar`,
disabled && `${prefixCls}--disabled`,
className
)

const [_fileList, uploadFiles, deleteFile] = useUpload({
fileList,
Expand All @@ -69,6 +74,7 @@ export const AvatarUpload = forwardRef<HTMLDivElement | null, UploadProps>(
beforeUpload,
customUpload,
method,
disabled,
})

const { aspectRatio = 0, dragMode = 'move' } = avatarOptions
Expand Down Expand Up @@ -210,42 +216,44 @@ export const AvatarUpload = forwardRef<HTMLDivElement | null, UploadProps>(
}}
>
<img src={file.url} className={`${prefixCls}__thumb`} />
{file.uploadState !== 'error' ? (
<div className={`${prefixCls}__mask`}>
<div className={`${prefixCls}__action-group`}>
<span className={`${prefixCls}__action-btn`}>
<EyeOutlined
onClick={() => {
previewImage(file.url || '')
}}
/>
{!disabled ? (
file.uploadState !== 'error' ? (
<div className={`${prefixCls}__mask`}>
<div className={`${prefixCls}__action-group`}>
<span className={`${prefixCls}__action-btn`}>
<EyeOutlined
onClick={() => {
previewImage(file.url || '')
}}
/>
</span>
<span className={`${prefixCls}__action-btn`}>
<DeleteOutlined
onClick={(e) => {
e.stopPropagation()
deleteFile(file, 0)
}}
/>
</span>
</div>
</div>
) : (
<div className={`${prefixCls}__percent`}>
<span className={`${prefixCls}__error-btn`}>
<ExclamationCircleOutlined />
</span>
<span className={`${prefixCls}__action-btn`}>
<DeleteOutlined
onClick={(e) => {
e.stopPropagation()
deleteFile(file, 0)
}}
/>
<span
className={cx(`${prefixCls}__delete-btn`)}
onClick={(e) => {
e.stopPropagation()
deleteFile(file, 0)
}}
>
<CloseCircleOutlined />
</span>
</div>
</div>
) : (
<div className={`${prefixCls}__percent`}>
<span className={`${prefixCls}__error-btn`}>
<ExclamationCircleOutlined />
</span>
<span
className={cx(`${prefixCls}__delete-btn`)}
onClick={(e) => {
e.stopPropagation()
deleteFile(file, 0)
}}
>
<CloseCircleOutlined />
</span>
</div>
)}
)
) : null}
</li>
))}
{!file && (
Expand All @@ -258,7 +266,7 @@ export const AvatarUpload = forwardRef<HTMLDivElement | null, UploadProps>(
>
{children === undefined ? (
<li
className={`${prefixCls}__item ${prefixCls}__item--upload`}
className={cx(`${prefixCls}__item`, `${prefixCls}__item--upload`)}
ref={uploadRef}
tabIndex={0}
onKeyDown={handleUploadKeydown}
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/upload/src/DragUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const DragUpload = forwardRef<HTMLDivElement | null, UploadProps>(
customUpload,
method,
accept,
disabled,
})

const nonInteractive = disabled || (!!maxCount && _fileList.length >= maxCount)
Expand Down Expand Up @@ -150,6 +151,7 @@ export const DragUpload = forwardRef<HTMLDivElement | null, UploadProps>(
onDelete={deleteFile}
onDownload={onDownload}
prefixCls={prefixCls}
disabled={disabled}
/>
)}
</div>
Expand Down
29 changes: 17 additions & 12 deletions packages/ui/upload/src/FileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const fileTypeMap = {
} as Record<string, any>

export const FileList = forwardRef<HTMLUListElement | null, UploadFileList>(
({ prefixCls = UPLOAD_PREFIX, onDownload, onDelete, fileList, showPic, actionRender }, ref) => {
(
{ prefixCls = UPLOAD_PREFIX, onDownload, onDelete, fileList, showPic, actionRender, disabled },
ref
) => {
const handleItemKeydown = useCallback(
(e: React.KeyboardEvent<HTMLLIElement>, file: UploadFileItem, index: number) => {
// ENTER
Expand All @@ -55,17 +58,19 @@ export const FileList = forwardRef<HTMLUListElement | null, UploadFileList>(
// 如果 actionRender 返回 `true`,则使用默认 title
const action = actionRender ? actionRender({ file, index }) : true

return action === true ? (
<span className={`${prefixCls}__del-btn`}>
{file.uploadState === 'loading' ? (
<CloseOutlined onClick={() => onDelete(file, index)} />
) : (
<DeleteOutlined onClick={() => onDelete(file, index)} />
)}
</span>
) : (
action
)
return !disabled ? (
action === true ? (
<span className={`${prefixCls}__del-btn`}>
{file.uploadState === 'loading' ? (
<CloseOutlined onClick={() => onDelete(file, index)} />
) : (
<DeleteOutlined onClick={() => onDelete(file, index)} />
)}
</span>
) : (
action
)
) : null
}

return (
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/upload/src/NormalUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const NormalUpload = forwardRef<HTMLDivElement | null, UploadProps>(
beforeUpload,
customUpload,
method,
disabled,
})

return (
Expand Down Expand Up @@ -98,6 +99,7 @@ export const NormalUpload = forwardRef<HTMLDivElement | null, UploadProps>(
onDownload={onDownload}
prefixCls={prefixCls}
actionRender={actionRender}
disabled={disabled}
/>
)}
</div>
Expand Down
1 change: 1 addition & 0 deletions packages/ui/upload/src/PictureListUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const PictureListUpload = forwardRef<HTMLDivElement | null, UploadProps>(
onDownload={onDownload}
showPic
prefixCls={prefixCls}
disabled={disabled}
/>
)}
</div>
Expand Down
81 changes: 44 additions & 37 deletions packages/ui/upload/src/PictureUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ export const PictureUpload = forwardRef<HTMLDivElement | null, UploadProps>(

const uploadSuccessText = i18n.get('upload.uploadSuccess')

const cls = cx(prefixCls, `${prefixCls}--photo`, className)
const cls = cx(
prefixCls,
`${prefixCls}--photo`,
disabled && `${prefixCls}--disabled`,
className
)

const [_fileList, uploadFiles, deleteFile] = useUpload({
fileList,
Expand All @@ -81,6 +86,7 @@ export const PictureUpload = forwardRef<HTMLDivElement | null, UploadProps>(
beforeUpload,
customUpload,
method,
disabled,
})

const uploadRef = useRef<HTMLLIElement>(null)
Expand Down Expand Up @@ -188,45 +194,48 @@ export const PictureUpload = forwardRef<HTMLDivElement | null, UploadProps>(
className={cx(`${prefixCls}__item`, `${prefixCls}__item--${photoSize}`, {
[`${prefixCls}__item--error`]: file.uploadState === 'error',
})}
onClick={() => previewImage(index)}
onKeyDown={(e) => handleItemKeydown(e, file, index)}
>
<img src={file.url} className={`${prefixCls}__thumb`} />
{file.uploadState !== 'error' ? (
<div className={`${prefixCls}__mask`}>
<div className={`${prefixCls}__action-group`}>
<span className={`${prefixCls}__action-btn`}>
<EyeOutlined
onClick={() => {
previewImage(index)
}}
/>
{!disabled ? (
file.uploadState !== 'error' ? (
<div className={`${prefixCls}__mask`}>
<div className={`${prefixCls}__action-group`}>
<span className={`${prefixCls}__action-btn`}>
<EyeOutlined
onClick={() => {
previewImage(index)
}}
/>
</span>
<span className={`${prefixCls}__action-btn`}>
<DeleteOutlined
onClick={(e) => {
e.stopPropagation()
deleteFile(file, index)
}}
/>
</span>
</div>
</div>
) : (
<div className={`${prefixCls}__percent`}>
<span className={`${prefixCls}__error-btn`}>
<ExclamationCircleOutlined />
</span>
<span className={`${prefixCls}__action-btn`}>
<DeleteOutlined
onClick={(e) => {
e.stopPropagation()
deleteFile(file, index)
}}
/>
<span
className={cx(`${prefixCls}__delete-btn`)}
onClick={(e) => {
e.stopPropagation()
deleteFile(file, index)
}}
>
<CloseCircleOutlined />
</span>
</div>
</div>
) : (
<div className={`${prefixCls}__percent`}>
<span className={`${prefixCls}__error-btn`}>
<ExclamationCircleOutlined />
</span>
<span
className={cx(`${prefixCls}__delete-btn`)}
onClick={(e) => {
e.stopPropagation()
deleteFile(file, index)
}}
>
<CloseCircleOutlined />
</span>
</div>
)}
)
) : null}
</li>
)
}
Expand All @@ -250,9 +259,7 @@ export const PictureUpload = forwardRef<HTMLDivElement | null, UploadProps>(
onKeyDown={handleUploadKeydown}
ref={uploadRef}
>
<label style={{ display: 'block', cursor: 'pointer' }}>
<PlusOutlined />
</label>
<PlusOutlined />
</li>
) : (
children
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/upload/src/hooks/use-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const useUpload = ({
maxCount,
method = 'POST',
accept,
disabled,
}: UploadProps): [
UploadFileItem[],
(files: HTMLInputElement['files']) => Promise<void>,
Expand All @@ -43,6 +44,9 @@ const useUpload = ({

const deleteFile = useCallback(
(file: UploadFileItem, index) => {
if (disabled) {
return
}
if (file.abort) {
file.abort()
}
Expand Down
11 changes: 4 additions & 7 deletions packages/ui/upload/src/styles/upload.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
$prefix: '#{$component-prefix}-upload' !default;

.#{$prefix} {
&--disabled {
cursor: not-allowed;
}

&__tips {
font-size: use-text-size('sm');
color: use-color('gray', 500);
Expand Down Expand Up @@ -213,7 +209,7 @@ $prefix: '#{$component-prefix}-upload' !default;
.drag-upload__title {
font-size: use-text-size('normal');

[class^=hi-v4-icon] {
[class^='hi-v4-icon'] {
font-size: use-text-size('lg');
margin-right: use-spacing(4);
}
Expand Down Expand Up @@ -242,11 +238,12 @@ $prefix: '#{$component-prefix}-upload' !default;
&--avatar,
&--photo {
&.#{$prefix}--disabled {
.photo-upload {
.#{$prefix}__item--upload {
cursor: not-allowed;

&,
&:hover {
&:hover,
&:focus {
border-color: use-color('gray', 300);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/upload/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface UploadFileList {
onDownload: UploadProps['onDownload']
showPic?: boolean
actionRender?: (props: ActionRenderProps) => React.ReactNode
disabled?: boolean
}

export interface UploadRequestOption {
Expand Down
Loading