Skip to content

TextField - charCount UX change #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions src/components/chipsInput/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('ChipsInput', () => {
});

it('should update state - tagIndexToRemove with last tag index', () => {
const pressEvent = {nativeEvent: {key: 'Backspace'}};
const pressEvent = {nativeEvent: {key: Constants.backspaceKey}};
uut.onKeyPress(pressEvent);
expect(uut.state.tagIndexToRemove).toBe(2);
});
Expand All @@ -54,7 +54,7 @@ describe('ChipsInput', () => {
});

it('should not update state if there are not tags', () => {
const pressEvent = {nativeEvent: {key: 'Backspace'}};
const pressEvent = {nativeEvent: {key: Constants.backspaceKey}};
_.set(uut.state, 'tags', []);
uut.onKeyPress(pressEvent);
expect(uut.state.tagIndexToRemove).toBe(undefined);
Expand All @@ -71,7 +71,7 @@ describe('ChipsInput', () => {
});

it('should not update state if input value is not empty', () => {
const pressEvent = {nativeEvent: {key: 'Backspace'}};
const pressEvent = {nativeEvent: {key: Constants.backspaceKey}};
_.set(uut.state, 'tags', [{}, {}, {}]);
_.set(uut.state, 'value', 'some text');
uut.onKeyPress(pressEvent);
Expand All @@ -89,7 +89,7 @@ describe('ChipsInput', () => {
});

it('should not set last tag index if it is already set to last index, instead call remove tag', () => {
const pressEvent = {nativeEvent: {key: 'Backspace'}};
const pressEvent = {nativeEvent: {key: Constants.backspaceKey}};
_.set(uut.state, 'tagIndexToRemove', 2);
uut.onKeyPress(pressEvent);
expect(removeTagSpy).toHaveBeenCalled();
Expand Down
2 changes: 1 addition & 1 deletion src/components/chipsInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export default class ChipsInput extends BaseComponent {
const tagsCount = _.size(tags);
const keyCode = _.get(event, 'nativeEvent.key');
const hasNoValue = _.isEmpty(value);
const pressedBackspace = Constants.isAndroid || keyCode === 'Backspace';
const pressedBackspace = Constants.isAndroid || keyCode === Constants.backspaceKey;
const hasTags = tagsCount > 0;

if (pressedBackspace) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/tagsInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export default class TagsInput extends BaseComponent {
const tagsCount = _.size(tags);
const keyCode = _.get(event, 'nativeEvent.key');
const hasNoValue = _.isEmpty(value);
const pressedBackspace = Constants.isAndroid || keyCode === 'Backspace';
const pressedBackspace = Constants.isAndroid || keyCode === Constants.backspaceKey;
const hasTags = tagsCount > 0;

if (pressedBackspace) {
Expand Down
32 changes: 24 additions & 8 deletions src/components/textField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ export default class TextField extends BaseInput {
value: props.value, // for floatingPlaceholder functionality
floatingPlaceholderState: new Animated.Value(this.shouldFloatPlaceholder(props.value) ? 1 : 0),
showExpandableModal: false,
floatingPlaceholderTranslate: 0
floatingPlaceholderTranslate: 0,
charCountColor: CHAR_COUNTER_COLOR_BY_STATE.default
};

this.generatePropsWarnings(props);
Expand Down Expand Up @@ -318,6 +319,23 @@ export default class TextField extends BaseInput {
return 0;
}

setCharCountColor(key) {
this.maxReached = key === Constants.backspaceKey ? false : this.isCounterLimit();
const color = this.state.focused && this.maxReached ?
CHAR_COUNTER_COLOR_BY_STATE.error : CHAR_COUNTER_COLOR_BY_STATE.default;

if (color !== this.state.charCountColor) {
this.setState({charCountColor: color});
}
}

getCharCountColor() {
const {charCountColor} = this.state;
const {disabledColor} = this.getThemeProps();

return this.isDisabled() && disabledColor ? disabledColor : charCountColor;
}

getTopPaddings() {
return this.shouldFakePlaceholder() ? (this.shouldShowTopError() ? undefined : 25) : undefined;
}
Expand Down Expand Up @@ -458,14 +476,11 @@ export default class TextField extends BaseInput {
}

renderCharCounter() {
const {focused} = this.state;
const {maxLength, showCharacterCounter, disabledColor} = this.getThemeProps();
const {maxLength, showCharacterCounter} = this.getThemeProps();

if (maxLength && showCharacterCounter) {
const counter = this.getCharCount();
const textColor =
this.isCounterLimit() && focused ? CHAR_COUNTER_COLOR_BY_STATE.error : CHAR_COUNTER_COLOR_BY_STATE.default;
const color = this.isDisabled() && disabledColor ? disabledColor : textColor;
const color = this.getCharCountColor();

return (
<Text
Expand Down Expand Up @@ -620,7 +635,7 @@ export default class TextField extends BaseInput {

return (
<TouchableOpacity
{...others} accessibilityLabel={accessibilityLabel}
{...others} accessibilityLabel={accessibilityLabel}
style={[this.styles.rightButton, style]} onPress={this.onPressRightButton}
>
<Image
Expand Down Expand Up @@ -698,13 +713,14 @@ export default class TextField extends BaseInput {

onKeyPress = event => {
this.lastKey = event.nativeEvent.key;
this.setCharCountColor(this.lastKey);
_.invoke(this.props, 'onKeyPress', event);
};

onChangeText = text => {
// when character count exceeds maxLength text will be empty string.
// HACK: To avoid setting state value to '' we check the source of that deletion
if (text === '' && this.lastKey && this.lastKey !== 'Backspace') {
if (text === '' && this.lastKey && this.lastKey !== Constants.backspaceKey) {
return;
}

Expand Down
5 changes: 4 additions & 1 deletion src/helpers/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ const constants = {
/* Accessibility */
get accessibility() {
return accessibility;
}
},
/* Keyboard */
backspaceKey: 'Backspace',
enterKey: 'Enter'
};

setStatusBarHeight();
Expand Down