Skip to content

Commit

Permalink
refactor(disabled): create a disabled mixin to reuse common disabled …
Browse files Browse the repository at this point in the history
…behavior (#736)

* refactor(disabled): create a disabled mixin to reuse common disabled behavior

this is the first of many since we need to refactor all common behaviors to centrilize them and make them more reusable.

potential mixins could be color, disableRipple, multiple, etc etc)

* chore(): polish disabled mixin a bit more

make it so its easier to check for boolean changes on its input

* *DEPRECATE* feat(chips): rename readOnly to disabled and reuse disabled mixin
  • Loading branch information
emoralesb05 authored Jul 6, 2017
1 parent 91fb852 commit e07e8e8
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 129 deletions.
10 changes: 5 additions & 5 deletions src/app/components/components/chips/chips.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[items]="filteredStrings"
[(ngModel)]="stringsModel"
placeholder="Enter autocomplete strings"
[readOnly]="readOnly"
[disabled]="disabled"
(inputChange)="filterStrings($event)"
requireMatch>
</td-chips>
Expand All @@ -29,7 +29,7 @@
[items]="filteredStrings"
[(ngModel)]="stringsModel"
placeholder="Enter autocomplete strings"
[readOnly]="readOnly"
[disabled]="disabled"
(inputChange)="filterStrings($event)"
requireMatch>
</td-chips>
Expand All @@ -39,7 +39,7 @@
<td-highlight lang="typescript">
<![CDATA[
export class ChipsDemoComponent {
readOnly: boolean = false;
disabled: boolean = false;
chipAddition: boolean = true;
chipRemoval: boolean = true;

Expand Down Expand Up @@ -85,8 +85,8 @@
<md-divider></md-divider>
<md-card-actions>
<div class="pad-sm">
<md-slide-toggle color="accent" [(ngModel)]="readOnly">
Read only
<md-slide-toggle color="accent" [(ngModel)]="disabled">
Disabled
</md-slide-toggle>
</div>
<md-divider></md-divider>
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/components/chips/chips.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ChipsDemoComponent implements OnInit {
@HostBinding('@routeAnimation') routeAnimation: boolean = true;
@HostBinding('class.td-route-animation') classAnimation: boolean = true;

readOnly: boolean = false;
disabled: boolean = false;
chipAddition: boolean = true;
chipRemoval: boolean = true;

Expand Down
7 changes: 4 additions & 3 deletions src/platform/core/chips/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Properties:
| `requireMatch?` | `boolean` | Blocks custom inputs and only allows selections from the autocomplete list.
| `stacked?` | `boolean` | Set stacked or horizontal chips depending on value. Defaults to false.
| `placeholder?` | `string` | Placeholder for the autocomplete input.
| `chipAddition` | `boolean` | Disables the ability to add chips. When setting readOnly as true, this will be overriden. Defaults to true.
| `chipRemoval` | `boolean` | Disables the ability to remove chips. When setting readOnly as true, this will be overriden. Defaults to true.
| `disabled?` | `boolean` | Sets disabled state and disabled addition/removal of chips.
| `chipAddition` | `boolean` | Disables the ability to add chips. When setting disabled as true, this will be overriden. Defaults to true.
| `chipRemoval` | `boolean` | Disables the ability to remove chips. When setting disabled as true, this will be overriden. Defaults to true.
| `debounce` | `string` | Debounce timeout between keypresses. Defaults to 200.
| `add?` | `function` | Method to be executed when a chip is added. Sends chip value as event.
| `remove?` | `function` | Method to be executed when a chip is removed. Sends chip value as event.
Expand Down Expand Up @@ -49,7 +50,7 @@ Example for HTML usage:
color="primary"
[items]="items"
[(ngModel)]="model"
[readOnly]="readOnly"
[disabled]="disabled"
[chipAddition]="chipAddition"
[chipRemoval]="chipRemoval"
(add)="addEvent($event)"
Expand Down
4 changes: 2 additions & 2 deletions src/platform/core/chips/chips.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="td-chips-wrapper" [class.td-chips-stacked]="stacked">
<ng-template let-chip let-first="first" let-index="index" ngFor [ngForOf]="value">
<md-basic-chip [class.td-chip-disabled]="readOnly"
<md-basic-chip [class.td-chip-disabled]="disabled"
[class.td-chip-after-pad]="!canRemoveChip"
[color]="color"
(keydown)="_chipKeydown($event, index)"
Expand Down Expand Up @@ -48,7 +48,7 @@
</md-autocomplete>
</div>
<div *ngIf="chipAddition" class="mat-input-underline"
[class.mat-disabled]="readOnly">
[class.mat-disabled]="disabled">
<span class="mat-input-ripple"
[class.mat-focused]="focused"></span>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/platform/core/chips/chips.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ describe('Component: Chips', () => {

});

describe('chip removal usage, requires readOnly to be false: ', () => {
describe('chip removal usage, requires disabled to be false: ', () => {
let fixture: ComponentFixture<TdChipRemovalTestComponent>;
let input: DebugElement;
let chips: DebugElement;
Expand Down Expand Up @@ -668,7 +668,7 @@ describe('Component: Chips', () => {

// more a11y unit tests

// readOnly usage
// disabled usage

// chipAddition usage

Expand Down
44 changes: 27 additions & 17 deletions src/platform/core/chips/chips.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import 'rxjs/add/observable/timer';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/debounceTime';

import { ICanDisable, mixinDisabled } from '../common/common.module';

const noop: any = () => {
// empty method
};
Expand All @@ -36,18 +38,24 @@ export class TdAutocompleteOptionDirective extends TemplatePortalDirective {
}
}

class TdChipsBase {}

/* tslint:disable-next-line */
const _TdChipsMixinBase = mixinDisabled(TdChipsBase);

@Component({
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => TdChipsComponent),
multi: true,
}],
selector: 'td-chips',
inputs: ['disabled'],
styleUrls: ['./chips.component.scss' ],
templateUrl: './chips.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit, AfterViewInit, OnDestroy {
export class TdChipsComponent extends _TdChipsMixinBase implements ControlValueAccessor, DoCheck, OnInit, AfterViewInit, OnDestroy, ICanDisable {

private _outsideClickSubs: Subscription;

Expand All @@ -62,7 +70,6 @@ export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit,
private _length: number = 0;
private _stacked: boolean = false;
private _requireMatch: boolean = false;
private _readOnly: boolean = false;
private _color: 'primary' | 'accent' | 'warn' = 'primary';
private _chipAddition: boolean = true;
private _chipRemoval: boolean = true;
Expand Down Expand Up @@ -133,21 +140,18 @@ export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit,
}

/**
* @deprecated [email protected]
* readOnly?: boolean
* Disables the chips input and chip removal icon.
*/
@Input('readOnly')
set readOnly(readOnly: boolean) {
this._readOnly = readOnly;
this._toggleInput();
}
get readOnly(): boolean {
return this._readOnly;
this.disabled = readOnly;
}

/**
* chipAddition?: boolean
* Disables the ability to add chips. When setting readOnly as true, this will be overriden.
* Disables the ability to add chips. When setting disabled as true, this will be overriden.
* Defaults to true.
*/
@Input('chipAddition')
Expand All @@ -160,17 +164,17 @@ export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit,
}

/**
* Checks if not in readOnly state and if chipAddition is set to 'true'
* Checks if not in disabled state and if chipAddition is set to 'true'
* States if a chip can be added and if the input is available
*/
get canAddChip(): boolean {
return this.chipAddition && !this.readOnly;
return this.chipAddition && !this.disabled;
}

/**
* chipRemoval?: boolean
* Disables the ability to remove chips. If it doesn't exist chip remmoval defaults to true.
* When setting readOnly as true, this will be overriden to false.
* When setting disabled as true, this will be overriden to false.
*/
@Input('chipRemoval')
set chipRemoval(chipRemoval: boolean) {
Expand All @@ -181,11 +185,11 @@ export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit,
}

/**
* Checks if not in readOnly state and if chipRemoval is set to 'true'
* Checks if not in disabled state and if chipRemoval is set to 'true'
* States if a chip can be removed
*/
get canRemoveChip(): boolean {
return this.chipRemoval && !this.readOnly;
return this.chipRemoval && !this.disabled;
}

/**
Expand Down Expand Up @@ -255,13 +259,14 @@ export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit,
*/
@HostBinding('attr.tabindex')
get tabIndex(): number {
return this.readOnly ? -1 : this._tabIndex;
return this.disabled ? -1 : this._tabIndex;
}

constructor(private _elementRef: ElementRef,
private _renderer: Renderer2,
private _changeDetectorRef: ChangeDetectorRef,
@Optional() @Inject(DOCUMENT) private _document: any) {
super();
this._renderer.addClass(this._elementRef.nativeElement, 'mat-' + this._color);
}

Expand Down Expand Up @@ -359,6 +364,11 @@ export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit,
}
}

/** Method executed when the disabled value changes */
onDisabledChange(v: boolean): void {
this._toggleInput();
}

/**
* Method that is executed when trying to create a new chip from the autocomplete.
* It check if [requireMatch] is enabled, and tries to add the first active option
Expand Down Expand Up @@ -462,7 +472,7 @@ export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit,
* Sets focus state of the component
*/
setFocusedState(): void {
if (!this.readOnly) {
if (!this.disabled) {
this._focused = true;
this._tabIndex = -1;
this._changeDetectorRef.markForCheck();
Expand All @@ -485,7 +495,7 @@ export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit,
focus(): void {
if (this.canAddChip) {
this._inputChild.focus();
} else if (!this.readOnly) {
} else if (!this.disabled) {
this._focusFirstChip();
}
}
Expand Down Expand Up @@ -664,7 +674,7 @@ export class TdChipsComponent implements ControlValueAccessor, DoCheck, OnInit,

/**
* Method to toggle the disable state of input
* Checks if not in readOnly state and if chipAddition is set to 'true'
* Checks if not in disabled state and if chipAddition is set to 'true'
*/
private _toggleInput(): void {
if (this.canAddChip) {
Expand Down
1 change: 1 addition & 0 deletions src/platform/core/common/behaviors/constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Constructor<T> = new (...args: any[]) => T;
36 changes: 36 additions & 0 deletions src/platform/core/common/behaviors/disabled.mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Constructor } from './constructor';

import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';

/** Interface to implement when applying the disabled mixin */
export interface ICanDisable {
disabled: boolean;
onDisabledChange(v: boolean): void;
}

/** Mixin to augment a component or directive with a `disabled` property. */
export function mixinDisabled<T extends Constructor<{}>>(base: T): Constructor<ICanDisable> & T {
return class extends base {
private _disabled: boolean = false;

constructor(...args: any[]) {
super(...args);
}

get disabled(): boolean {
return this._disabled;
}
set disabled(value: boolean) {
let newValue: boolean = <any>value !== '' ? (<any>value === 'true' || value === true) : true;
if (this._disabled !== newValue) {
this._disabled = newValue;
this.onDisabledChange(this._disabled);
}
}

onDisabledChange(v: boolean): void {
/** NOT IMPLEMENTED, this needs to be overriden by subclasses if needed */
}
};
}
6 changes: 6 additions & 0 deletions src/platform/core/common/common.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export { TdToggleDirective, TdFadeDirective };
export { TdCollapseAnimation } from './animations/collapse/collapse.animation';
export { TdFadeInOutAnimation } from './animations/fade/fadeInOut.animation';

/**
* BEHAVIORS
*/

export { ICanDisable, mixinDisabled } from './behaviors/disabled.mixin';

/**
* FORMS
*/
Expand Down
Loading

0 comments on commit e07e8e8

Please sign in to comment.