Skip to content

Commit

Permalink
feat: common form control set snapshot by default
Browse files Browse the repository at this point in the history
  • Loading branch information
fengtianze authored and Feng Tianze committed Apr 24, 2020
1 parent 0370784 commit a5782a5
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Run unit test

on: [push, pull_request]
on: pull_request

jobs:
run_unit_test:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"lint:ts": "tslint -p . -t stylish",
"lint:tsc": "tsc --incremental false --noEmit",
"postinstall": "yarn-deduplicate || exit 0",
"release": "standard-version --no-verify",
"release": "standard-version --no-verify --release-as",
"start": "yarn storybook",
"storybook": "start-storybook -c .storybook -p 9001",
"storybook:build": "build-storybook -c .storybook -o dist",
Expand Down
2 changes: 1 addition & 1 deletion src/checkbox/checkbox-group/checkbox-group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class CheckboxGroupComponent extends CommonFormControl<any[]> {
}
const values = this.checkboxes
.filter(item =>
item === checkbox ? !item.snapshot.checked : item.snapshot.checked,
item === checkbox ? !item.snapshot.value : item.snapshot.value,
)
.map(item => item.label);
this.emitValueChange(values);
Expand Down
16 changes: 4 additions & 12 deletions src/checkbox/checkbox.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
forwardRef,
} from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { BehaviorSubject, Observable, Subject, combineLatest } from 'rxjs';
import { map, takeUntil, tap } from 'rxjs/operators';
import { BehaviorSubject, Subject, combineLatest } from 'rxjs';
import { map, takeUntil } from 'rxjs/operators';

import { CommonFormControl } from '../form/public-api';

Expand Down Expand Up @@ -60,14 +60,6 @@ export class CheckboxComponent extends CommonFormControl<boolean>
@ViewChild('elRef', { static: true })
elRef: ElementRef;

value$: Observable<boolean> = this.value$$.asObservable().pipe(
tap(value => {
this.snapshot.checked = value;
}),
);

snapshot: { checked: boolean } = { checked: false };

private readonly checkboxGroup: CheckboxGroupComponent;
private _label: any;
private readonly label$$ = new BehaviorSubject<any>(this.label);
Expand All @@ -77,7 +69,7 @@ export class CheckboxComponent extends CommonFormControl<boolean>
cdr: ChangeDetectorRef,
@Optional()
@Inject(forwardRef(() => CheckboxGroupComponent))
checkboxGroup: any, // FIXME: workaround temporarily
checkboxGroup: CheckboxGroupComponent,
private readonly focusMonitor: FocusMonitor,
) {
super(cdr);
Expand Down Expand Up @@ -122,7 +114,7 @@ export class CheckboxComponent extends CommonFormControl<boolean>
if (this.disabled) {
return;
}
this.emitValueChange(!this.snapshot.checked);
this.emitValueChange(!this.snapshot.value);
if (this.checkboxGroup) {
this.checkboxGroup.onCheckboxChange(this);
}
Expand Down
21 changes: 16 additions & 5 deletions src/form/common-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import { Observable, ReplaySubject } from 'rxjs';
import { publishReplay, refCount, tap } from 'rxjs/operators';

import { coerceAttrBoolean } from '../utils/coercion';

Expand All @@ -16,7 +17,7 @@ import { coerceAttrBoolean } from '../utils/coercion';
*/
@Directive()
// tslint:disable-next-line:directive-class-suffix
export abstract class CommonFormControl<T> implements ControlValueAccessor {
export class CommonFormControl<T> implements ControlValueAccessor {
@Input()
get disabled() {
return this._disabled;
Expand Down Expand Up @@ -48,7 +49,15 @@ export abstract class CommonFormControl<T> implements ControlValueAccessor {
private _value: T;
private _disabled = false;

value$: Observable<T> = this.value$$.asObservable();
snapshot: { value: T } = { value: null };

value$: Observable<T> = this.value$$.asObservable().pipe(
tap(value => {
this.snapshot.value = value;
}),
publishReplay(1),
refCount(),
);

constructor(protected cdr: ChangeDetectorRef) {}

Expand All @@ -60,11 +69,11 @@ export abstract class CommonFormControl<T> implements ControlValueAccessor {
this.valueChange.emit(value);
}

registerOnChange(fn: (_: T) => void): void {
registerOnChange(fn: (_: T) => void) {
this.onChange = fn;
}

registerOnTouched(fn: () => void): void {
registerOnTouched(fn: () => void) {
this.onTouched = fn;
}

Expand All @@ -73,7 +82,9 @@ export abstract class CommonFormControl<T> implements ControlValueAccessor {
this.cdr.markForCheck();
}

abstract writeValue(value: T): void;
writeValue(value: T) {
this.value$$.next(value);
}
}

/**
Expand Down
14 changes: 3 additions & 11 deletions src/input/number-input/number-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
forwardRef,
} from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

import { CommonFormControl } from '../../form/public-api';
import { ComponentSize } from '../../types';
Expand Down Expand Up @@ -61,12 +59,6 @@ export class NumberInputComponent extends CommonFormControl<number> {

minDisabled = false;
maxDisabled = false;
inputValue: number;
value$: Observable<number> = this.value$$.asObservable().pipe(
tap(value => {
this.inputValue = value;
}),
);

constructor(protected cdr: ChangeDetectorRef) {
super(cdr);
Expand All @@ -75,9 +67,9 @@ export class NumberInputComponent extends CommonFormControl<number> {
changeHandle(event: KeyboardEvent) {
const inputEl = event.target as HTMLInputElement;
const value = inputEl.value;
inputEl.value = this.inputValue.toString();
inputEl.value = this.snapshot.value.toString();
if (Number.isNaN(+value)) {
this.emitValueChange(this.inputValue);
this.emitValueChange(this.snapshot.value);
return;
}
this.checkButtonAndSetValue(Number(value));
Expand All @@ -88,7 +80,7 @@ export class NumberInputComponent extends CommonFormControl<number> {
return;
}
const step: number = isPositive ? this.step : -this.step;
const val: number = step + this.inputValue;
const val: number = step + this.snapshot.value;
if (Number.isNaN(val)) {
return;
}
Expand Down
20 changes: 9 additions & 11 deletions src/input/tags-input/tags-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { publishReplay, refCount, tap } from 'rxjs/operators';

import { CommonFormControl } from '../../form/public-api';
import { ComponentSize } from '../../types';
Expand Down Expand Up @@ -63,15 +63,13 @@ export class TagsInputComponent extends CommonFormControl<string[]> {

value$: Observable<string[]> = this.value$$.asObservable().pipe(
tap(value => {
this.snapshot.tags = value;
this.snapshot.value = value;
this.clearInput();
}),
publishReplay(1),
refCount(),
);

snapshot: { tags: string[] } = {
tags: [],
};

focused = false;

inputValue = '';
Expand Down Expand Up @@ -100,7 +98,7 @@ export class TagsInputComponent extends CommonFormControl<string[]> {
}

get displayPlaceholder() {
return !this.snapshot.tags.length && !this.focused;
return !this.snapshot.value.length && !this.focused;
}

constructor(cdr: ChangeDetectorRef, private readonly renderer: Renderer2) {
Expand All @@ -112,7 +110,7 @@ export class TagsInputComponent extends CommonFormControl<string[]> {
}

onRemove(index: number) {
this.emitValueChange(this.snapshot.tags.filter((_, i) => i !== index));
this.emitValueChange(this.snapshot.value.filter((_, i) => i !== index));
}

onInput() {
Expand All @@ -130,7 +128,7 @@ export class TagsInputComponent extends CommonFormControl<string[]> {
onKeyDown(event: KeyboardEvent) {
const inputEl = event.target as HTMLInputElement;
if (event.key === 'Backspace' && inputEl.value === '') {
this.onRemove(this.snapshot.tags.length - 1);
this.onRemove(this.snapshot.value.length - 1);
event.stopPropagation();
event.preventDefault();
} else if (event.key === 'Enter') {
Expand Down Expand Up @@ -160,10 +158,10 @@ export class TagsInputComponent extends CommonFormControl<string[]> {
if (!this.allowEmpty && !value) {
return;
}
if (!this.allowRepeat && this.snapshot.tags.includes(value)) {
if (!this.allowRepeat && this.snapshot.value.includes(value)) {
return;
}
this.emitValueChange(this.snapshot.tags.concat(value));
this.emitValueChange(this.snapshot.value.concat(value));
}

private clearInput() {
Expand Down
12 changes: 1 addition & 11 deletions src/switch/switch.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
forwardRef,
} from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

import { CommonFormControl } from '../form/public-api';
import { Bem, buildBem } from '../utils/bem';
Expand Down Expand Up @@ -35,14 +33,6 @@ export class SwitchComponent extends CommonFormControl<boolean> {
@Input()
loading = false;

checked = false;

value$: Observable<boolean> = this.value$$.asObservable().pipe(
tap(value => {
this.checked = value;
}),
);

writeValue(value: boolean) {
this.value$$.next(value);
}
Expand All @@ -51,7 +41,7 @@ export class SwitchComponent extends CommonFormControl<boolean> {
if (this.disabled) {
return;
}
this.emitValueChange(!this.checked);
this.emitValueChange(!this.snapshot.value);
}

onBlur() {
Expand Down

0 comments on commit a5782a5

Please sign in to comment.