1 |
|
2 | title: "Text field character counter"
|
3 | layout: detail
|
4 | section: components
|
5 | excerpt: "Character counter displays the ratio of characters used and the total character limit"
|
6 | iconId: text_field
|
7 | path: /catalog/input-controls/text-field/character-counter/
|
8 | -->
|
9 |
|
10 | # Text field character counter
|
11 |
|
12 | Character counter is used if there is a character limit. It displays the ratio of characters used and the total character limit.
|
13 |
|
14 | ## Basic usage
|
15 |
|
16 | ### HTML structure
|
17 |
|
18 | ```html
|
19 | <div class="mdc-text-field-character-counter">0 / 140</div>
|
20 | ```
|
21 |
|
22 | > NOTE: Place this inside `.mdc-text-field-helper-line` for single line mdc text field which is an immediate sibling of `.mdc-text-field` and
|
23 | > place it as first element of `.mdc-text-field` for multi-line text field variant (textarea).
|
24 |
|
25 | ### Styles
|
26 |
|
27 | ```scss
|
28 | @use "@material/textfield/character-counter";
|
29 |
|
30 | @include character-counter.character-counter-core-styles;
|
31 | ```
|
32 |
|
33 | ### JavaScript instantiation
|
34 |
|
35 | ```js
|
36 | import {MDCTextFieldCharacterCounter} from '@material/textfield/character-counter';
|
37 |
|
38 | const characterCounter = new MDCTextFieldCharacterCounter(document.querySelector('.mdc-text-field-character-counter'));
|
39 | ```
|
40 |
|
41 | ## API
|
42 |
|
43 | ### CSS classes
|
44 |
|
45 | CSS Class | Description
|
46 | --- | ---
|
47 | `mdc-text-field-character-counter` | Mandatory.
|
48 |
|
49 | ### Sass mixins
|
50 |
|
51 | Mixin | Description
|
52 | --- | ---
|
53 | `mdc-text-field-character-counter-color($color)` | Customizes the color of the character counter associated with an enabled text field.
|
54 | `mdc-text-field-disabled-character-counter-color($color)` | Customizes the color of the character counter associated with a disabled text field.
|
55 | `mdc-text-field-character-counter-position($xOffset, $yOffset)` | Positions the character counter element inside text field. Used only for textarea variant.
|
56 |
|
57 | ## `MDCTextFieldCharacterCounter` properties and methods
|
58 |
|
59 | Property | Value Type | Description
|
60 | --- | --- | ---
|
61 | `foundation` | `MDCTextFieldCharacterCounterFoundation` | Returns the character counter's foundation. This allows the parent `MDCTextField` component to access the public methods on the `MDCTextFieldCharacterCounterFoundation` class.
|
62 |
|
63 | ## Usage within frameworks
|
64 |
|
65 | If you are using a JavaScript framework, such as React or Angular, you can create a Character Counter for your framework. Depending on your needs, you can use the _Simple Approach: Wrapping MDC Web Vanilla Components_, or the _Advanced Approach: Using Foundations and Adapters_. Please follow the instructions [here](../../../docs/integrating-into-frameworks.md).
|
66 |
|
67 | ### `MDCTextFieldCharacterCounterAdapter`
|
68 |
|
69 | Method Signature | Description
|
70 | --- | ---
|
71 | `setContent(content: string) => void` | Sets the text content of character counter element.
|
72 |
|
73 | ### `MDCTextFieldCharacterCounterFoundation`
|
74 |
|
75 | Method Signature | Description
|
76 | --- | ---
|
77 | `setCounterValue(currentLength: number, maxLength: number) => void` | Sets the character counter values including characters used and total character limit.
|