import { Component } from '@angular/core' ; import { BaseSearchComponent } from '../base-search.component' ; @ Component ( { selector : 'ux-search-date-range' , templateUrl : './date-range.component.html' } ) export class SearchDateRangeComponent extends BaseSearchComponent { type : string = 'date-range' ; get label ( ) : string { return this . config . label ; } get from ( ) { if ( ! this . value || ! this . value . from ) { this . from = new Date ( ) ; } if ( this . value . from instanceof Date === false ) { this . value . from = new Date ( this . value . from ) ; } return this . value . from ; } set from ( fromValue ) { const value = Object . assign ( { } , this . value ) ; if ( fromValue instanceof Date === false ) { fromValue = new Date ( fromValue ) ; } value . from = fromValue ; this . value = value ; } get to ( ) { if ( ! this . value || ! this . value . to ) { this . to = new Date ( ) ; } if ( this . value . to instanceof Date === false ) { this . value . to = new Date ( this . value . to ) ; } return this . value . to ; } set to ( toValue ) { const value = Object . assign ( { } , this . value ) ; if ( toValue instanceof Date === false ) { toValue = new Date ( toValue ) ; } value . to = toValue ; this . value = value ; } get fromLabel ( ) : string { return this . config . fromLabel || 'From' ; } get toLabel ( ) : string { return this . config . toLabel || 'To' ; } get fromPlaceholder ( ) : string { return this . config . fromPlaceholder ; } get toPlaceholder ( ) : string { return this . config . toPlaceholder ; } /**
     * Override the default validation
     */ validate ( ) { if ( this . config . validation ) { return super . validate ( ) ; } const from = new Date ( this . value . from ) ; const to = new Date ( this . value . to ) ; from . setHours ( 0 , 0 , 0 , 0 ) ; to . setHours ( 0 , 0 , 0 , 0 ) ; this . valid = from <= to ; } } export interface SearchDateRangeConfig { label ? : string ; fromLabel ? : string ; toLabel ? : string ; fromPlaceholder ? : string ; toPlaceholder ? : string ; validation : ( value : any ) => boolean ; }