This repository was archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
feat(text-field): character counter #861
Merged
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
c08b2a7
fix(chips): ClientRect/DOMRect is not IE11 compatible (#855)
cpboyd 1d74bdc
feat(text-field): create character-counter
cf24b80
feat(text-field): delete state
536e233
feat(text-field): use constant
706e30d
feat(text-field): render character counter
e2d0f4c
feat(text-field): use inputProps in characterCounterProps
d2e47d0
feat(text-field): textarea render case
573311e
test(text-field): create unit test
8c4c383
fix(chips): ClientRect/DOMRect is not IE11 compatible (#855)
cpboyd 317c1b3
chore: add prettier (#863)
lucasecdb 624f1c8
Merge branch 'rc0.13.0' into rc0.13.0
2bba304
test(text-field): recover inputProps test
99c8897
test(text-field): Character counter renders in front of input when ta…
c660984
test(text-field): Character counter renders in front of input when ta…
0d0122f
test(text-field): template test
81a30f2
test(text-field): Useless test for test coverage
c808552
test(text-field): unmount test
c4303eb
test(text-field): screenshot test
4a9e9c1
test(text-field): add screenshot test url
01437ad
feat(text-field): update golden.json
fce303d
test(text-field): add characterCounterMap
b517e3a
feat(text-field): update golden.json
7649f90
fix(chips, text-field): add null check for foundation (#675) (#870)
Markus-Schwer 2a07566
feat(text-field): apply prettier
e7775b6
docs(text-field): character-counter README.md
0a83e71
fix(select): className option is passed to select and overrides selec…
gugu 11a0372
test(text-field): set maxLength
517c7e3
feat(text-field): sync width text-field to helper-line
5b151e5
test(text-field): screenshot test for characterCounter
aef0913
fix(select): componentDidUpdate handles disabled prop change (#877)
gugu c41d809
fix(select): edit * imports (#874)
15046f8
fix(list): reflect code reviews
e88d667
test(list): revert screenshot test
6343cd1
test(list): test for template and textarea
ba07b50
test(list): remove inline style
8febf2c
docs(text-field): Update label description (#896)
AllanWang af1b20b
feat(list): add ripple to list-item (#894)
461aee0
Update golden.json
d45afbe
fix(chips): error with undefined or null children (#883)
lucasecdb a254376
fix(text-field): autofocus floats label (#875)
f542a25
Merge branch 'rc0.13.0' into rc0.13.0
2e11633
test(list): inputProps is change to getter
b7df35b
Merge branch 'rc0.14.0' into rc0.13.0
d0a6bb4
test(list): combine character-counter screeneshot
ba27ef5
Update golden.json
963d359
fix(text-field): fix lint
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
# React Text Field Character Counter | ||
|
||
MDC React Text Field Character Counter is a React Component which uses [MDC Text Field Character Counter](https://github.com/material-components/material-components-web/tree/master/packages/mdc-textfield/character-counter)'s Sass and Foundational JavaScript logic. | ||
|
||
## Usage | ||
|
||
```js | ||
import CharacterCounter from '@material/react-text-field/character-counter/index.js'; | ||
|
||
const MyComponent = () => { | ||
return ( | ||
<CharacterCounter /> | ||
); | ||
} | ||
``` | ||
|
||
## Props | ||
|
||
Prop Name | Type | Description | ||
--- | --- | --- | ||
className | String | CSS classes for element. | ||
template | String | You can set custom template. [See below](#custom-template) | ||
|
||
## Custom Template | ||
|
||
CharacterCounter provides customization with the `template` prop in CharacterCounter. | ||
The `template` prop accepts the `${count}` and `${maxLength}` arguments. | ||
The default template is `${count} / ${maxLength}`, so it appears `0 / 140`. | ||
If you set template as `${count} : ${maxLength}`, it appears as `0 : 140`. | ||
|
||
### Sample | ||
|
||
``` js | ||
import React from 'react'; | ||
import TextField, {CharacterCounter, Input} from '@material/react-text-field'; | ||
|
||
class MyApp extends React.Component { | ||
state = {value: 'Happy Coding!'}; | ||
|
||
render() { | ||
return ( | ||
<TextField characterCounter={<CharacterCounter template='${count} : ${maxLength}' />}> | ||
<Input | ||
maxLength={140} | ||
value={this.state.value} | ||
onChange={(e) => this.setState({value: e.target.value})} | ||
/> | ||
</TextField> | ||
); | ||
} | ||
} | ||
``` | ||
|
||
## Sass Mixins | ||
|
||
Sass mixins may be available to customize various aspects of the Components. Please refer to the | ||
MDC Web repository for more information on what mixins are available, and how to use them. | ||
|
||
[Advanced Sass Mixins](https://github.com/material-components/material-components-web/tree/master/packages/mdc-textfield/character-counter#sass-mixins) |
This file contains hidden or 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,23 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 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/textfield/character-counter/mdc-text-field-character-counter"; |
This file contains hidden or 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,93 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 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 React from 'react'; | ||
import classnames from 'classnames'; | ||
import {MDCTextFieldCharacterCounterAdapter} from '@material/textfield/character-counter/adapter'; | ||
import {MDCTextFieldCharacterCounterFoundation} from '@material/textfield/character-counter/foundation'; | ||
|
||
const cssClasses = MDCTextFieldCharacterCounterFoundation.cssClasses; | ||
|
||
const TEMPLATE = { | ||
COUNT: '${count}', | ||
MAX_LENGTH: '${maxLength}', | ||
}; | ||
|
||
export interface CharacterCounterProps extends React.HTMLProps<HTMLDivElement> { | ||
count?: number; | ||
maxLength?: number; | ||
template?: string; | ||
} | ||
|
||
export default class CharacterCounter extends React.Component< | ||
CharacterCounterProps | ||
> { | ||
foundation = new MDCTextFieldCharacterCounterFoundation(this.adapter); | ||
|
||
componentWillUnmount() { | ||
this.foundation.destroy(); | ||
} | ||
|
||
get adapter(): MDCTextFieldCharacterCounterAdapter { | ||
return { | ||
// Please manage content through JSX | ||
setContent: () => undefined, | ||
}; | ||
} | ||
|
||
renderTemplate(template: string) { | ||
const {count = 0, maxLength = 0} = this.props; | ||
|
||
return template | ||
.replace(TEMPLATE.COUNT, count.toString()) | ||
.replace(TEMPLATE.MAX_LENGTH, maxLength.toString()); | ||
} | ||
|
||
get classes() { | ||
return classnames(cssClasses.ROOT, this.props.className); | ||
} | ||
|
||
get otherProps() { | ||
const { | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
className, | ||
count, | ||
maxLength, | ||
template, | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
...otherProps | ||
} = this.props; | ||
|
||
return otherProps; | ||
} | ||
|
||
render() { | ||
const {template} = this.props; | ||
|
||
return ( | ||
<div className={this.classes} {...this.otherProps}> | ||
{this.renderTemplate( | ||
template ? template : `${TEMPLATE.COUNT} / ${TEMPLATE.MAX_LENGTH}` | ||
)} | ||
</div> | ||
); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.