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

Add "readonly" support to other form control components #115

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions src/components/checkbox/checkbox.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ describe('Checkbox', () => {
)
);

it('should not emit a single "change" when the checkbox is readonly',
componentTest(() => TestComponent, `
<gtx-checkbox readonly="true" (change)="onChange($event)">
</gtx-checkbox>`,
fixture => {
const nativeInput: HTMLInputElement = fixture.nativeElement.querySelector('input');
const instance: TestComponent = fixture.componentInstance;
fixture.detectChanges();
const spy = instance.onChange = jasmine.createSpy('onChange');

nativeInput.click();
tick();
fixture.detectChanges();

expect(instance.onChange).toHaveBeenCalledTimes(0);
expect(spy.calls.count()).toBe(0);
}
)
);

it('should emit "blur" with current check state when the native input blurs',
componentTest(() => TestComponent, `
<gtx-checkbox
Expand Down
20 changes: 20 additions & 0 deletions src/components/checkbox/checkbox.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {KeyCode} from '../../common/keycodes';
import { coerceToBoolean } from '../../common/coerce-to-boolean';

export type CheckState = boolean | 'indeterminate';

Expand Down Expand Up @@ -86,6 +87,15 @@ export class Checkbox implements ControlValueAccessor {
}
}

/** Set to `true` to change mode of the checkbox to `readOnly`. */
@Input()
get readonly(): boolean {
return this._readonly;
}
set readonly(value: any) {
this._readonly = coerceToBoolean(value);
}

/**
* Set the checkbox to its disabled state.
*/
Expand Down Expand Up @@ -124,6 +134,8 @@ export class Checkbox implements ControlValueAccessor {
*/
@Output() change = new EventEmitter<CheckState>();

_readonly: boolean = false;

checkState: CheckState = false;
tabbedFocus: boolean = false;

Expand Down Expand Up @@ -171,6 +183,10 @@ export class Checkbox implements ControlValueAccessor {
this.fixInitialAnimation();
}

onClick(): boolean {
return !this.readonly;
}

onInputChanged(e: Event, input: HTMLInputElement): boolean {
if (e) {
e.stopPropagation();
Expand All @@ -197,6 +213,10 @@ export class Checkbox implements ControlValueAccessor {
this.disabled = disabled;
this.changeDetector.markForCheck();
}
setReadOnlyState(readonly: boolean): void {
this.readonly = readonly;
this.changeDetector.markForCheck();
}

private onChange: Function = () => {};
private onTouched: Function = () => {};
Expand Down
2 changes: 1 addition & 1 deletion src/components/checkbox/checkbox.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(blur)="onBlur()"
(focus)="onFocus()"
(change)="onInputChanged($event, input)"

(click)="onClick()"
[class.tabbed]="tabbedFocus"

#input
Expand Down
37 changes: 37 additions & 0 deletions src/components/date-time-picker/date-time-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {OverlayHostService} from '../overlay-host/overlay-host.service';
import {DateTimePickerFormatProvider} from './date-time-picker-format-provider.service';
import {DateTimePickerModal} from './date-time-picker-modal.component';
import {DateTimePicker} from './date-time-picker.component';
import { crossBrowserInitKeyboardEvent, KeyboardEventConfig } from '../../testing/keyboard-event';
import { KeyCode } from '../../common/keycodes';

const TEST_TIMESTAMP: number = 1457971763;

Expand Down Expand Up @@ -161,6 +163,12 @@ describe('DateTimePicker:', () => {

describe('input display:', () => {

function dispatchKeyboardEvent(keyCode: number, element: HTMLElement, options?: KeyboardEventConfig): void {
let mergedOptions = Object.assign({ keyCode, bubbles: true }, options);
let event = crossBrowserInitKeyboardEvent('keypress', mergedOptions);
element.dispatchEvent(event);
}

function inputValue(fixture: ComponentFixture<TestComponent>): string {
fixture.detectChanges();
return fixture.nativeElement.querySelector('input').value.trim();
Expand Down Expand Up @@ -277,6 +285,35 @@ describe('DateTimePicker:', () => {
)
);

it('does not emit "clear" and "change" when the date picker is readonly',
componentTest(() => TestComponent, `
<gtx-date-time-picker clearable [readonly]="true" (clear)="onClear($event)" (change)="onChange($event)" timestamp="${TEST_TIMESTAMP}">
</gtx-date-time-picker>`,
(fixture, testComponent) => {
fixture.detectChanges();
tick();

expect(testComponent.onClear).not.toHaveBeenCalled();

const clearButton = fixture.debugElement.query(By.css('gtx-button'));
clearButton.triggerEventHandler('click', document.createEvent('Event'));
fixture.detectChanges();

expect(testComponent.onClear).toHaveBeenCalledTimes(0);

const input = fixture.debugElement.query(By.css('gtx-input'));
input.triggerEventHandler('click', document.createEvent('Event'));
fixture.detectChanges();

expect(testComponent.onChange).toHaveBeenCalledTimes(0);

dispatchKeyboardEvent(KeyCode.Enter, input.nativeElement)

expect(testComponent.onChange).toHaveBeenCalledTimes(0);
}
)
);

it('does not clear its value when clicking the clear button if the date picker is disabled',
componentTest(() => TestComponent, `
<gtx-date-time-picker clearable disabled [(ngModel)]="testModel"
Expand Down
13 changes: 12 additions & 1 deletion src/components/date-time-picker/date-time-picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export class DateTimePicker implements ControlValueAccessor, OnInit, OnDestroy {
this._displaySeconds = coerceToBoolean(val);
}

/** Set to `true` to change mode of the date picker to `readOnly`. */
@Input() set readonly(val: any) {
this._readonly = coerceToBoolean(val);
}

/** Fires when the "okay" button is clicked to close the picker. */
@Output() change = new EventEmitter<number|null>();

Expand All @@ -109,6 +114,7 @@ export class DateTimePicker implements ControlValueAccessor, OnInit, OnDestroy {
_clearable: boolean = false;
_selectYear: boolean = false;
_disabled: boolean = false;
_readonly: boolean = false;
displayValue: string = '';
/** @internal */
private value: Moment;
Expand Down Expand Up @@ -150,7 +156,7 @@ export class DateTimePicker implements ControlValueAccessor, OnInit, OnDestroy {
}

handleEnterKey(event: KeyboardEvent): void {
if (event.keyCode === 13 && !this._disabled) {
if (event.keyCode === 13 && !this._disabled && !this._readonly) {
this.showModal();
}
}
Expand Down Expand Up @@ -209,6 +215,11 @@ export class DateTimePicker implements ControlValueAccessor, OnInit, OnDestroy {
this.changeDetector.markForCheck();
}

setReadOnlyState(readonly: boolean): void {
this.readonly = readonly;
this.changeDetector.markForCheck();
}

/** Format date to a human-readable string for displaying in the component's input field. */
updateDisplayValue(): void {
if (!this.value) {
Expand Down
6 changes: 3 additions & 3 deletions src/components/date-time-picker/date-time-picker.tpl.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<gtx-input [value]="displayValue"
[label]="label"
(click)="!_disabled && showModal()"
(click)="!_disabled && !_readonly && showModal()"
(keydown)="handleEnterKey($event)"
(blur)="onTouched()"
[disabled]="_disabled"
readonly="true"
[readonly]="true"
[class.clearable]="_clearable"></gtx-input>
<gtx-button icon
class="clear-button"
*ngIf="_clearable"
type="secondary"
[disabled]="_disabled"
(click)="!_disabled && clearDateTime()">
(click)="!_disabled && !_readonly && clearDateTime()">
<icon>clear</icon>
</gtx-button>

14 changes: 13 additions & 1 deletion src/components/dropdown-list/dropdown-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class DropdownList implements OnDestroy {
@Output() close = new EventEmitter<void>();

private _disabled: boolean = false;
private _readonly: boolean = false;
private overlayHostView: ViewContainerRef;
private scrollMaskFactory: ScrollMask;
private scrollMaskRef: ComponentRef<ScrollMask>;
Expand Down Expand Up @@ -164,6 +165,17 @@ export class DropdownList implements OnDestroy {
this._disabled = coerceToBoolean(val);
}

/**
* If true, the dropdown will be in readonly mode.
*/
@Input()
get readonly(): boolean {
return this._readonly;
}
set readonly(value: any) {
this._readonly = coerceToBoolean(value);
}

get isOpen(): boolean {
return !!this.contentComponentRef;
}
Expand Down Expand Up @@ -217,7 +229,7 @@ export class DropdownList implements OnDestroy {
* Open the dropdown contents in the correct position.
*/
openDropdown(): void {
if (this._disabled) {
if (this._disabled || this._readonly) {
return;
}
this.contentComponentRef = this.overlayHostView.createComponent(DropdownContentWrapper, null);
Expand Down
21 changes: 21 additions & 0 deletions src/components/range/range.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ describe('Range:', () => {
)
);

it('does not update the value when readonly attribute is true',
componentTest (() => TestComponent, `
<gtx-range readonly="true" value="35" (change)="onChange($event)"
></gtx-range>`,
(fixture, instance) => {
let nativeInput: HTMLInputElement = fixture.nativeElement.querySelector('input');
fixture.detectChanges();
instance.onChange = jasmine.createSpy('onChange');
triggerInputEvent(nativeInput);
tick();
expect(instance.onChange).toHaveBeenCalledTimes(0);

instance.onInput = jasmine.createSpy('onInput');
triggerInputEvent(nativeInput);
tick();
expect(instance.onInput).toHaveBeenCalledTimes(0);
}
)
);

it('emits "blur" with the current value when its native input blurs',
componentTest(() => TestComponent, `
<gtx-range
Expand Down Expand Up @@ -351,6 +371,7 @@ class TestComponent {
onBlur(...args: any[]): void {}
onFocus(...args: any[]): void {}
onChange(...args: any[]): void {}
onInput(...args: any[]): void {}
}

/**
Expand Down
19 changes: 18 additions & 1 deletion src/components/range/range.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ export class Range implements ControlValueAccessor {
*/
@Input() id: string;

/**
* Sets the readonly state.
*/
@Input()
get readonly(): boolean {
return this._readonly;
}
set readonly(value: any) {
this._readonly = coerceToBoolean(value);
}

/**
* Set to false to not show the thumb label. Defaults to true.
*/
Expand Down Expand Up @@ -102,12 +113,13 @@ export class Range implements ControlValueAccessor {
thumbLeft: string = '';
currentValue: number;
private showThumbLabel: boolean = true;
private _readonly: boolean = false;


@ViewChild('input', { static: true }) private inputElement: ElementRef;

private get canModify(): boolean {
return !this.disabled;
return !this.disabled && !this.readonly;
}

// ValueAccessor members
Expand Down Expand Up @@ -197,6 +209,11 @@ export class Range implements ControlValueAccessor {
this.changeDetector.markForCheck();
}

setReadonlyState(readonly: boolean): void {
this.readonly = readonly;
this.changeDetector.markForCheck();
}

private getValueFromEvent(e: Event): number {
const target: HTMLInputElement = <HTMLInputElement> e.target;
return Number(target.value);
Expand Down
6 changes: 3 additions & 3 deletions src/components/range/range.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
[attr.name]="name"
[required]="required"
[attr.step]="step"

[attr.id]="id"

(blur)="onBlur($event)"
(change)="onChangeEvent($event)"
(focus)="onFocus($event)"
(input)="onInput($event)"
(mousedown)="onMousedown($event)"
(mouseup)="onMouseup()"
(mousedown)="!readonly && onMousedown($event)"
(mouseup)="!readonly && onMouseup()"
(mousemove)="onMousemove($event)"

#input
>
<span class="thumb"
[class.hidden]="!thumbLabel"
[class.readonly]="readonly"
[class.active]="active"
[style.left]="thumbLeft">
<span class="value">{{ currentValue }}</span>
Expand Down
17 changes: 17 additions & 0 deletions src/components/select/select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,23 @@ describe('Select:', () => {
})
);

it('does not open the dropdown when the it is readonly',
componentTest(() => TestComponent, `
<gtx-select readonly multiple="true" [value]="[value]" (change)="onChange($event)">
<gtx-option *ngFor="let option of options" [value]="option">{{ option }}</gtx-option>
</gtx-select>
<gtx-overlay-host></gtx-overlay-host>`,
(fixture, instance) => {
fixture.detectChanges();
tick();
clickSelectAndOpen(fixture);
const listItems = getListItems(fixture);
expect(listItems).toHaveSize(0);
tick(1000);
}
)
);

it('should open when space is pressed',
componentTest(() => TestComponent, fixture => {
fixture.detectChanges();
Expand Down
Loading