-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(material/timepicker): add timepicker component
Addresses a long-time feature request by adding a component that allows users to select a time. The new component uses a combination of an `input` and a dropdown to allow users to either type a time or select it from a pre-defined list. Example usage: ```html <mat-form-field> <mat-label>Pick a time</mat-label> <input matInput [matTimepicker]="picker"/> <mat-timepicker #picker/> <mat-timepicker-toggle [for]="picker"/> </mat-form-field> ``` Features of the new component include: * Automatically parses the typed-in value to a date object using the current `DateAdapter`. Existing date adapters have been updated to add support for parsing times. * Time values can be generated either using the `interval` input (e.g. `interval="45min"`) or provided directly through the `options` input. * Integrated into `@angular/forms` by providing itself as a `ControlValueAccessor` and `Validator`. * Offers built-in validation for minimum, maximum and time formatting. * Offers keyboard navigation support. * Accessibility implemented using the combobox + listbox pattern. * Can be used either with `mat-form-field` or on its own. * Can be combined with `mat-datepicker` (docs to follow, see the dev app for now). * Includes test harnesses for all directives. * Works with Material's theming system. * Can be configured globally through an injection token. * Can be used either as an `NgModule` or by importing the standalone directives. One of the main reasons why we hadn't provided a timepicker component until now was that there's no universally-accepted design for what a timepicker should look like. Material Design has had a [specification for a timepicker](https://m3.material.io/components/time-pickers/overview) for years, but we didn't want to implement it because: 1. This design is primarily geared towards mobile users on Android. It would look out of place in the desktop-focused enterprise UIs that a lot of Angular developers build. 2. The time dial UI is complicated and can be overwhelming, especially in the 24h variant. 3. The accessibility pattern is unclear, users may have to fall back to using the inputs. 4. It's unclear how the time selection would work on non-Westernized locales whose time formatting isn't some variation of `HH:MM`. 5. The time dial requires very precise movements if the user wants to select a specific time between others (e.g. 6:52). This can be unusable for users with some disabilities. 6. The non-dial functionality (inputs in a dropdown) don't add much to the user experience. There are [community implementations](https://dhutaryan.github.io/ngx-mat-timepicker) of the dial design that you can install if you want it for your app. Some libraries like [Kendo UI](https://www.telerik.com/kendo-angular-ui/components/dateinputs/timepicker), [Ignite UI](https://www.infragistics.com/products/ignite-ui-angular/angular/components/time-picker) or [MUI](https://mui.com/x/react-date-pickers/time-picker/), as well as Chrome's implementation of `<input type="time"/>` appear to have settled on a multi-column design for the dropdown. We didn't want to do something similar because: 1. The selected state is only shown using one sensory characteristic (color) which is problematic for accessibility. While we could either add a second one (e.g. a checkbox) or adjust the design somehow, we felt that this would make it look sub-optimal. 2. The UI only looks good on smaller sizes and when each column has roughly the same amount of text. Changing either for a particular column can throw off the whole UI's appearance. 3. It requires the user to tab through several controls within the dialog. 4. It's unclear how the time selection would work on non-Westernized locales whose time formatting isn't some variation of `HH:MM`. 5. Each column requires a lot of filler whitespace in order to be able to align the selected states to each other which can look off on some selections. We chose the current design, because: 1. Users are familiar with it, e.g. Google Calendar uses something similar for their time selection. 2. It reuses the design from existing Material Design components. 3. It uses an established accessibility pattern (combobox + listbox) and it doesn't have the same concerns as the multi-column design around indicating the selected state. 4. It allows us to support a wide range of locales. 5. It's compact, allowing us to do some sort of unified control with `mat-datepicker` in the future. Fixes #5648.
- Loading branch information
Showing
20 changed files
with
2,776 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,99 @@ | ||
<mat-timepicker/> | ||
<div class="demo-row"> | ||
<div> | ||
<div> | ||
<h2>Basic timepicker</h2> | ||
<mat-form-field> | ||
<mat-label>Pick a time</mat-label> | ||
<input | ||
matInput | ||
[matTimepicker]="basicPicker" | ||
[matTimepickerMin]="minControl.value" | ||
[matTimepickerMax]="maxControl.value" | ||
[formControl]="control"> | ||
<mat-timepicker [interval]="intervalControl.value" #basicPicker/> | ||
<mat-timepicker-toggle [for]="basicPicker" matSuffix/> | ||
</mat-form-field> | ||
|
||
<p>Value: {{control.value}}</p> | ||
<p>Dirty: {{control.dirty}}</p> | ||
<p>Touched: {{control.touched}}</p> | ||
<p>Errors: {{control.errors | json}}</p> | ||
<button mat-button (click)="randomizeValue()">Assign a random value</button> | ||
</div> | ||
|
||
<div> | ||
<h2>Timepicker and datepicker</h2> | ||
<mat-form-field> | ||
<mat-label>Pick a date</mat-label> | ||
<input | ||
matInput | ||
[matDatepicker]="combinedDatepicker" | ||
[(ngModel)]="combinedValue"> | ||
<mat-datepicker #combinedDatepicker/> | ||
<mat-datepicker-toggle [for]="combinedDatepicker" matSuffix/> | ||
</mat-form-field> | ||
|
||
<div> | ||
<mat-form-field> | ||
<mat-label>Pick a time</mat-label> | ||
<input | ||
matInput | ||
[matTimepicker]="combinedTimepicker" | ||
[matTimepickerMin]="minControl.value" | ||
[matTimepickerMax]="maxControl.value" | ||
[(ngModel)]="combinedValue" | ||
[ngModelOptions]="{updateOn: 'blur'}"> | ||
<mat-timepicker [interval]="intervalControl.value" #combinedTimepicker/> | ||
<mat-timepicker-toggle [for]="combinedTimepicker" matSuffix/> | ||
</mat-form-field> | ||
</div> | ||
|
||
<p>Value: {{combinedValue}}</p> | ||
</div> | ||
|
||
<div> | ||
<h2>Timepicker without form field</h2> | ||
<input [matTimepicker]="nonFormFieldPicker"> | ||
<mat-timepicker aria-label="Standalone timepicker" #nonFormFieldPicker/> | ||
</div> | ||
</div> | ||
|
||
<mat-card appearance="outlined" class="demo-card"> | ||
<mat-card-header> | ||
<mat-card-title>State</mat-card-title> | ||
</mat-card-header> | ||
|
||
<mat-card-content> | ||
<div class="demo-form-fields"> | ||
<mat-form-field> | ||
<mat-label>Locale</mat-label> | ||
<mat-select [formControl]="localeControl"> | ||
@for (locale of locales; track $index) { | ||
<mat-option [value]="locale">{{locale}}</mat-option> | ||
} | ||
</mat-select> | ||
</mat-form-field> | ||
|
||
<mat-form-field> | ||
<mat-label>Interval</mat-label> | ||
<input matInput [formControl]="intervalControl"/> | ||
</mat-form-field> | ||
|
||
<mat-form-field> | ||
<mat-label>Min time</mat-label> | ||
<input matInput [matTimepicker]="minPicker" [formControl]="minControl"> | ||
<mat-timepicker #minPicker/> | ||
<mat-timepicker-toggle [for]="minPicker" matSuffix/> | ||
</mat-form-field> | ||
|
||
<mat-form-field> | ||
<mat-label>Max time</mat-label> | ||
<input matInput [matTimepicker]="maxPicker" [formControl]="maxControl"> | ||
<mat-timepicker #maxPicker/> | ||
<mat-timepicker-toggle [for]="maxPicker" matSuffix/> | ||
</mat-form-field> | ||
</div> | ||
</mat-card-content> | ||
</mat-card> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
// TODO | ||
.demo-row { | ||
display: flex; | ||
align-items: flex-start; | ||
gap: 100px; | ||
} | ||
|
||
.demo-card { | ||
width: 600px; | ||
max-width: 100%; | ||
flex-shrink: 0; | ||
} | ||
|
||
.demo-form-fields { | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 0 2%; | ||
margin-top: 16px; | ||
|
||
mat-form-field { | ||
flex-basis: 49%; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.