Skip to content

Commit

Permalink
Refactor #1876
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Apr 13, 2021
1 parent d33107e commit 61d8dcc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
13 changes: 12 additions & 1 deletion src/components/button/Button.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ import TooltipOptions from '../tooltip/TooltipOptions';

declare namespace Button {

type PositionType = 'top' | 'bottom' | 'left' | 'right';

type LoadingIconType = React.ReactNode | ((props: ButtonProps) => React.ReactNode);

interface LoadingOption {
icon: LoadingIconType,
position: PositionType
}

interface ButtonProps extends React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement> {
label?: string;
icon?: string;
iconPos?: string;
iconPos?: PositionType;
badge?: string;
badgeClassName?: string;
tooltip?: string;
tooltipOptions?: TooltipOptions;
loading?: boolean;
loadingOptions?: LoadingOptions;
}
}

Expand Down
58 changes: 25 additions & 33 deletions src/components/button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export class ButtonComponent extends Component {
tooltip: null,
tooltipOptions: null,
forwardRef: null,
loading: false
loading: false,
loadingOptions: {
icon: 'pi pi-spin pi-spinner',
position: 'left'
}
}

static propTypes = {
Expand All @@ -28,7 +32,8 @@ export class ButtonComponent extends Component {
tooltip: PropTypes.string,
tooltipOptions: PropTypes.object,
forwardRef: PropTypes.any,
loading: PropTypes.bool
loading: PropTypes.bool,
loadingOptions: PropTypes.any
};

getElementRef(el) {
Expand Down Expand Up @@ -76,18 +81,22 @@ export class ButtonComponent extends Component {
});
}

renderIcon() {
if (this.props.icon) {
let className = classNames(this.props.icon, 'p-c', {
'p-button-icon-left': this.props.iconPos === 'left' && this.props.label,
'p-button-icon-right': this.props.iconPos === 'right' && this.props.label,
'p-button-icon-top': this.props.iconPos === 'top' && this.props.label,
'p-button-icon-bottom': this.props.iconPos === 'bottom' && this.props.label
});

return (
<span className={className}></span>
);
renderIcon(icon, position) {
if (icon) {
if (typeof icon === 'string') {
let className = classNames(icon, 'p-c', {
'p-button-icon-left': position === 'left' && this.props.label,
'p-button-icon-right': position === 'right' && this.props.label,
'p-button-icon-top': position === 'top' && this.props.label,
'p-button-icon-bottom': position === 'bottom' && this.props.label
});

return (
<span className={className}></span>
);
}

return ObjectUtils.getJSXElement(icon, this.props);
}

return null;
Expand All @@ -111,33 +120,16 @@ export class ButtonComponent extends Component {
return null;
}

renderLoading() {
if (this.props.loading) {
let className = classNames('pi pi-spin pi-spinner', 'p-c', {
'p-button-icon-left': this.props.iconPos === 'left' && this.props.label,
'p-button-icon-right': this.props.iconPos === 'right' && this.props.label,
'p-button-icon-top': this.props.iconPos === 'top' && this.props.label,
'p-button-icon-bottom': this.props.iconPos === 'bottom' && this.props.label
});

return (
<span className={className}></span>
);
}

return null;
}

render() {
let className = classNames('p-button p-component', this.props.className, {
'p-button-icon-only': this.props.icon && !this.props.label,
'p-button-vertical': (this.props.iconPos === 'top' || this.props.iconPos === 'bottom') && this.label,
'p-disabled': this.props.disabled
});
let icon = this.renderIcon();
let icon = this.renderIcon(this.props.icon, this.props.iconPos);
let label = this.renderLabel();
let badge = this.renderBadge();
let loading = this.renderLoading();
let loading = this.props.loading && this.renderIcon(this.props.loadingOptions.icon, this.props.loadingOptions.position);

let buttonProps = ObjectUtils.findDiffKeys(this.props, ButtonComponent.defaultProps);

Expand Down

0 comments on commit 61d8dcc

Please sign in to comment.