This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(text-field): Added support for character counter. (#4244)
- Loading branch information
Showing
50 changed files
with
1,728 additions
and
374 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<!--docs: | ||
title: "Text Field Character Counter" | ||
layout: detail | ||
section: components | ||
excerpt: "Character counter displays the ratio of characters used and the total character limit" | ||
iconId: text_field | ||
path: /catalog/input-controls/text-field/character-counter/ | ||
--> | ||
|
||
# Text Field Character Counter | ||
|
||
Character counter is used if there is a character limit. It displays the ratio of characters used and the total character limit. | ||
|
||
## Design & API Documentation | ||
|
||
<ul class="icon-list"> | ||
<li class="icon-list-item icon-list-item--spec"> | ||
<a href="https://material.io/go/design-text-fields#text-fields-layout">Material Design guidelines: Text Fields Layout</a> | ||
</li> | ||
</ul> | ||
|
||
## Basic Usage | ||
|
||
### HTML Structure | ||
|
||
```html | ||
<div class="mdc-text-field-character-counter">0 / 140</div> | ||
``` | ||
|
||
> 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 | ||
> place it as first element of `.mdc-text-field` for multi-line text field variant (textarea). | ||
### Styles | ||
|
||
```scss | ||
@import "@material/textfield/character-counter/mdc-text-field-character-counter"; | ||
``` | ||
|
||
### JavaScript Instantiation | ||
|
||
```js | ||
import {MDCTextFieldCharacterCounter} from '@material/textfield/character-counter'; | ||
|
||
const characterCounter = new MDCTextFieldCharacterCounter(document.querySelector('.mdc-text-field-character-counter')); | ||
``` | ||
|
||
## Style Customization | ||
|
||
### CSS Classes | ||
|
||
CSS Class | Description | ||
--- | --- | ||
`mdc-text-field-character-counter` | Mandatory. | ||
|
||
### Sass Mixins | ||
|
||
Mixin | Description | ||
--- | --- | ||
`mdc-text-field-character-counter-color($color)` | Customizes the color of the character counter associated with text field. | ||
`mdc-text-field-character-counter-position($xOffset, $yOffset)` | Positions the character counter element inside text field. Used only for textarea variant. | ||
|
||
## `MDCTextFieldCharacterCounter` Properties and Methods | ||
|
||
Property | Value Type | Description | ||
--- | --- | --- | ||
`foundation` | `MDCTextFieldCharacterCounterFoundation` | Returns the character counter's foundation. This allows the parent `MDCTextField` component to access the public methods on the `MDCTextFieldCharacterCounterFoundation` class. | ||
|
||
## Usage Within Frameworks | ||
|
||
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). | ||
|
||
### `MDCTextFieldCharacterCounterAdapter` | ||
|
||
Method Signature | Description | ||
--- | --- | ||
`setContent(content: string) => void` | Sets the text content of character counter element. | ||
|
||
### `MDCTextFieldCharacterCounterFoundation` | ||
|
||
Method Signature | Description | ||
--- | --- | ||
`setCounterValue(currentLength: number, maxLength: number) => void` | Sets the character counter values including characters used and total character limit. |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Copyright 2019 Google Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
|
||
@import "@material/rtl/mixins"; | ||
|
||
// Public mixins | ||
|
||
@mixin mdc-text-field-character-counter-color($color) { | ||
&:not(.mdc-text-field--disabled) { | ||
@include mdc-text-field-character-counter-color_($color); | ||
} | ||
} | ||
|
||
@mixin mdc-text-field-character-counter-position($xOffset, $yOffset) { | ||
.mdc-text-field-character-counter { | ||
@include mdc-rtl-reflexive-position(right, $xOffset); | ||
|
||
position: absolute; | ||
bottom: $yOffset; | ||
} | ||
} | ||
|
||
// Private mixins | ||
|
||
@mixin mdc-text-field-character-counter-color_($color) { | ||
// Character counter is placed inside mdc-textfield element (for textarea variant ) or | ||
// inside helper line which is sibling to mdc-textfield. | ||
.mdc-text-field-character-counter, | ||
+ .mdc-text-field-helper-line .mdc-text-field-character-counter { | ||
@include mdc-theme-prop(color, $color); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Google Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
/* eslint no-unused-vars: [2, {"args": "none"}] */ | ||
|
||
/** | ||
* Adapter for MDC Text Field Character Counter. | ||
* | ||
* Defines the shape of the adapter expected by the foundation. Implement this | ||
* adapter to integrate the TextField character counter into your framework. See | ||
* https://github.com/material-components/material-components-web/blob/master/docs/authoring-components.md | ||
* for more information. | ||
* | ||
* @record | ||
*/ | ||
class MDCTextFieldCharacterCounterAdapter { | ||
/** | ||
* Sets the text content of character counter element. | ||
* @param {string} content | ||
*/ | ||
setContent(content) {} | ||
} | ||
|
||
export default MDCTextFieldCharacterCounterAdapter; |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Google Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
/** @enum {string} */ | ||
const cssClasses = { | ||
ROOT: 'mdc-text-field-character-counter', | ||
}; | ||
|
||
/** @enum {string} */ | ||
const strings = { | ||
ROOT_SELECTOR: `.${cssClasses.ROOT}`, | ||
}; | ||
|
||
export {strings, cssClasses}; |
Oops, something went wrong.
This is a breaking change that wasn't communicated anywhere! All existing helper text no longer works unless its wrapped.