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: select all AIT-8411 #154

Merged
merged 3 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/friendly-beans-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@alauda/ui": patch
---

feat: select all AIT-8411
1 change: 1 addition & 0 deletions src/i18n/language/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export const en: I18NInterface = {
saturday: 'Sat',
sunday: 'Sun',
now: 'Now',
select_all: 'Select All',
},
};
1 change: 1 addition & 0 deletions src/i18n/language/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export const zh: I18NInterface = {
saturday: '六',
sunday: '日',
now: '此刻',
select_all: '全选',
},
};
28 changes: 28 additions & 0 deletions src/select/multi-select/multi-select.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@
(mousedown)="$event.preventDefault()"
>
<div #optionListRef class="aui-option-container__content">
<ng-container
*ngIf="
allowSelectAll &&
((hasVisibleOption$ | async) ||
(allowCreate && (customCreatedOptions$ | async).length) ||
(allowCreate && filterString))
"
>
<div
class="aui-option"
[class]="bemSelectAll.block(size$ | async)"
[class.isDisabled]="disabled"
[class.isSelected]="!!(selectAllStatus$ | async)"
[class.isMulti]="true"
(click)="onSelectAllClick()"
>
<i class="aui-option__pointer">
<aui-icon
[icon]="
(selectAllStatus$ | async) === 'checked' ? 'check_s' : 'minus_s'
"
[hidden]="!(selectAllStatus$ | async)"
></aui-icon>
</i>
{{ 'select_all' | auiI18n }}
</div>
<hr class="divider" />
</ng-container>
<aui-option
#inputtingOption
*ngIf="allowCreate && filterString && !(hasMatchedOption$ | async)"
Expand Down
3 changes: 3 additions & 0 deletions src/select/multi-select/multi-select.component.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
@import '../../theme/var';
@import '../../theme/mixin';
@import '../../input/tags-input/mixin';
@import '../option/option-style';
@import '../option-container';

@include option('aui-option');

@include option-container(aui-option-container);

$block: aui-multi-select;
Expand Down
68 changes: 66 additions & 2 deletions src/select/multi-select/multi-select.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AfterContentInit,
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Expand All @@ -21,14 +22,16 @@ import {
startWith,
switchMap,
takeUntil,
tap,
} from 'rxjs/operators';

import { createWithMaxRowCount } from '../../input/tags-input/with-max-row-count';
import { ComponentSize } from '../../types';
import { Bem, buildBem, coerceString } from '../../utils';
import { Bem, buildBem, coerceAttrBoolean, coerceString } from '../../utils';
import { BaseSelect } from '../base-select';
import { OptionComponent } from '../option/option.component';
import {
SelectAllStatus,
SelectFilterOption,
SelectPrimitiveValue,
TagClassFn,
Expand Down Expand Up @@ -59,10 +62,14 @@ import {
})
export class MultiSelectComponent<T = SelectPrimitiveValue>
extends BaseSelect<T, T[]>
implements AfterContentInit {
implements AfterContentInit, AfterViewInit {
bem: Bem = buildBem('aui-multi-select');
bemSelectAll: Bem = buildBem('aui-option');
JounQin marked this conversation as resolved.
Show resolved Hide resolved
selectedOptions$: Observable<Array<SelectFilterOption<T>>>;
selectAllStatus$: Observable<SelectAllStatus>;
selectAllStatus: SelectAllStatus;

private _allowSelectAll = false;
selectedValues: T[] = [];
values$ = this.value$$.asObservable();

Expand All @@ -75,6 +82,15 @@ export class MultiSelectComponent<T = SelectPrimitiveValue>
@Input()
customRowHeight = 0; // 0: use default style const value, > 1: for ```tagClassFn``` maybe affect lineHeight

@Input()
get allowSelectAll() {
return this._allowSelectAll;
}

set allowSelectAll(val: boolean | '') {
this._allowSelectAll = coerceAttrBoolean(val);
}

@ViewChild('inputRef', { static: true })
inputRef: ElementRef<HTMLInputElement>;

Expand Down Expand Up @@ -187,6 +203,34 @@ export class MultiSelectComponent<T = SelectPrimitiveValue>
);
}

ngAfterViewInit() {
super.ngAfterViewInit();
this.selectAllStatus$ = combineLatest([
this.allOptions$.pipe(startWith(this.contentOptions)),
this.value$,
this.filterString$,
]).pipe(
map(([allOptions]: [QueryList<OptionComponent<T>>, T[], string]) => {
const visibleOptions = allOptions.filter(({ visible }) => visible);
const checkedOptions = visibleOptions.filter(
({ selected }) => selected,
);
return checkedOptions.length &&
checkedOptions.length !== visibleOptions.length
? SelectAllStatus.Indeterminate
: checkedOptions.length === 0
? SelectAllStatus.Empty
: SelectAllStatus.Checked;
}),
startWith(SelectAllStatus.Empty),
tap(selectAllStatus => {
this.selectAllStatus = selectAllStatus;
}),
publishReplay(1),
refCount(),
);
}

onShowOptions() {
super.onShowOptions();
this.inputRef.nativeElement.focus();
Expand Down Expand Up @@ -290,6 +334,26 @@ export class MultiSelectComponent<T = SelectPrimitiveValue>
event.preventDefault();
}

onSelectAllClick() {
if (this.disabled) {
return;
}
const visibleOptionsValue = this.allOptions
.filter(({ visible }) => visible)
.map(({ value }) => value);
if (this.selectAllStatus === SelectAllStatus.Checked) {
this.emitValueChange(
this.snapshot.value.filter(
value => !visibleOptionsValue.includes(value),
),
);
} else {
this.emitValueChange([
...new Set(this.snapshot.value.concat(visibleOptionsValue)),
]);
}
}

private resetInput() {
this.inputRef.nativeElement.value = '';
this.setInputWidth();
Expand Down
5 changes: 5 additions & 0 deletions src/select/option-container.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@import '../theme/var';
@import '../theme/mixin';
@import './option-group/option-group-style.scss';

@mixin option-container($block) {
.#{$block} {
Expand All @@ -13,6 +14,10 @@
position: relative;
overflow: auto;
@include scroll-bar;
.divider {
border: none;
@include option-group-divider;
}
}

&__placeholder {
Expand Down
8 changes: 6 additions & 2 deletions src/select/option-group/option-group-style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
@import '../../theme/var';
@import '../../theme/mixin';

@mixin option-group-divider {
border-top: $menu-group-divide-width $menu-group-divide-style
$menu-group-divide-color;
}

@mixin option-group($block) {
.#{$block} {
&__title {
Expand All @@ -14,7 +19,6 @@
#{$block}:not(:first-child) .#{$block} {
margin-top: $menu-padding;
padding-top: $menu-padding;
border-top: $menu-group-divide-width $menu-group-divide-style
$menu-group-divide-color;
@include option-group-divider;
}
}
10 changes: 9 additions & 1 deletion src/select/select.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';

import { I18nModule } from '../i18n/public-api';
import { IconModule } from '../icon/public-api';
tunblr marked this conversation as resolved.
Show resolved Hide resolved
import { InputModule } from '../input/public-api';
import { TagModule } from '../tag/public-api';
Expand All @@ -18,7 +19,14 @@ import { SelectComponent } from './select.component';
import { IncludesDirective } from './validators';

@NgModule({
imports: [CommonModule, InputModule, IconModule, TooltipModule, TagModule],
imports: [
CommonModule,
InputModule,
I18nModule,
IconModule,
TooltipModule,
TagModule,
],
declarations: [
SelectComponent,
OptionComponent,
Expand Down
6 changes: 6 additions & 0 deletions src/select/select.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ export type TagClassFn<
value: V,
) => // tslint:disable-next-line: max-union-size
string | string[] | Set<string> | { [className: string]: unknown };

export enum SelectAllStatus {
Empty = '',
Checked = 'checked',
Indeterminate = 'indeterminate',
}
6 changes: 5 additions & 1 deletion stories/select/select.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ import { SelectModule, IconModule } from '@alauda/ui';
<Story name="multi" height="240px">
{{
template: /* HTML */ `
<aui-multi-select [(ngModel)]="value" [maxRowCount]="3">
<aui-multi-select
[(ngModel)]="value"
[maxRowCount]="3"
[allowSelectAll]="true"
>
<aui-option *ngFor="let option of arr" [value]="option">
{{ option }}
</aui-option>
Expand Down