#Open a datepicker from a button
52 messages · Page 1 of 1 (latest)
Yes that is possible
What would be the syntaxe of that ?
Depends on the date picker but
@Component({
template: `
<app-datepicker *ngIf="showPicker"></app-picker>
<input *ngIf="!showPicker">
`,
})
export class TheComponent {
public showPicker: boolean = false;
}
g!toh @fervent flame Have you built the Tour of Heroes apps from the tutorial in the Angular Guide? They are a great starting point for Angular because they show you some of what Angular can do and how to do it. I highly recommend that you put aside this current project and go do the tutorial. It will save you time.
@fervent flame The place we recommend starting your Angular journey is with the Tour of Heroes tutorial, located here https://angular.io/tutorial.
It's been 3.5+ years I'm doing Angular in entreprise, I did the ToH long time ago.
Only thing is, I never had the situation of where I need to open a datepicker from another button. I'm mostly using material, and with that they give you the ability to open the picker with the open() method, which I don't know if it exist for a "default" datepicker. When I say open the datepicker it's that I'm talking about (browser native datepicker) :
How was I supposed to know that from your original question?
I'm sorry maybe it wasn't clear enough 😦
Basically you have an <input type="date" that is visually hidden by CSS.
Then you make a button that on click focuses the input element to display the picker.
you could probably use a label too
Okey so display: none for the input, and then for the button it's something like : (click)="myInput.focus"?
I'm not sure for the syntaxe 🤔
<label>
<input type="date" class="visually-hidden">
<i class="icon">date picker</i>
</label>
No, display none would remove it from the DOM. I doubt that would work. But try it.
I was suggesting https://developer.mozilla.org/en-US/docs/Web/CSS/visibility
Ok, wasn't aware visibity existed, that is intersting. So something like that ?
<div class="btn">
<mat-icon>event</mat-icon>
<span (pointerdown)="$event.preventDefault()" (click)="dateInput.focus">Add Date</span>
<input #dateInput type="date" style="visibility: hidden; position: absolute;" name="date" id="date">
</div>```
(didn't change for a label since it's a whole button, and the styling will go in the scss, just for now I placing it here for test purposes)
dateInput.focus doesn't seem to do anything, any idea why ?
Apparently you need https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/showPicker
Is it possible to have the showPicker() inline in the html ?
That's intersting, contreticts the doc
You could try:
<label (click)="dateInpt.showPicker()">
<input #dateInpt type="date" class="visually-hidden">
<i class="icon">date picker</i>
</label>
What version of Angular are you using?
That's weird, same with the code as you provided
What version of Angular are you using?
Paste the complete output of ng version in a code block please
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 14.2.6
Node: 16.13.1
Package Manager: npm 8.1.2
OS: darwin arm64
Angular: 14.2.6
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1402.6
@angular-devkit/build-angular 14.2.6
@angular-devkit/core 14.2.6
@angular-devkit/schematics 14.2.6
@angular/cdk 14.2.5
@angular/material 14.2.5
@schematics/angular 14.2.6
rxjs 7.5.7
typescript 4.7.4```
Might just be your editor having old definitions/types.
does it work in ng serve or ng build?
At runtime too :/
Try it in a new Angular project. works just fine in mine. I assume your environment is broken subtly somehow
That is so weird
the definition file should be: node_modules/typescript/lib/lib.dom.d.ts
/**
* Sets the start and end positions of a selection in a text field.
* @param start The offset into the text field for the start of the selection.
* @param end The offset into the text field for the end of the selection.
* @param direction The direction in which the selection is performed.
*/
setSelectionRange(start: number | null, end: number | null, direction?: "forward" | "backward" | "none"): void;
showPicker(): void;
/**
* Decrements a range input control's value by the value given by the Step attribute. If the optional parameter is used, it will decrement the input control's step value multiplied by the parameter's value.
* @param n Value to decrement the value by.
*/
stepDown(n?: number): void;
It is a relatively new feature; version 99 wasn't that long ago:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/showPicker#browser_compatibility
Maybe I should update to ng 14.2.0 to 14.2.8
Because I indeed don't see it in the node_modules the showPicker method
It was added 5 months ago to TypeScript
https://github.com/microsoft/TypeScript/commit/6004b35ce4c9ed1c9e8c1e450068ef3f9c4cbd5c
version 4.8.4. But even with typescript 4.6.4 that code compiles for me
If I run ng build with a typo I get:
Error: src/app/user-input/user-input.component.html:118:26 - error TS2339: Property 'showPiker' does not exist on type 'HTMLInputElement'.
118 <button (click)="foo.showPiker()">Open</button>
~~~~~~~~~
src/app/user-input/user-input.component.ts:55:16
55 templateUrl: './user-input.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component UserInputComponent.
If I fix the typo to foo.showPicker() it builds just fine.
I'm on typescript 4.7, so that's probably it
What is your lib in tsconfig.json?
{
"lib": [
"es2020",
"dom",
"dom.iterable"
],
Ah didn't see that
Perhaps it is that I added dom.iterable to my list. Seems like the default is:
{
"lib": [
"es2020",
"dom"
]
mmh yes it's default that, so I added dom.itarable but it doesn't seem to fix the issue
Probably you will have to do it in your TS code so you can do:
// @ts-expect-error showPicker was introduced in typescript 4.8.4
this.dateInput.nativeElement.showPicker()
Okey I upgrade to 4.8.4 it fixes the issue
That is a risky upgrade...
because v4.7 and v4.8 are different. And because Angular hasn't declared itself compatible with v4.8 yet
Okey I see... 🤔
I revertet to 4.7, and added that and it looks like it solve the TS error
Thank you for your help !