Skip to content

Commit c63b9f4

Browse files
authored
chore: remove BooleanFieldValue (#1290)
1 parent 9eaf7e4 commit c63b9f4

File tree

14 files changed

+467
-439
lines changed

14 files changed

+467
-439
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
/libpeerconnection.log
3434
npm-debug.log
3535
testem.log
36+
/.chrome

src/lib/button-toggle/button-toggle.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@ import {
1515
forwardRef,
1616
AfterViewInit
1717
} from '@angular/core';
18-
import {
19-
NG_VALUE_ACCESSOR,
20-
ControlValueAccessor,
21-
FormsModule,
22-
} from '@angular/forms';
18+
import {NG_VALUE_ACCESSOR, ControlValueAccessor, FormsModule} from '@angular/forms';
2319
import {Observable} from 'rxjs/Observable';
24-
import {BooleanFieldValue, MdUniqueSelectionDispatcher} from '../core';
20+
import {MdUniqueSelectionDispatcher, coerceBooleanProperty} from '../core';
2521

2622
export type ToggleType = 'checkbox' | 'radio';
2723

@@ -102,13 +98,12 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
10298
}
10399

104100
@Input()
105-
@BooleanFieldValue()
106101
get disabled(): boolean {
107102
return this._disabled;
108103
}
109104

110105
set disabled(value) {
111-
this._disabled = (value != null && value !== false) ? true : null;
106+
this._disabled = coerceBooleanProperty(value);
112107
}
113108

114109
@Input()

src/lib/button/button.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
ModuleWithProviders,
1111
} from '@angular/core';
1212
import {CommonModule} from '@angular/common';
13-
import {BooleanFieldValue, MdRippleModule} from '../core';
13+
import {MdRippleModule, coerceBooleanProperty} from '../core';
1414

1515
// TODO(jelbourn): Make the `isMouseDown` stuff done with one global listener.
1616
// TODO(kara): Convert attribute selectors to classes when attr maps become available
@@ -41,7 +41,11 @@ export class MdButton {
4141
_isMouseDown: boolean = false;
4242

4343
/** Whether the ripple effect on click should be disabled. */
44-
@Input() @BooleanFieldValue() disableRipple: boolean = false;
44+
private _disableRipple: boolean = false;
45+
46+
@Input()
47+
get disableRipple() { return this._disableRipple; }
48+
set disableRipple(v) { this._disableRipple = coerceBooleanProperty(v); }
4549

4650
constructor(private _elementRef: ElementRef, private _renderer: Renderer) { }
4751

src/lib/checkbox/checkbox.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
ModuleWithProviders,
1313
} from '@angular/core';
1414
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
15-
import {BooleanFieldValue} from '../core';
15+
import {coerceBooleanProperty} from '../core/coersion/boolean-property';
16+
1617

1718
/**
1819
* Monotonically increasing integer used to auto-generate unique ids for checkbox components.
@@ -93,8 +94,12 @@ export class MdCheckbox implements ControlValueAccessor {
9394
return `input-${this.id}`;
9495
}
9596

97+
private _required: boolean;
98+
9699
/** Whether the checkbox is required or not. */
97-
@Input() @BooleanFieldValue() required: boolean = false;
100+
@Input()
101+
get required(): boolean { return this._required; }
102+
set required(value) { this._required = coerceBooleanProperty(value); }
98103

99104
/** Whether or not the checkbox should come before or after the label. */
100105
@Input() align: 'start' | 'end' = 'start';

src/lib/core/annotations/field-value.spec.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/lib/core/annotations/field-value.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {coerceBooleanProperty} from './boolean-property';
2+
3+
4+
describe('coerceBooleanProperty', () => {
5+
it('should coerce undefined to false', () => {
6+
expect(coerceBooleanProperty(undefined)).toBe(false);
7+
});
8+
9+
it('should coerce null to false', () => {
10+
expect(coerceBooleanProperty(null)).toBe(false);
11+
});
12+
13+
it('should coerce the empty string to true', () => {
14+
expect(coerceBooleanProperty('')).toBe(true);
15+
});
16+
17+
it('should coerce zero to true', () => {
18+
expect(coerceBooleanProperty(0)).toBe(true);
19+
});
20+
21+
it('should coerce the string "false" to false', () => {
22+
expect(coerceBooleanProperty('false')).toBe(false);
23+
});
24+
25+
it('should coerce the boolean false to false', () => {
26+
expect(coerceBooleanProperty(false)).toBe(false);
27+
});
28+
29+
it('should coerce the boolean true to true', () => {
30+
expect(coerceBooleanProperty(true)).toBe(true);
31+
});
32+
33+
it('should coerce the string "true" to true', () => {
34+
expect(coerceBooleanProperty('true')).toBe(true);
35+
});
36+
37+
it('should coerce an arbitrary string to true', () => {
38+
expect(coerceBooleanProperty('pink')).toBe(true);
39+
});
40+
41+
it('should coerce an object to true', () => {
42+
expect(coerceBooleanProperty({})).toBe(true);
43+
});
44+
45+
it('should coerce an array to true', () => {
46+
expect(coerceBooleanProperty([])).toBe(true);
47+
});
48+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** Coerces a data-bound value (typically a string) to a boolean. */
2+
export function coerceBooleanProperty(value: any): boolean {
3+
return value != null && `${value}` !== 'false';
4+
}

src/lib/core/core.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ export {applyCssTransform} from './style/apply-transform';
7070
// Error
7171
export {MdError} from './errors/error';
7272

73-
// Annotations.
74-
export {BooleanFieldValue} from './annotations/field-value';
75-
7673
// Misc
7774
export {ComponentType} from './overlay/generic-component-type';
7875

@@ -84,6 +81,9 @@ export * from './compatibility/style-compatibility';
8481
// Animation
8582
export * from './animation/animation';
8683

84+
// Coersion
85+
export {coerceBooleanProperty} from './coersion/boolean-property';
86+
8787

8888
@NgModule({
8989
imports: [MdLineModule, RtlModule, MdRippleModule, PortalModule, OverlayModule, A11yModule],

src/lib/input/input.ts

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ import {
1818
ModuleWithProviders,
1919
ViewEncapsulation,
2020
} from '@angular/core';
21-
import {
22-
NG_VALUE_ACCESSOR,
23-
ControlValueAccessor,
24-
FormsModule,
25-
} from '@angular/forms';
21+
import {NG_VALUE_ACCESSOR, ControlValueAccessor, FormsModule} from '@angular/forms';
2622
import {CommonModule} from '@angular/common';
27-
import {BooleanFieldValue, MdError} from '../core';
23+
import {MdError, coerceBooleanProperty} from '../core';
2824
import {Observable} from 'rxjs/Observable';
2925

3026

@@ -118,9 +114,22 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
118114
*/
119115
@Input('aria-label') ariaLabel: string;
120116
@Input('aria-labelledby') ariaLabelledBy: string;
121-
@Input('aria-disabled') @BooleanFieldValue() ariaDisabled: boolean;
122-
@Input('aria-required') @BooleanFieldValue() ariaRequired: boolean;
123-
@Input('aria-invalid') @BooleanFieldValue() ariaInvalid: boolean;
117+
118+
private _ariaDisabled: boolean;
119+
private _ariaRequired: boolean;
120+
private _ariaInvalid: boolean;
121+
122+
@Input('aria-disabled')
123+
get ariaDisabled(): boolean { return this._ariaDisabled; }
124+
set ariaDisabled(value) { this._ariaDisabled = coerceBooleanProperty(value); }
125+
126+
@Input('aria-required')
127+
get ariaRequired(): boolean { return this._ariaRequired; }
128+
set ariaRequired(value) { this._ariaRequired = coerceBooleanProperty(value); }
129+
130+
@Input('aria-invalid')
131+
get ariaInvalid(): boolean { return this._ariaInvalid; }
132+
set ariaInvalid(value) { this._ariaInvalid = coerceBooleanProperty(value); }
124133

125134
/**
126135
* Content directives.
@@ -141,29 +150,55 @@ export class MdInput implements ControlValueAccessor, AfterContentInit, OnChange
141150
*/
142151
@Input() align: 'start' | 'end' = 'start';
143152
@Input() dividerColor: 'primary' | 'accent' | 'warn' = 'primary';
144-
@Input() @BooleanFieldValue() floatingPlaceholder: boolean = true;
145153
@Input() hintLabel: string = '';
146154

147155
@Input() autocomplete: string;
148156
@Input() autocorrect: string;
149157
@Input() autocapitalize: string;
150-
@Input() @BooleanFieldValue() autofocus: boolean = false;
151-
@Input() @BooleanFieldValue() disabled: boolean = false;
152158
@Input() id: string = `md-input-${nextUniqueId++}`;
153159
@Input() list: string = null;
154160
@Input() max: string | number = null;
155161
@Input() maxlength: number = null;
156162
@Input() min: string | number = null;
157163
@Input() minlength: number = null;
158164
@Input() placeholder: string = null;
159-
@Input() @BooleanFieldValue() readonly: boolean = false;
160-
@Input() @BooleanFieldValue() required: boolean = false;
161-
@Input() @BooleanFieldValue() spellcheck: boolean = false;
162165
@Input() step: number = null;
163166
@Input() tabindex: number = null;
164167
@Input() type: string = 'text';
165168
@Input() name: string = null;
166169

170+
private _floatingPlaceholder: boolean = false;
171+
private _autofocus: boolean = false;
172+
private _disabled: boolean = false;
173+
private _readonly: boolean = false;
174+
private _required: boolean = false;
175+
private _spellcheck: boolean = false;
176+
177+
@Input()
178+
get floatingPlaceholder(): boolean { return this._floatingPlaceholder; }
179+
set floatingPlaceholder(value) { this._floatingPlaceholder = coerceBooleanProperty(value); }
180+
181+
@Input()
182+
get autofocus(): boolean { return this._autofocus; }
183+
set autofocus(value) { this._autofocus = coerceBooleanProperty(value); }
184+
185+
@Input()
186+
get disabled(): boolean { return this._disabled; }
187+
set disabled(value) { this._disabled = coerceBooleanProperty(value); }
188+
189+
@Input()
190+
get readonly(): boolean { return this._readonly; }
191+
set readonly(value) { this._readonly = coerceBooleanProperty(value); }
192+
193+
@Input()
194+
get required(): boolean { return this._required; }
195+
set required(value) { this._required = coerceBooleanProperty(value); }
196+
197+
@Input()
198+
get spellcheck(): boolean { return this._spellcheck; }
199+
set spellcheck(value) { this._spellcheck = coerceBooleanProperty(value); }
200+
201+
167202
private _blurEmitter: EventEmitter<FocusEvent> = new EventEmitter<FocusEvent>();
168203
private _focusEmitter: EventEmitter<FocusEvent> = new EventEmitter<FocusEvent>();
169204

src/lib/sidenav/sidenav.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
ViewEncapsulation,
1717
} from '@angular/core';
1818
import {CommonModule} from '@angular/common';
19-
import {Dir, MdError} from '../core';
19+
import {Dir, MdError, coerceBooleanProperty} from '../core';
20+
2021

2122
/** Exception thrown when two MdSidenav are matching the same side. */
2223
export class MdDuplicatedSidenavError extends MdError {
@@ -79,9 +80,7 @@ export class MdSidenav {
7980
@Input()
8081
get opened(): boolean { return this._opened; }
8182
set opened(v: boolean) {
82-
// TODO(jelbourn): this coercion goes away when BooleanFieldValue is removed.
83-
let booleanValue = v != null && `${v}` !== 'false';
84-
this.toggle(booleanValue);
83+
this.toggle(coerceBooleanProperty(v));
8584
}
8685

8786

0 commit comments

Comments
 (0)