ts-basic-components-datepicker.md 4.0 KB
Newer Older
Z
zengyawen 已提交
1 2
# DatePicker

E
ester.zhou 已提交
3
The **\<DatePicker>** component allows users to select a date from the given range.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6
>  **NOTE**
>
E
ester.zhou 已提交
7
>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9


E
esterzhou 已提交
10
## Child Components
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
Not supported
Z
zengyawen 已提交
13 14 15 16


## APIs

E
ester.zhou 已提交
17
DatePicker(options?: {start?: Date, end?: Date, selected?: Date})
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
Creates a date picker in the given date range.
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21
**Parameters**
E
ester.zhou 已提交
22

E
ester.zhou 已提交
23 24 25 26 27
| Name  | Type| Mandatory| Description                                                    |
| -------- | -------- | ---- | ------------------------------------------------------------ |
| start    | Date     | No  | Start date of the picker.<br>Default value: **Date('1970-1-1')**         |
| end      | Date     | No  | End date of the picker.<br>Default value: **Date('2100-12-31')**       |
| selected | Date     | No  | Date of the selected item.<br>Default value: current system date<br>Since API version 10, this parameter supports [$$](../../quick-start/arkts-two-way-sync.md) for two-way binding of variables.|
Z
zengyawen 已提交
28 29 30

## Attributes

E
ester.zhou 已提交
31 32 33 34 35 36 37 38
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.

| Name                              | Type                                    | Description                                      |
| -------------------------------- | ---------------------------------------- | ---------------------------------------- |
| lunar                            | boolean                                  | Whether to display the lunar calendar.<br>- **true**: Display the lunar calendar.<br>- **false**: Do not display the lunar calendar.<br>Default value: **false**|
| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](#pickertextstyle10) | Font color, font size, and font width for the top and bottom items.          |
| textStyle<sup>10+</sup>          | [PickerTextStyle](#pickertextstyle10) | Font color, font size, and font width of all items except the top, bottom, and selected items.      |
| selectedTextStyle<sup>10+</sup>  | [PickerTextStyle](#pickertextstyle10) | Font color, font size, and font width of the selected item.                     |
Z
zengyawen 已提交
39

E
ester.zhou 已提交
40 41 42 43 44 45
## PickerTextStyle<sup>10+</sup>

| Name  | Type                                    | Mandatory  | Description                     |
| ----- | ---------------------------------------- | ---- | ------------------------- |
| color | [ResourceColor](ts-types.md#resourcecolor) | No   | Font color.                    |
| font  | [Font](ts-types.md#font)                 | No   | Text style. Only the font size and font width are supported.|
Z
zengyawen 已提交
46 47 48

## Events

E
ester.zhou 已提交
49 50 51 52
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.

| Name                                      | Description       |
| ---------------------------------------- | ----------- |
E
ester.zhou 已提交
53
| onChange(callback: (value: DatePickerResult) =&gt; void) | Triggered when a date is selected.|
Z
zengyawen 已提交
54

E
ester.zhou 已提交
55 56
## DatePickerResult

E
ester.zhou 已提交
57 58 59
| Name   | Type  | Description                         |
| ----- | ------ | --------------------------- |
| year  | number | Year of the selected date.                    |
E
ester.zhou 已提交
60
| month | number | Month of the selected date. The value ranges from 0 to 11. The value **0** indicates January, and **11** indicates December.|
E
ester.zhou 已提交
61
| day   | number | Day of the selected date.                    |
Z
zengyawen 已提交
62 63 64 65 66


## Example


E
ester.zhou 已提交
67 68
```ts
// xxx.ets
Z
zengyawen 已提交
69 70
@Entry
@Component
E
ester.zhou 已提交
71 72
struct DatePickerExample {
  @State isLunar: boolean = false
Z
zengyawen 已提交
73 74 75 76
  private selectedDate: Date = new Date('2021-08-08')

  build() {
    Column() {
E
ester.zhou 已提交
77
      Button('Switch Calendar')
E
ester.zhou 已提交
78
        .margin({ top: 30, bottom: 30 })
E
ester.zhou 已提交
79 80 81
        .onClick(() => {
          this.isLunar = !this.isLunar
        })
Z
zengyawen 已提交
82 83
      DatePicker({
        start: new Date('1970-1-1'),
E
ester.zhou 已提交
84
        end: new Date('2100-1-1'),
E
ester.zhou 已提交
85
        selected: this.selectedDate
Z
zengyawen 已提交
86
      })
E
ester.zhou 已提交
87 88 89 90 91
        .lunar(this.isLunar)
        .onChange((value: DatePickerResult) => {
          this.selectedDate.setFullYear(value.year, value.month, value.day)
          console.info('select current date is: ' + JSON.stringify(value))
        })
Z
zengyawen 已提交
92 93 94 95 96 97

    }.width('100%')
  }
}
```

E
ester.zhou 已提交
98
![datePicker](figures/datePicker.gif)