Browse Source

fix: Upload disabled state logic (#42143)

* fix: Upload disable state logic

* chore(typo): update

* test(upload): add case

* fix: Upload disable state logic

* test: remove case

* chore: remove unused

(cherry picked from commit 6d0acb0518)

# Conflicts:
#	components/upload/Upload.tsx
#	components/upload/UploadList/index.tsx

* chore: skip githook format

* test: update

* test: update

* chore: update
pull/42163/head
Wuxh 2 years ago
committed by GitHub
parent
commit
5e52057671
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      components/upload/Upload.tsx
  2. 2
      components/upload/UploadList/index.tsx
  3. 27
      components/upload/__tests__/uploadlist.test.tsx
  4. 4
      components/upload/interface.tsx

12
components/upload/Upload.tsx

@ -9,14 +9,19 @@ import DisabledContext from '../config-provider/DisabledContext';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale/default';
import warning from '../_util/warning';
import type { RcFile, ShowUploadListInterface, UploadChangeParam, UploadFile } from './interface';
import { UploadProps } from './interface';
import type {
RcFile,
ShowUploadListInterface,
UploadChangeParam,
UploadFile,
UploadProps,
} from './interface';
import UploadList from './UploadList';
import { file2Obj, getFileItem, removeFileItem, updateFileList } from './utils';
export const LIST_IGNORE = `__LIST_IGNORE_${Date.now()}__`;
export { UploadProps };
export type { UploadProps };
const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (props, ref) => {
const {
@ -368,6 +373,7 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
appendAction={button}
appendActionVisible={buttonVisible}
itemRender={itemRender}
disabled={mergedDisabled}
/>
);
}}

2
components/upload/UploadList/index.tsx

@ -49,6 +49,7 @@ const InternalUploadList: React.ForwardRefRenderFunction<unknown, UploadListProp
appendAction,
appendActionVisible = true,
itemRender,
disabled,
} = props;
const forceUpdate = useForceUpdate();
const [motionAppear, setMotionAppear] = React.useState(false);
@ -130,6 +131,7 @@ const InternalUploadList: React.ForwardRefRenderFunction<unknown, UploadListProp
type: 'text',
size: 'small',
title,
disabled,
onClick: (e: React.MouseEvent<HTMLElement>) => {
callback();
if (isValidElement(customIcon) && customIcon.props.onClick) {

27
components/upload/__tests__/uploadlist.test.tsx

@ -1541,4 +1541,31 @@ describe('Upload List', () => {
expect(container.querySelector('.ant-upload-list-item-error')).toBeTruthy();
});
// https://github.com/ant-design/ant-design/issues/42056
describe('when form is disabled but upload is not', () => {
it('should not disable remove button', () => {
const { container } = render(
<Form name="base" disabled>
<Form.Item name="upload">
<Upload
disabled={false}
defaultFileList={[
{
uid: '1',
name: 'zzz.png',
status: 'error',
url: 'http://www.baidu.com/zzz.png',
},
]}
/>
</Form.Item>
</Form>,
);
const removeButton = container.querySelector('.ant-upload-list-item-card-actions > button');
expect(removeButton).toBeTruthy();
expect(removeButton).not.toBeDisabled();
});
});
});

4
components/upload/interface.tsx

@ -159,4 +159,8 @@ export interface UploadListProps<T = any> {
appendAction?: React.ReactNode;
appendActionVisible?: boolean;
itemRender?: ItemRender<T>;
/**
* @internal Only the internal remove button is provided for use
*/
disabled?: boolean;
}

Loading…
Cancel
Save