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

feat: support classNames and styles #518

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
28 changes: 20 additions & 8 deletions src/AjaxUploader.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint react/no-is-mounted:0,react/sort-comp:0,react/prop-types:0 */
import type { ReactElement } from 'react';
import React, { Component } from 'react';
import classNames from 'classnames';
import clsx from 'classnames';
import pickAttrs from 'rc-util/lib/pickAttrs';
import defaultRequest from './request';
import getUid from './uid';
Expand Down Expand Up @@ -264,9 +264,11 @@ class AjaxUploader extends Component<UploadProps> {
component: Tag,
prefixCls,
className,
classNames,
Wxh16144 marked this conversation as resolved.
Show resolved Hide resolved
disabled,
id,
style,
styles,
multiple,
accept,
capture,
Expand All @@ -277,11 +279,14 @@ class AjaxUploader extends Component<UploadProps> {
onMouseLeave,
...otherProps
} = this.props;
const cls = classNames({
[prefixCls]: true,
[`${prefixCls}-disabled`]: disabled,
[className]: className,
});
const cls = clsx(
className,
prefixCls,
{
[`${prefixCls}-disabled`]: disabled,
},
classNames?.wrapper,
Wxh16144 marked this conversation as resolved.
Show resolved Hide resolved
);
// because input don't have directory/webkitdirectory type declaration
const dirProps: any = directory
? { directory: 'directory', webkitdirectory: 'webkitdirectory' }
Expand All @@ -297,8 +302,14 @@ class AjaxUploader extends Component<UploadProps> {
onDragOver: this.onFileDrop,
tabIndex: '0',
};

const mergedWrapperStyle: React.CSSProperties = {
...style,
...styles?.wrapper,
};

return (
<Tag {...events} className={cls} role="button" style={style}>
<Tag {...events} className={cls} role="button" style={mergedWrapperStyle}>
<input
{...pickAttrs(otherProps, { aria: true, data: true })}
id={id}
Expand All @@ -307,7 +318,8 @@ class AjaxUploader extends Component<UploadProps> {
ref={this.saveFileInput}
onClick={e => e.stopPropagation()} // https://github.com/ant-design/ant-design/issues/19948
key={this.state.uid}
style={{ display: 'none' }}
style={styles?.input ?? { display: 'none' }}
Wxh16144 marked this conversation as resolved.
Show resolved Hide resolved
className={classNames?.input}
accept={accept}
{...dirProps}
multiple={multiple}
Expand Down
8 changes: 8 additions & 0 deletions src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export interface UploadProps
onMouseEnter?: (e: React.MouseEvent<HTMLDivElement>) => void;
onMouseLeave?: (e: React.MouseEvent<HTMLDivElement>) => void;
onClick?: (e: React.MouseEvent<HTMLDivElement> | React.KeyboardEvent<HTMLDivElement>) => void;
classNames?: {
wrapper?: string;
input?: string;
};
styles?: {
wrapper?: React.CSSProperties;
input?: React.CSSProperties;
};
}

export interface UploadProgressEvent extends Partial<ProgressEvent> {
Expand Down
20 changes: 20 additions & 0 deletions tests/uploader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,24 @@ describe('uploader', () => {

expect(requests[0].url).toEqual('bamboo');
});

it('input style defaults to display none', () => {
const wrapper = mount(<Uploader />);
expect(wrapper.find('input').props().style.display).toBe('none');
});

it('classNames and styles should work', () => {
const wrapper = mount(
<Uploader
classNames={{ wrapper: 'bamboo', input: 'bamboo-input' }}
styles={{ wrapper: { height: 100 }, input: { color: 'red' } }}
/>,
);
expect(wrapper.find('.bamboo').length).toBeTruthy();
expect(wrapper.find('.bamboo-input').length).toBeTruthy();

expect(wrapper.find('.bamboo').props().style.height).toEqual(100);
expect(wrapper.find('.bamboo-input').props().style.color).toEqual('red');
expect(wrapper.find('input').props().style.display).not.toBe('none');
});
});