Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 13 additions & 2 deletions src/AjaxUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,21 @@ class AjaxUploader extends Component<UploadProps> {
return;
}

const { onStart, customRequest, name, headers, withCredentials, method } = this.props;
const {
onStart,
customRequest: propsCustomRequest,
name,
headers,
withCredentials,
method,
} = this.props;

const { uid } = origin;
const request = customRequest || defaultRequest;

const request =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥不直接去 request 调用的地方改?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request = customRequest || defaultRequest;
如果在调用时候改,有可能 defaultRequest 的参数也加上了{defaultRequest}

typeof propsCustomRequest === 'function'
? args => propsCustomRequest(args, { defaultRequest })
: propsCustomRequest || defaultRequest;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了提高代码的简洁性和可读性,这里的 request 变量赋值逻辑可以进行简化。

考虑到 propsCustomRequest 的类型是函数或 undefined,我们可以使用更直接的三元运算符。这与现有逻辑在功能上是等价的,但结构更清晰。

    const request = propsCustomRequest
      ? args => propsCustomRequest(args, { defaultRequest })
      : defaultRequest;


const requestOption = {
action,
Expand Down
6 changes: 5 additions & 1 deletion src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface UploadProps
file: RcFile,
FileList: RcFile[],
) => BeforeUploadFileType | Promise<void | BeforeUploadFileType> | void;
customRequest?: (option: UploadRequestOption) => void | { abort: () => void };
customRequest?: CustomUploadRequestOption;
withCredentials?: boolean;
openFileDialogOnClick?: boolean;
prefixCls?: string;
Expand Down Expand Up @@ -78,6 +78,10 @@ export interface UploadRequestOption<T = any> {
method: UploadRequestMethod;
}

export type CustomUploadRequestOption = (
option: UploadRequestOption,
info: { defaultRequest: (option: UploadRequestOption) => { abort: () => void } | void },
) => void | { abort: () => void };
export interface RcFile extends File {
uid: string;
}
27 changes: 27 additions & 0 deletions tests/uploader.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1266,4 +1266,31 @@ describe('uploader', () => {
expect(container.querySelector('span')!.tabIndex).not.toBe(0);
expect(container.querySelector('span')!).not.toHaveAttribute('role', 'button');
});
it('should support defaultRequest in customRequest', done => {
const customRequest = jest.fn(({ file, onSuccess, onError }, { defaultRequest }) => {
Comment thread
EmilyyyLiu marked this conversation as resolved.
Outdated
if (file.name === 'success.png') {
defaultRequest({ file, onSuccess, onError });
} else {
onError(new Error('custom error'));
}
});
Comment thread
EmilyyyLiu marked this conversation as resolved.
Outdated
const onSuccess = jest.fn();
const onError = jest.fn();
const { container } = render(
<Upload customRequest={customRequest} onSuccess={onSuccess} onError={onError} />,
);
const input = container.querySelector('input')!;
const files = [new File([''], 'success.png', { type: 'image/png' })];
Object.defineProperty(files, 'item', {
value: i => files[i],
});
fireEvent.change(input, { target: { files } });

setTimeout(() => {
requests[0].respond(200, {}, `["","${files[0].name}"]`);
expect(customRequest).toHaveBeenCalled();
expect(onSuccess).toHaveBeenCalled();
done();
}, 100);
});
});