• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# advanced.Counter
2
3The **Counter** component is a component used to precisely adjust numerical values.
4
5>  **NOTE**
6>
7>  This component is supported since API version 11. Updates will be marked with a superscript to indicate their earliest API version.
8>
9>  If the **Counter** component has [universal attributes](ts-component-general-attributes.md) and [universal events](ts-component-general-events.md) configured, the compilation toolchain automatically generates an additional **__Common__** node and mounts the universal attributes and events on this node rather than the **Counter** component itself. As a result, the configured universal attributes and events may fail to take effect or behave as intended. For this reason, avoid using universal attributes and events with the **Counter** component.
10
11## Modules to Import
12
13```
14import { CounterType, CounterComponent, CounterOptions, DateData } from '@kit.ArkUI';
15```
16
17## Child Components
18
19Not supported
20
21## CounterComponent
22
23CounterComponent({ options: CounterOptions })
24
25Defines a counter.
26
27**Decorator**: @Component
28
29**Atomic service API**: This API can be used in atomic services since API version 12.
30
31**System capability**: SystemCapability.ArkUI.ArkUI.Full
32
33**Parameters**
34
35| Name  | Type                             | Mandatory| Decorator| Description                   |
36| ------- | --------------------------------- | ---- | ---------- | ----------------------- |
37| options | [CounterOptions](#counteroptions) | Yes  | @Prop      | Parameters of the counter.|
38
39## CounterOptions
40
41Defines the type and style parameters of the counter.
42
43**Atomic service API**: This API can be used in atomic services since API version 12.
44
45**System capability**: SystemCapability.ArkUI.ArkUI.Full
46
47| Name       | Type      | Mandatory| Description                           |
48| ----------- | ---------- | ---- | ------------------------------- |
49| type | [CounterType](#countertype) | Yes  | Type of the current counter.|
50| direction<sup>12+</sup> | [Direction](ts-appendix-enums.md#direction) | No| Layout direction.<br>Default value: **Direction.Auto**|
51| numberOptions | [NumberStyleOptions](#numberstyleoptions) | No   | Parameters of the number style counter.|
52| inlineOptions | [InlineStyleOptions](#inlinestyleoptions) | No| Parameters of the inline number style counter.|
53| dateOptions | [DateStyleOptions](#datestyleoptions) | No| Parameters of the inline date style counter.|
54
55A counter type must go with parameters of the matching counter style. Below is a mapping between the counter types and counter styles.
56
57| Counter Type            | Counter Style       |
58| ----------------------- | ------------------ |
59| CounterType.LIST        | NumberStyleOptions |
60| CounterType.COMPACT     | NumberStyleOptions |
61| CounterType.INLINE      | InlineStyleOptions |
62| CounterType.INLINE_DATE | DateStyleOptions   |
63
64## CounterType
65
66Enumerates the counter types.
67
68**Atomic service API**: This API can be used in atomic services since API version 12.
69
70**System capability**: SystemCapability.ArkUI.ArkUI.Full
71
72| Name       | Value  | Description                       |
73| ----------- | ---- | --------------------------- |
74| LIST        | 0    | List counter.            |
75| COMPACT     | 1    | Compact counter.            |
76| INLINE      | 2    | Inline number counter.|
77| INLINE_DATE | 3    | Inline date counter.      |
78
79## CommonOptions
80
81Defines common attributes and events of counters.
82
83**Atomic service API**: This API can be used in atomic services since API version 12.
84
85**System capability**: SystemCapability.ArkUI.ArkUI.Full
86
87| Name           | Type                     | Mandatory| Description                                                        |
88| --------------- | ------------------------- | ---- | ------------------------------------------------------------ |
89| focusable       | boolean                   | No  | Whether the counter is focusable.<br>**NOTE**<br>This attribute takes effect for the number style counter.<br>Default value: **true**<br>**true**: The counter is focusable.<br>**false**: The counter is not focusable.|
90| step            | number                    | No  | Step of the counter.<br>Value range: an integer greater than or equal to 1.<br>Default value: **1**|
91| onHoverIncrease | (isHover: boolean) => void | No  | Callback invoked when the mouse pointer is moved over or away from the increase button of the counter.<br>**isHover**: whether the mouse pointer hovers over the component. The value **true** means that the mouse pointer enters the component, and the value **false** means that the mouse pointer leaves the component.|
92| onHoverDecrease | (isHover: boolean) => void | No  | Callback invoked when the mouse pointer is moved over or away from the decrease button of the counter.<br>**isHover**: whether the mouse pointer hovers over the component. The value **true** means that the mouse pointer enters the component, and the value **false** means that the mouse pointer leaves the component.|
93
94## InlineStyleOptions
95
96Defines the attributes and events of the inline number style counter.
97
98Inherits from [CommonOptions](#commonoptions).
99
100**Atomic service API**: This API can be used in atomic services since API version 12.
101
102**System capability**: SystemCapability.ArkUI.ArkUI.Full
103
104| Name     | Type                  | Mandatory| Description                                                  |
105| --------- | ---------------------- | ---- | ------------------------------------------------------ |
106| value     | number                 | No  | Initial value of the counter.<br>Default value: **0**                   |
107| min       | number                 | No  | Minimum value of the counter.<br>Default value: **0**                   |
108| max       | number                 | No  | Maximum value of the counter.<br>Default value: **999**                 |
109| textWidth | number                 | No  | Text width of the counter.<br>Default value: **0**                    |
110| onChange  | (value: number) => void | No  | Callback invoked when the value changes. The current value is returned.<br>**value**: current value.|
111
112## NumberStyleOptions
113
114Defines the attributes and events of the number style counter.
115
116Inherits from [InlineStyleOptions](#inlinestyleoptions).
117
118**Atomic service API**: This API can be used in atomic services since API version 12.
119
120**System capability**: SystemCapability.ArkUI.ArkUI.Full
121
122| Name           | Type                                  | Mandatory| Description                                         |
123| --------------- | -------------------------------------- | ---- | --------------------------------------------- |
124| label           | [ResourceStr](ts-types.md#resourcestr) | No  | Label of the counter.                      |
125| onFocusIncrease | () => void                              | No  | Callback invoked when the increase button of the counter gains focus.|
126| onFocusDecrease | () => void                              | No  | Callback invoked when the decrease button of the counter gains focus.|
127| onBlurIncrease  | () => void                              | No  | Callback invoked when the increase button of the counter loses focus.|
128| onBlurDecrease  | () => void                              | No  | Callback invoked when the decrease button of the counter loses focus.|
129
130## DateStyleOptions
131
132Defines the attributes and events of the inline date style counter.
133
134Inherits from [CommonOptions](#commonoptions).
135
136**Atomic service API**: This API can be used in atomic services since API version 12.
137
138**System capability**: SystemCapability.ArkUI.ArkUI.Full
139
140| Name        | Type                               | Mandatory| Description                                                     |
141| ------------ | ----------------------------------- | ---- | --------------------------------------------------------- |
142| year         | number                              | No  | Initial year of the counter.<br>Default value: **1**                   |
143| month        | number                              | No  | Initial month of the counter.<br>Default value: **1**                   |
144| day          | number                              | No  | Initial day of the counter.<br>Default value: **1**                     |
145| onDateChange | (date: [DateData](#datedata)) => void | No  | Callback invoked when the date changes. The current date is returned.<br>**date**: current date.|
146
147## DateData
148
149Defines common date attributes and methods.
150
151**Atomic service API**: This API can be used in atomic services since API version 12.
152
153**System capability**: SystemCapability.ArkUI.ArkUI.Full
154
155| Name | Type  | Read-Only| Optional| Description                    |
156| ----- | ------ | ---- | ---- | ------------------------ |
157| year  | number | No  | No  | Initial year of the counter.|
158| month | number | No  | No  | Initial month of the counter.|
159| day   | number | No  | No  | Initial day of the counter.  |
160
161### constructor
162
163constructor(year: number, month: number, day: number)
164
165A constructor used to create a **DateData** object.
166
167**Atomic service API**: This API can be used in atomic services since API version 12.
168
169**System capability**: SystemCapability.ArkUI.ArkUI.Full
170
171| Name| Type| Description|
172| ---------- | ------ | ---------------------------- |
173| year       | number | Initial year of the counter.    |
174| month      | number | Initial month of the counter.    |
175| day        | number | Initial day of the counter.      |
176
177### toString
178
179toString(): string
180
181Current date.
182
183**Atomic service API**: This API can be used in atomic services since API version 12.
184
185**System capability**: SystemCapability.ArkUI.ArkUI.Full
186
187**Return value**
188
189| Type| Description|
190| -------- | -------- |
191| string | Current date.|
192
193## Example
194
195### Example 1: Implementing a List Counter
196
197This example implements a list counter by setting **type** to **CounterType.LIST** and configuring **numberOptions**.
198
199```ts
200import { CounterType, CounterComponent } from '@kit.ArkUI';
201
202@Entry
203@Component
204struct ListCounterExample {
205  build() {
206    Column() {
207      // List counter
208      CounterComponent({
209        options: {
210          type: CounterType.LIST,
211          numberOptions: {
212            label: "Price",
213            min: 0,
214            value: 5,
215            max: 10
216          }
217        }
218      })
219    }
220  }
221}
222```
223
224![listcounter](figures/listcounter.gif)
225
226### Example 2: Implementing a Compact Counter
227
228This example implements a compact counter by setting **type** to **CounterType.COMPACT** and configuring **numberOptions**.
229
230```ts
231import { CounterType, CounterComponent } from '@kit.ArkUI';
232
233@Entry
234@Component
235struct CompactCounterExample {
236  build() {
237    Column() {
238      // Compact counter
239      CounterComponent({
240        options: {
241          type: CounterType.COMPACT,
242          numberOptions: {
243            label: "Quantity",
244            value: 10,
245            min: 0,
246            max: 100,
247            step: 10
248          }
249        }
250      })
251    }
252  }
253}
254```
255
256![compactcounter](figures/compactcounter.gif)
257
258### Example 3: Implementing an Inline Number Counter
259
260This example implements an inline number counter by setting **type** to **CounterType.INLINE** and configuring **inlineOptions**.
261
262```ts
263import { CounterType, CounterComponent } from '@kit.ArkUI';
264
265@Entry
266@Component
267struct NumberStyleExample {
268  build() {
269    Column() {
270      // Inline number counter
271      CounterComponent({
272        options: {
273          type: CounterType.INLINE,
274          inlineOptions: {
275            value: 100,
276            min: 10,
277            step: 2,
278            max: 1000,
279            textWidth: 100,
280            onChange: (value: number) => {
281              console.log("onDateChange Date: " + value.toString());
282            }
283          }
284        }
285      })
286    }
287  }
288}
289```
290
291![numberstyle](figures/numberstyle.gif)
292
293### Example 4: Implementing an Inline Date Counter
294
295This example implements an inline date counter by setting **type** to **CounterType.INLINE_DATE** and configuring **dateOptions**, allowing for manual date input.
296
297```ts
298import { CounterType, CounterComponent, DateData } from '@kit.ArkUI';
299
300@Entry
301@Component
302struct DataStyleExample {
303  build() {
304    Column() {
305      // Inline date counter
306      CounterComponent({
307        options: {
308          type: CounterType.INLINE_DATE,
309          dateOptions: {
310            year: 2016,
311            onDateChange: (date: DateData) => {
312              console.log("onDateChange Date: " + date.toString());
313            }
314          }
315        }
316      })
317    }
318  }
319}
320```
321
322![datestyle](figures/datestyle.gif)
323
324### Example 5: Implementing a Mirrored Layout
325
326This example implements a mirrored layout for list, compact, inline number, and inline date counters by setting **direction**.
327
328```ts
329// xxx.ets
330import { CounterType, CounterComponent, DateData } from '@kit.ArkUI';
331
332@Entry
333@Component
334struct CounterPage {
335  @State currentDirection: Direction = Direction.Rtl
336
337  build() {
338    Column({}) {
339
340      // List counter
341      CounterComponent({
342        options: {
343          direction: this.currentDirection,
344          type: CounterType.LIST,
345          numberOptions: {
346            label: "Price",
347            min: 0,
348            value: 5,
349            max: 10,
350          }
351        }
352      })
353        .width('80%')
354
355      // Compact counter
356      CounterComponent({
357        options: {
358          direction: this.currentDirection,
359          type: CounterType.COMPACT,
360          numberOptions: {
361            label: "Quantity",
362            value: 10,
363            min: 0,
364            max: 100,
365            step: 10
366          }
367        }
368      }).margin({ top: 20 })
369
370      // Inline number counter
371      CounterComponent({
372        options: {
373          type: CounterType.INLINE,
374          direction: this.currentDirection,
375          inlineOptions: {
376            value: 100,
377            min: 10,
378            step: 2,
379            max: 1000,
380            textWidth: 100,
381            onChange: (value: number) => {
382              console.log("onDateChange Date: " + value.toString());
383            }
384          }
385        }
386      }).margin({ top: 20 })
387      // Inline date counter
388      CounterComponent({
389        options: {
390          direction: this.currentDirection,
391          type: CounterType.INLINE_DATE,
392          dateOptions: {
393            year: 2024,
394            onDateChange: (date: DateData) => {
395              console.log("onDateChange Date: " + date.toString());
396            }
397          }
398        }
399      }).margin({ top: 20 })
400    }
401    .width('100%')
402    .height('100%')
403    .justifyContent(FlexAlign.Center)
404    .alignItems(HorizontalAlign.Center)
405  }
406}
407```
408
409![datestyle](figures/counter_direction.png)
410