Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

feat(text-field): character counter #861

Merged
merged 46 commits into from Jun 3, 2019
Merged
Show file tree
Hide file tree
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 May 10, 2019
1d74bdc
feat(text-field): create character-counter
May 12, 2019
cf24b80
feat(text-field): delete state
May 12, 2019
536e233
feat(text-field): use constant
May 12, 2019
706e30d
feat(text-field): render character counter
May 12, 2019
e2d0f4c
feat(text-field): use inputProps in characterCounterProps
May 12, 2019
d2e47d0
feat(text-field): textarea render case
May 12, 2019
573311e
test(text-field): create unit test
May 12, 2019
8c4c383
fix(chips): ClientRect/DOMRect is not IE11 compatible (#855)
cpboyd May 10, 2019
317c1b3
chore: add prettier (#863)
lucasecdb May 15, 2019
624f1c8
Merge branch 'rc0.13.0' into rc0.13.0
May 16, 2019
2bba304
test(text-field): recover inputProps test
May 16, 2019
99c8897
test(text-field): Character counter renders in front of input when ta…
May 16, 2019
c660984
test(text-field): Character counter renders in front of input when ta…
May 16, 2019
0d0122f
test(text-field): template test
May 16, 2019
81a30f2
test(text-field): Useless test for test coverage
May 16, 2019
c808552
test(text-field): unmount test
May 16, 2019
c4303eb
test(text-field): screenshot test
May 16, 2019
4a9e9c1
test(text-field): add screenshot test url
May 16, 2019
01437ad
feat(text-field): update golden.json
May 17, 2019
fce303d
test(text-field): add characterCounterMap
May 18, 2019
b517e3a
feat(text-field): update golden.json
May 18, 2019
7649f90
fix(chips, text-field): add null check for foundation (#675) (#870)
Markus-Schwer May 20, 2019
2a07566
feat(text-field): apply prettier
May 23, 2019
e7775b6
docs(text-field): character-counter README.md
May 23, 2019
0a83e71
fix(select): className option is passed to select and overrides selec…
gugu May 23, 2019
11a0372
test(text-field): set maxLength
May 24, 2019
517c7e3
feat(text-field): sync width text-field to helper-line
May 24, 2019
5b151e5
test(text-field): screenshot test for characterCounter
May 25, 2019
aef0913
fix(select): componentDidUpdate handles disabled prop change (#877)
gugu May 28, 2019
c41d809
fix(select): edit * imports (#874)
May 28, 2019
15046f8
fix(list): reflect code reviews
May 29, 2019
e88d667
test(list): revert screenshot test
May 29, 2019
6343cd1
test(list): test for template and textarea
May 29, 2019
ba07b50
test(list): remove inline style
May 29, 2019
8febf2c
docs(text-field): Update label description (#896)
AllanWang May 29, 2019
af1b20b
feat(list): add ripple to list-item (#894)
May 29, 2019
461aee0
Update golden.json
May 29, 2019
d45afbe
fix(chips): error with undefined or null children (#883)
lucasecdb May 30, 2019
a254376
fix(text-field): autofocus floats label (#875)
May 30, 2019
f542a25
Merge branch 'rc0.13.0' into rc0.13.0
May 30, 2019
2e11633
test(list): inputProps is change to getter
May 30, 2019
b7df35b
Merge branch 'rc0.14.0' into rc0.13.0
May 31, 2019
d0a6bb4
test(list): combine character-counter screeneshot
Jun 1, 2019
ba27ef5
Update golden.json
Jun 3, 2019
963d359
fix(text-field): fix lint
Jun 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/list/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import {MDCListFoundation} from '@material/list/foundation';
import {ListItemContext, ListItemContextShape} from './index';

export interface ListItemProps<T extends HTMLElement = HTMLElement>
extends React.HTMLProps<T>, ListItemContextShape, InjectedProps<T> {
extends React.HTMLProps<T>,
ListItemContextShape,
InjectedProps<T> {
checkboxList?: boolean;
radioList?: boolean;
tag?: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/menu/MenuListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class MenuListItem<T extends HTMLElement = HTMLElement> extends React.Component<
const {
role = 'menuitem',
children,
/* eslint-disable no-unused-vars */
/* eslint-disable @typescript-eslint/no-unused-vars */
computeBoundingRect,
/* eslint-disable no-unused-vars */
/* eslint-disable @typescript-eslint/no-unused-vars */
...otherProps
} = this.props;

Expand Down
59 changes: 59 additions & 0 deletions packages/text-field/character-counter/README.md
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)
23 changes: 23 additions & 0 deletions packages/text-field/character-counter/index.scss
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";
93 changes: 93 additions & 0 deletions packages/text-field/character-counter/index.tsx
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>
);
}
}
55 changes: 45 additions & 10 deletions packages/text-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import {
} from '@material/textfield/adapter';
import {MDCTextFieldFoundation} from '@material/textfield/foundation';
import Input, {InputProps} from './Input';
import Icon, {IconProps} from './icon/index';
import HelperText, {HelperTextProps} from './helper-text/index';
import Icon, {IconProps} from './icon';
import HelperText, {HelperTextProps} from './helper-text';
import CharacterCounter, {CharacterCounterProps} from './character-counter';
import FloatingLabel from '@material/react-floating-label';
import LineRipple from '@material/react-line-ripple';
import NotchedOutline from '@material/react-notched-outline';
Expand All @@ -48,7 +49,7 @@ export interface Props<T extends HTMLElement = HTMLInputElement> {
floatingLabelClassName?: string;
fullWidth?: boolean;
helperText?: React.ReactElement<HelperTextProps>;
characterCounter?: React.ReactElement<any>;
characterCounter?: React.ReactElement<CharacterCounterProps>;
label?: React.ReactNode;
leadingIcon?: React.ReactElement<React.HTMLProps<HTMLOrSVGElement>>;
lineRippleClassName?: string;
Expand Down Expand Up @@ -82,6 +83,7 @@ interface TextFieldState {
class TextField<
T extends HTMLElement = HTMLInputElement
> extends React.Component<TextFieldProps<T>, TextFieldState> {
textFieldElement: React.RefObject<HTMLDivElement> = React.createRef();
floatingLabelElement: React.RefObject<FloatingLabel> = React.createRef();
inputComponent_: null | Input<T> = null;

Expand Down Expand Up @@ -175,6 +177,7 @@ class TextField<
floatingLabelClassName,
fullWidth,
helperText,
characterCounter,
label,
leadingIcon,
lineRippleClassName,
Expand Down Expand Up @@ -278,10 +281,11 @@ class TextField<
};
}

inputProps(child: React.ReactElement<InputProps<T>>) {
get inputProps() {
// ref does exist on React.ReactElement<InputProps<T>>
// @ts-ignore
const {props} = child;
const {props} = React.Children.only(this.props.children);

return Object.assign({}, props, {
foundation: this.state.foundation,
handleFocusChange: (isFocused: boolean) => {
Expand All @@ -300,6 +304,14 @@ class TextField<
});
}

get characterCounterProps() {
const {value, maxLength} = this.inputProps;
return {
count: value ? value.length : 0,
maxLength: maxLength ? parseInt(maxLength) : 0,
};
}

/**
* render methods
*/
Expand All @@ -323,11 +335,15 @@ class TextField<
className={this.classes}
onClick={() => foundation!.handleTextFieldInteraction()}
onKeyDown={() => foundation!.handleTextFieldInteraction()}
ref={this.textFieldElement}
key='text-field-container'
>
{leadingIcon
? this.renderIcon(leadingIcon, onLeadingIconSelect)
: null}
{textarea &&
characterCounter &&
this.renderCharacterCounter(characterCounter)}
{this.renderInput()}
{this.notchedOutlineAdapter.hasOutline() ? (
this.renderNotchedOutline()
Expand All @@ -352,8 +368,7 @@ class TextField<
const child: React.ReactElement<InputProps<T>> = React.Children.only(
this.props.children
);
const props = this.inputProps(child);
return React.cloneElement(child, props);
return React.cloneElement(child, this.inputProps);
}

renderLabel() {
Expand Down Expand Up @@ -402,12 +417,14 @@ class TextField<

renderHelperLine(
helperText?: React.ReactElement<HelperTextProps>,
characterCounter?: React.ReactElement<any>
characterCounter?: React.ReactElement<CharacterCounterProps>
) {
return (
<div className={cssClasses.HELPER_LINE}>
{helperText && this.renderHelperText(helperText)}
{characterCounter}
{characterCounter &&
!this.props.textarea &&
this.renderCharacterCounter(characterCounter)}
</div>
);
}
Expand Down Expand Up @@ -436,7 +453,25 @@ class TextField<
</Icon>
);
}

renderCharacterCounter(
characterCounter: React.ReactElement<CharacterCounterProps>
) {
return React.cloneElement(
characterCounter,
Object.assign(this.characterCounterProps, characterCounter.props)
);
}
}

export {Icon, HelperText, Input, IconProps, HelperTextProps, InputProps};
export {
Icon,
HelperText,
CharacterCounter,
Input,
IconProps,
HelperTextProps,
CharacterCounterProps,
InputProps,
};
export default TextField;
1 change: 1 addition & 0 deletions test/screenshot/golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"tab-bar": "6c28ec268b2baf308459e7df9d7471fb7907b6473240b9a28a81be54a335f932",
"tab-indicator": "7ce7ce8fd50301c67d7ebfb0ba953208260ce2881bee0c7e640c46bf60dc90b6",
"tab-scroller": "468866dd0c222b36b55485ab44a5760133a4ddfb2a6cf81e6ae4672d7e02a447",
"text-field/character-counter": "b6c744bd58b76dd7d3794fa84dae98e44a612b11f7e6dab895e91aceae8aba73",
"text-field/helper-text": "59604d0f39e0846fc97aae7573d317dded215282a677e4641c5e33426e3a2a1e",
"text-field/icon": "0bbc8c762d27071e55983e5742548d166864b6fcebc0b9f1e413523fb93b7075",
"text-field/textArea": "dde78e3f154a8b910a989f8ce96e320e7ad2b3e199e6e7a81034174c598cbd9d",
Expand Down
1 change: 1 addition & 0 deletions test/screenshot/screenshot-test-urls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const urls = [
'tab-bar',
'tab-indicator',
'tab-scroller',
'text-field/character-counter',
'text-field/helper-text',
'text-field/icon',
'typography',
Expand Down
1 change: 1 addition & 0 deletions test/screenshot/text-field/TestTextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TestField extends React.Component<TestFieldProps, TestFieldState> {
required={required}
disabled={disabled}
onChange={this.onChange}
maxLength={140}
/>
</TextField>
</div>
Expand Down
Loading