Skip to content

Instantly share code, notes, and snippets.

@jorroll
Created November 4, 2019 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorroll/09b8580ab33bfce266221839f399fdd5 to your computer and use it in GitHub Desktop.
Save jorroll/09b8580ab33bfce266221839f399fdd5 to your computer and use it in GitHub Desktop.
declare const stringDateValidator: ValidatorFn;
declare const stringToDate: (value: string) => Date | null;
declare const dateToString: (value: Date | null) => string;
class ExampleFourComponent implements OnInit {
inputControl = new FormControl('', {
validators: stringDateValidator,
});
dateControl = new FormControl<Date | null>(null);
ngOnInit() {
this.inputControl.events
.pipe(
map(event => {
if (event.type === 'StateChange' && event.changes.has('value')) {
const changes = new Map(event.changes);
changes.set('value', stringToDate(changes.get('value')));
return {
...event,
changes,
};
}
return event;
}),
)
.subscribe(this.dateControl.source);
this.dateControl.events
.pipe(
map(event => {
if (event.type === 'StateChange' && event.changes.has('value')) {
const changes = new Map(event.changes);
changes.set('value', dateToString(changes.get('value')));
return {
...event,
changes,
};
}
return event;
}),
)
.subscribe(this.inputControl.source);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment