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(grid): click on a blank space to deselect #WIK-16181 #31

Merged
merged 3 commits into from
Aug 5, 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
9 changes: 9 additions & 0 deletions packages/grid/src/components/cell-editors/cell-editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
height: 100%;
}
}

.text-cell-editor {
display: block;

textarea {
min-height: 44px;
resize: none;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NgIf } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, inject, Renderer2 } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ThyInputDirective } from 'ngx-tethys/input';
import { ThyAutofocusDirective, ThyEnterDirective } from 'ngx-tethys/shared';
Expand All @@ -8,13 +8,54 @@ import { AbstractEditCellEditor } from '../abstract-cell-editor.component';
@Component({
selector: 'text-cell-editor',
template: `
<input thyInput placeholder="" [thyAutofocus]="true" [(ngModel)]="modelValue" (thyEnter)="updateValue()" (blur)="updateValue()" />
<textarea
placeholder=""
thyInput
[thyAutofocus]="true"
[(ngModel)]="modelValue"
(ngModelChange)="valueChange()"
(thyEnter)="updateValue()"
(blur)="updateValue()"
></textarea>
`,
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [NgIf, FormsModule, ThyAutofocusDirective, ThyInputDirective, ThyEnterDirective]
imports: [NgIf, FormsModule, ThyAutofocusDirective, ThyInputDirective, ThyEnterDirective],
host: {
class: 'd-block'
}
})
export class TextCellEditorComponent extends AbstractEditCellEditor<string> {
export class TextCellEditorComponent extends AbstractEditCellEditor<string> implements AfterViewInit {
private elementRef = inject(ElementRef);

private render2 = inject(Renderer2);

private maxHeight = 148;

constructor() {
super();
}

ngAfterViewInit() {
setTimeout(() => {
this.updateStyle();
});
}

updateStyle() {
const textarea = this.elementRef.nativeElement.querySelector('textarea');
const height = textarea.scrollHeight <= this.maxHeight ? textarea.scrollHeight : this.maxHeight;

this.render2.setStyle(textarea, 'height', `${height}px`);
this.render2.setStyle(textarea, 'min-height', `44px`);
this.render2.setStyle(textarea, 'max-height', `${this.maxHeight}px`);
this.render2.setStyle(textarea, 'resize', 'none');
}

valueChange() {
this.updateStyle();
}

updateValue() {
this.updateFieldValue();
this.closePopover();
Expand Down
12 changes: 7 additions & 5 deletions packages/grid/src/grid.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ThyProgress } from 'ngx-tethys/progress';
import { ThyRate } from 'ngx-tethys/rate';
import { ThyStopPropagationDirective } from 'ngx-tethys/shared';
import { ThyTag } from 'ngx-tethys/tag';
import { mergeWith } from 'rxjs/operators';
import { ProgressEditorComponent } from './components';
import { FieldMenu } from './components/field-menu/field-menu.component';
import { AITableFieldPropertyEditor } from './components/field-property-editor/field-property-editor.component';
Expand Down Expand Up @@ -178,11 +179,12 @@ export class AITableGrid implements OnInit {
this.aiTableGridEventService.dblClickEvent$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => {
this.dblClick(event);
});
this.aiTableGridEventService.mousedownEvent$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => {
if ((event as MouseEvent)?.target) {
this.aiTableGridSelectionService.updateSelect(event as MouseEvent);
}
});
this.aiTableGridEventService.mousedownEvent$
.pipe(mergeWith(this.aiTableGridEventService.globalMousedownEvent$), takeUntilDestroyed(this.destroyRef))
.subscribe((event) => {
this.aiTableGridSelectionService.updateSelect(event);
});

this.aiTableGridEventService.mouseoverEvent$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => {
this.mouseoverHandle(event);
});
Expand Down
11 changes: 10 additions & 1 deletion packages/grid/src/services/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class AITableGridEventService {

globalMouseoverEvent$ = new Subject<MouseEvent>();

globalMousedownEvent$ = new Subject<MouseEvent>();

private destroyRef = inject(DestroyRef);

private thyPopover = inject(ThyPopover);
Expand Down Expand Up @@ -54,6 +56,12 @@ export class AITableGridEventService {
.subscribe((event) => {
this.mousedownEvent$.next(event);
});

fromEvent<MouseEvent>(document, 'mousedown')
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((event) => {
this.globalMousedownEvent$.next(event as MouseEvent);
});
}

private getEditorComponent(type: AITableFieldType) {
Expand Down Expand Up @@ -92,7 +100,8 @@ export class AITableGridEventService {
outsideClosable: false,
hasBackdrop: false,
manualClosure: true,
animationDisabled: true
animationDisabled: true,
autoAdaptive: true
});
return ref;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/grid/src/services/selection.servive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export class AITableGridSelectionService {
}

updateSelect(event: MouseEvent) {
const target = event.target as HTMLElement;
const target = event?.target as HTMLElement;
if (!target) {
return;
}

const cellDom = target.closest('.grid-cell');
const colDom = target.closest('.grid-field');
if (cellDom) {
Expand All @@ -64,5 +68,8 @@ export class AITableGridSelectionService {
const fieldId = colDom.getAttribute('fieldId');
fieldId && this.selectField(fieldId);
}
if (!cellDom && !colDom) {
this.clearSelection();
Copy link
Contributor

Choose a reason for hiding this comment

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

这里如果点击到的是行前方的复选框,清空是不是就有问题了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

没问题,document 的 mousedown 先执行,这时取消选中,然后执行复选框的 mousedown,又选上了

}
}
}