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(module:space): add space compact component #8755

Merged
merged 1 commit into from
Nov 22, 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
3 changes: 3 additions & 0 deletions components/button/button-group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { takeUntil } from 'rxjs/operators';

export type NzButtonGroupSize = 'large' | 'default' | 'small';

/**
* @deprecated Will be removed in v20. Use `NzSpaceCompactComponent` instead.
*/
@Component({
selector: 'nz-button-group',
exportAs: 'nzButtonGroup',
Expand Down
54 changes: 35 additions & 19 deletions components/button/button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,35 @@ import {
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Renderer2,
SimpleChanges,
ViewEncapsulation,
booleanAttribute
booleanAttribute,
computed,
inject,
signal
} from '@angular/core';
import { Subject, fromEvent } from 'rxjs';
import { filter, startWith, takeUntil } from 'rxjs/operators';

import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { NzDestroyService } from 'ng-zorro-antd/core/services';
import { NzSizeLDSType } from 'ng-zorro-antd/core/types';
import { NzIconDirective, NzIconModule } from 'ng-zorro-antd/icon';
import { NZ_SPACE_COMPACT_ITEM_TYPE, NZ_SPACE_COMPACT_SIZE, NzSpaceCompactItemDirective } from 'ng-zorro-antd/space';

export type NzButtonType = 'primary' | 'default' | 'dashed' | 'link' | 'text' | null;
export type NzButtonShape = 'circle' | 'round' | null;
export type NzButtonSize = 'large' | 'default' | 'small';
export type NzButtonSize = NzSizeLDSType;

const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'button';

@Component({
selector: 'button[nz-button], a[nz-button]',
exportAs: 'nzButton',
standalone: true,
imports: [NzIconModule],
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
Expand All @@ -55,8 +62,8 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'button';
'[class.ant-btn-text]': `nzType === 'text'`,
'[class.ant-btn-circle]': `nzShape === 'circle'`,
'[class.ant-btn-round]': `nzShape === 'round'`,
'[class.ant-btn-lg]': `nzSize === 'large'`,
'[class.ant-btn-sm]': `nzSize === 'small'`,
'[class.ant-btn-lg]': `finalSize() === 'large'`,
'[class.ant-btn-sm]': `finalSize() === 'small'`,
'[class.ant-btn-dangerous]': `nzDanger`,
'[class.ant-btn-loading]': `nzLoading`,
'[class.ant-btn-background-ghost]': `nzGhost`,
Expand All @@ -67,10 +74,10 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'button';
'[attr.tabindex]': 'disabled ? -1 : (tabIndex === null ? null : tabIndex)',
'[attr.disabled]': 'disabled || null'
},
imports: [NzIconModule],
standalone: true
hostDirectives: [NzSpaceCompactItemDirective],
providers: [NzDestroyService, { provide: NZ_SPACE_COMPACT_ITEM_TYPE, useValue: 'btn' }]
})
export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, AfterContentInit, OnInit {
export class NzButtonComponent implements OnChanges, AfterViewInit, AfterContentInit, OnInit {
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;

@ContentChild(NzIconDirective, { read: ElementRef }) nzIconDirectiveElement!: ElementRef;
Expand All @@ -85,7 +92,17 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
@Input() nzShape: NzButtonShape = null;
@Input() @WithConfig() nzSize: NzButtonSize = 'default';
dir: Direction = 'ltr';
private destroy$ = new Subject<void>();

protected finalSize = computed(() => {
if (this.compactSize) {
return this.compactSize();
}
return this.size();
});

private size = signal<NzSizeLDSType>(this.nzSize);
private compactSize = inject(NZ_SPACE_COMPACT_SIZE, { optional: true });
private destroy$ = inject(NzDestroyService);
private loading$ = new Subject<boolean>();

insertSpan(nodes: NodeList, renderer: Renderer2): void {
Expand Down Expand Up @@ -117,16 +134,18 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
private renderer: Renderer2,
public nzConfigService: NzConfigService,
private directionality: Directionality
) {
) {}

ngOnInit(): void {
this.size.set(this.nzSize);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

此处用于同步来自 @WithConfig 第一次设置的值

this.nzConfigService
.getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME)
.pipe(takeUntil(this.destroy$))
.subscribe(() => {
this.size.set(this.nzSize);
this.cdr.markForCheck();
});
}

ngOnInit(): void {
this.directionality.change?.pipe(takeUntil(this.destroy$)).subscribe((direction: Direction) => {
this.dir = direction;
this.cdr.detectChanges();
Expand All @@ -150,11 +169,13 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
});
}

ngOnChanges(changes: SimpleChanges): void {
const { nzLoading } = changes;
ngOnChanges({ nzLoading, nzSize }: SimpleChanges): void {
if (nzLoading) {
this.loading$.next(this.nzLoading);
}
if (nzSize) {
this.size.set(nzSize.currentValue);
}
}

ngAfterViewInit(): void {
Expand All @@ -177,9 +198,4 @@ export class NzButtonComponent implements OnDestroy, OnChanges, AfterViewInit, A
}
});
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}
4 changes: 4 additions & 0 deletions components/button/demo/button-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ title:

通过设置 `nzSize` 为 `large` `small` 分别把按钮组合设为大、小尺寸。若不设置 `nzSize`,则尺寸为中。

警告:`<nz-button-group>` 在 `v19` 中被弃用,请使用 `<nz-space-compact>` 组件替代。

## en-US

Buttons can be grouped by placing multiple `nz-button` components into a `nz-button-group`.

The `nzSize` can be set to `large`, `small` or left unset resulting in a default size.

Warning: `<nz-button-group>` is deprecated in `v19`, please use `<nz-space-compact>` instead.
4 changes: 2 additions & 2 deletions components/button/style/space-compact.less
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

// Special styles for Primary Button
&-compact-item.@{btn-prefix-cls}-primary {
&:not([disabled]) + &:not([disabled]) {
&:not([disabled]) + &:not([disabled]):not([ant-click-animating-without-extra-node='true']) {
Copy link
Collaborator Author

@HyperLife1119 HyperLife1119 Oct 18, 2024

Choose a reason for hiding this comment

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

由于 antd4 遗留的样式 bug,导致按钮点击的 wave 效果显示错误

QQ_1729239613460

这里通过排除 animating-node 来解决这个问题。

position: relative;

&::after {
Expand Down Expand Up @@ -69,7 +69,7 @@
// Special styles for Primary Button
&-compact-vertical-item {
&.@{btn-prefix-cls}-primary {
&:not([disabled]) + &:not([disabled]) {
&:not([disabled]) + &:not([disabled]):not([ant-click-animating-without-extra-node='true']) {
Copy link
Collaborator Author

@HyperLife1119 HyperLife1119 Oct 18, 2024

Choose a reason for hiding this comment

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

同样的,由于 antd4 遗留的样式 bug,导致按钮点击的 wave 效果显示错误

QQ_1729239652627

这里通过排除 animating-node 来解决这个问题。

position: relative;

&::after {
Expand Down
Loading
Loading