This repository has been archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
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): character counter (#861)
- Loading branch information
태재영
authored and
Matt Goo
committed
Jun 11, 2019
1 parent
407de75
commit c3f9439
Showing
16 changed files
with
419 additions
and
54 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
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 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 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 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
Oops, something went wrong.