Skip to content

Commit

Permalink
fix: radio button can't inject radio group
Browse files Browse the repository at this point in the history
  • Loading branch information
fengtianze authored and Feng Tianze committed Apr 14, 2020
1 parent 523edb3 commit c0f8e07
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 80 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ on:
branches: [master]

jobs:
build:
deploy_stories:
name: Deploy stories
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ on:
types: [published]

jobs:
build:
publish_package:
name: Publish package
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: Run unit test
on: push

jobs:
build:
run_unit_test:
name: Run unit test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
86 changes: 86 additions & 0 deletions src/radio/base-radio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import {
AfterViewInit,
ChangeDetectorRef,
ElementRef,
Input,
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core';
import { BehaviorSubject, Subject, combineLatest } from 'rxjs';
import { map, takeUntil } from 'rxjs/operators';

import { RadioGroupComponent } from './radio-group/radio-group.component';

let uniqueId = 0;

export class BaseRadio implements OnInit, AfterViewInit, OnDestroy {
id = `aui-radio-${uniqueId++}`;

@Input()
disabled = false;

@Input()
get value() {
return this._value;
}

set value(val) {
this._value = val;
this.value$$.next(val);
}

@ViewChild('elRef', { static: true })
elRef: ElementRef;

checked = false;
name = '';

protected _value: any;
protected readonly value$$ = new BehaviorSubject<any>(this.value);
protected destroy$$ = new Subject<void>();

constructor(
protected radioGroup: RadioGroupComponent,
protected focusMonitor: FocusMonitor,
protected cdr: ChangeDetectorRef,
) {}

ngOnInit() {
this.radioGroup.name$.pipe(takeUntil(this.destroy$$)).subscribe(name => {
this.name = name;
this.cdr.markForCheck();
});

combineLatest([this.radioGroup.value$, this.value$$])
.pipe(
takeUntil(this.destroy$$),
map(([groupValue, selfValue]) => groupValue === selfValue),
)
.subscribe(checked => {
this.checked = checked;
this.cdr.markForCheck();
});
}

ngAfterViewInit() {
this.focusMonitor.monitor(this.elRef.nativeElement, true);
}

ngOnDestroy() {
this.destroy$$.next();
this.focusMonitor.stopMonitoring(this.elRef.nativeElement);
}

onClick() {
if (this.checked || this.disabled) {
return;
}
this.radioGroup.onRadioChange(this.value);
}

onBlur() {
this.radioGroup.onRadioBlur();
}
}
16 changes: 14 additions & 2 deletions src/radio/radio-button/radio-button.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
OnInit,
ViewEncapsulation,
} from '@angular/core';
import { takeUntil } from 'rxjs/operators';

import { Bem, buildBem } from '../../utils/bem';
import { RadioComponent } from '../radio.component';
import { BaseRadio } from '../base-radio';
import { RadioGroupComponent } from '../radio-group/radio-group.component';
import { RadioSize } from '../radio.types';

@Component({
Expand All @@ -18,7 +21,7 @@ import { RadioSize } from '../radio.types';
changeDetection: ChangeDetectionStrategy.OnPush,
preserveWhitespaces: false,
})
export class RadioButtonComponent extends RadioComponent implements OnInit {
export class RadioButtonComponent extends BaseRadio implements OnInit {
bem: Bem = buildBem('aui-radio-button');

size = RadioSize.Medium;
Expand All @@ -30,6 +33,15 @@ export class RadioButtonComponent extends RadioComponent implements OnInit {
} ${this.isPlain ? 'isPlain' : ''}`;
}

// eslint-disable-next-line @typescript-eslint/no-useless-constructor
constructor(
radioGroup: RadioGroupComponent,
focusMonitor: FocusMonitor,
cdr: ChangeDetectorRef,
) {
super(radioGroup, focusMonitor, cdr);
}

ngOnInit() {
super.ngOnInit();
this.radioGroup.size$.pipe(takeUntil(this.destroy$$)).subscribe(size => {
Expand Down
83 changes: 8 additions & 75 deletions src/radio/radio.component.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import { FocusMonitor } from '@angular/cdk/a11y';
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Input,
OnDestroy,
OnInit,
ViewChild,
ViewEncapsulation,
} from '@angular/core';
import { BehaviorSubject, Subject, combineLatest } from 'rxjs';
import { map, takeUntil } from 'rxjs/operators';

import { Bem, buildBem } from '../utils/bem';

import { BaseRadio } from './base-radio';
import { RadioGroupComponent } from './radio-group/radio-group.component';

let uniqueId = 0;

@Component({
selector: 'aui-radio',
templateUrl: './radio.component.html',
Expand All @@ -28,79 +19,21 @@ let uniqueId = 0;
changeDetection: ChangeDetectionStrategy.OnPush,
preserveWhitespaces: false,
})
export class RadioComponent implements OnInit, AfterViewInit, OnDestroy {
id = `aui-radio-${uniqueId++}`;
export class RadioComponent extends BaseRadio {
bem: Bem = buildBem('aui-radio');

@Input()
disabled = false;

@Input()
get value() {
return this._value;
}

set value(val) {
this._value = val;
this.value$$.next(val);
}

@ViewChild('elRef', { static: true })
elRef: ElementRef;

checked = false;
name = '';

private _value: any;
private readonly value$$ = new BehaviorSubject<any>(this.value);
protected destroy$$ = new Subject<void>();

get rootClass() {
return `${this.bem.block()} ${this.disabled ? 'isDisabled' : ''} ${
this.checked ? 'isChecked' : ''
}`;
}

// eslint-disable-next-line @typescript-eslint/no-useless-constructor
constructor(
protected cdr: ChangeDetectorRef,
protected radioGroup: RadioGroupComponent,
protected focusMonitor: FocusMonitor,
) {}

ngOnInit() {
this.radioGroup.name$.pipe(takeUntil(this.destroy$$)).subscribe(name => {
this.name = name;
this.cdr.markForCheck();
});

combineLatest([this.radioGroup.value$, this.value$$])
.pipe(
takeUntil(this.destroy$$),
map(([groupValue, selfValue]) => groupValue === selfValue),
)
.subscribe(checked => {
this.checked = checked;
this.cdr.markForCheck();
});
}

ngAfterViewInit() {
this.focusMonitor.monitor(this.elRef.nativeElement, true);
}

ngOnDestroy() {
this.destroy$$.next();
this.focusMonitor.stopMonitoring(this.elRef.nativeElement);
}

onClick() {
if (this.checked || this.disabled) {
return;
}
this.radioGroup.onRadioChange(this.value);
}

onBlur() {
this.radioGroup.onRadioBlur();
radioGroup: RadioGroupComponent,
focusMonitor: FocusMonitor,
cdr: ChangeDetectorRef,
) {
super(radioGroup, focusMonitor, cdr);
}
}
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"paths": {
"@alauda/ui": ["src/public-api"]
}
},
"exclude": ["./release"],
"angularCompilerOptions": {
"enableIvy": false
}
}

0 comments on commit c0f8e07

Please sign in to comment.