Skip to content

Commit

Permalink
Merge pull request #7 from extrawest/fix/time-range-filter-toggle
Browse files Browse the repository at this point in the history
Made it possible to select whether date-time-input should include time
  • Loading branch information
Bohdan-Horodyskyi-ew authored Mar 4, 2024
2 parents 5587a81 + bf54423 commit 5d08283
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
#inputStart
label="From"
[disabled]="disabled"
[withTime]="withTime"
[(value)]="visualRange.start"
/>

<ec-date-time-input
#inputEnd
label="To"
[disabled]="disabled"
[withTime]="withTime"
[(value)]="visualRange.end"
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export class EcDateTimeGroupComponent implements OnChanges {
@Input()
public value?: EcCustomTimeRange;

@Input()
public withTime: boolean = true;

@Output()
public apply: EventEmitter<EcCustomTimeRange> = new EventEmitter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</label>

<input
type="datetime-local"
[type]="inputType"
clrInput
#datetimeInput
class="timestamp-input"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import { datetimeInputValidator } from './date-time-input.validators';
styleUrls: ['./date-time-input.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [
CommonModule,
ReactiveFormsModule,
ClrInputModule,
],
imports: [CommonModule, ReactiveFormsModule, ClrInputModule],
})
export class EcDateTimeInputComponent implements AfterViewInit, OnChanges, OnDestroy {
export class EcDateTimeInputComponent
implements AfterViewInit, OnChanges, OnDestroy
{
@Input()
public disabled: boolean = false;

Expand All @@ -41,6 +39,9 @@ export class EcDateTimeInputComponent implements AfterViewInit, OnChanges, OnDes
@Input()
public value: number | null = null;

@Input()
public withTime: boolean = true;

@Output()
public valueChange: EventEmitter<number | null> = new EventEmitter();

Expand All @@ -50,10 +51,18 @@ export class EcDateTimeInputComponent implements AfterViewInit, OnChanges, OnDes
@ViewChild(ClrInput, { static: true })
protected clrInputRef?: ClrInput;

public readonly formControl = new FormControl<string>('', { nonNullable: true });
public inputType = this.inputDateType;

public readonly formControl = new FormControl<string>('', {
nonNullable: true,
});

private readonly destroy$ = new Subject<void>();

get inputDateType(): string {
return this.withTime ? 'datetime-local' : 'date';
}

constructor(private changeDetectorRef: ChangeDetectorRef) {}

ngAfterViewInit(): void {
Expand All @@ -79,6 +88,10 @@ export class EcDateTimeInputComponent implements AfterViewInit, OnChanges, OnDes
if (changes['value'] && this.value !== undefined) {
this.updateFormValue(this.value);
}

if (changes['withTime']) {
this.inputType = this.withTime ? 'datetime-local' : 'date';
}
}

ngOnDestroy(): void {
Expand Down Expand Up @@ -125,7 +138,9 @@ export class EcDateTimeInputComponent implements AfterViewInit, OnChanges, OnDes
const hh = date.getHours().toString().padStart(2, '0');
const mm = date.getMinutes().toString().padStart(2, '0');

return `${YYYY}-${MM}-${DD}T${hh}:${mm}`;
return this.withTime
? `${YYYY}-${MM}-${DD}T${hh}:${mm}`
: `${YYYY}-${MM}-${DD}`;
}

private convertFromFormValue(timestampAsString: string): number | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
[value]="visualCustomRange"
[disabled]="radioControl.value !== null"
[forceEnabledButtons]="radioControl.value !== filterValue.preset"
[withTime]="withTime"
(apply)="onCustomRangeApply($event)"
(discard)="onCustomRangeDiscard()"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export class EcTimeRangeFilterComponent<T extends object = {}>
@Input()
public withCustomRange: boolean = false;

/** Whether the input field should allow selecting a date-time range. */
@Input()
public withTime: boolean = true;

/**
* Emits the filter's state object on every change of the internal filter value.
* The state object contains the name of a `property` to filter by, defined by the `[propertyKey]` input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
[closeOnChange]="closeOnChange"
[widthPx]="widthPx"
[value]="value"
[withTime]="withTime"
(filterValueChanged)="onTimeRangeChange($event)"
/>
</ec-popover-toggle>
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export class EcTimeRangeFilterToggleComponent implements EcResettableFilter {
@Input()
public withCustomRange: boolean = false;

@Input()
public withTime: boolean = true;

@Input()
public closeOnChange: boolean = false;

Expand Down

0 comments on commit 5d08283

Please sign in to comment.