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

Commit

Permalink
feat(text-field): Support for types- color, date, datetime-local, etc (
Browse files Browse the repository at this point in the history
…#2854)

Updates the floating label logic to always float for certain types.
  • Loading branch information
namannehra authored and williamernest committed Jul 31, 2018
1 parent cd36b2e commit 0d02f1f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
4 changes: 0 additions & 4 deletions packages/mdc-textfield/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ npm install @material/textfield
</div>
```

> NOTE: Text field supports `text`, `number`, and `password` input types (e.g., `<input type="password" class="mdc-text-field__input">`).
>
> Other input types (such as `date`) are not currently supported.
> NOTE: For more details, see [MDC Line Ripple](../mdc-line-ripple/README.md)
> and [MDC Floating Label](../mdc-floating-label/README.md).
Expand Down
7 changes: 6 additions & 1 deletion packages/mdc-textfield/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ const VALIDATION_ATTR_WHITELIST = [
'pattern', 'min', 'max', 'required', 'step', 'minlength', 'maxlength',
];

export {cssClasses, strings, numbers, VALIDATION_ATTR_WHITELIST};
// Label should always float for these types as they show some UI even if value is empty.
const ALWAYS_FLOAT_TYPES = [
'color', 'date', 'datetime-local', 'month', 'range', 'time', 'week',
];

export {cssClasses, strings, numbers, VALIDATION_ATTR_WHITELIST, ALWAYS_FLOAT_TYPES};
40 changes: 22 additions & 18 deletions packages/mdc-textfield/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import MDCTextFieldHelperTextFoundation from './helper-text/foundation';
import MDCTextFieldIconFoundation from './icon/foundation';
/* eslint-enable no-unused-vars */
import {MDCTextFieldAdapter, NativeInputType, FoundationMapType} from './adapter';
import {cssClasses, strings, numbers, VALIDATION_ATTR_WHITELIST} from './constants';

import {cssClasses, strings, numbers, VALIDATION_ATTR_WHITELIST, ALWAYS_FLOAT_TYPES} from './constants';

/**
* @extends {MDCFoundation<!MDCTextFieldAdapter>}
Expand All @@ -49,9 +48,18 @@ class MDCTextFieldFoundation extends MDCFoundation {
return !this.isValid() && !this.isFocused_;
}

/**
* @return {boolean}
* @private
*/
get shouldAlwaysFloat_() {
const type = this.getNativeInput_().type;
return ALWAYS_FLOAT_TYPES.indexOf(type) >= 0;
}

/** @return {boolean} */
get shouldFloat() {
return this.isFocused_ || !!this.getValue() || this.isBadInput_();
return this.shouldAlwaysFloat_ || this.isFocused_ || !!this.getValue() || this.isBadInput_();
}

/**
Expand Down Expand Up @@ -129,14 +137,12 @@ class MDCTextFieldFoundation extends MDCFoundation {

init() {
this.adapter_.addClass(MDCTextFieldFoundation.cssClasses.UPGRADED);
// Ensure label does not collide with any pre-filled value.
if (this.adapter_.hasLabel() && (this.getValue() || this.isBadInput_())) {
this.adapter_.floatLabel(this.shouldFloat);
this.notchOutline(this.shouldFloat);
}

if (this.adapter_.isFocused()) {
this.inputFocusHandler_();
} else if (this.adapter_.hasLabel() && this.shouldFloat) {
this.notchOutline(true);
this.adapter_.floatLabel(true);
}

this.adapter_.registerInputInteractionHandler('focus', this.inputFocusHandler_);
Expand Down Expand Up @@ -194,7 +200,7 @@ class MDCTextFieldFoundation extends MDCFoundation {
* @param {boolean} openNotch
*/
notchOutline(openNotch) {
if (!this.adapter_.hasOutline() || !this.adapter_.hasLabel()) {
if (!this.adapter_.hasOutline()) {
return;
}

Expand All @@ -216,10 +222,10 @@ class MDCTextFieldFoundation extends MDCFoundation {
this.isFocused_ = true;
this.styleFocused_(this.isFocused_);
this.adapter_.activateLineRipple();
this.notchOutline(this.shouldFloat);
if (this.adapter_.hasLabel()) {
this.adapter_.shakeLabel(this.shouldShake);
this.notchOutline(this.shouldFloat);
this.adapter_.floatLabel(this.shouldFloat);
this.adapter_.shakeLabel(this.shouldShake);
}
if (this.helperText_) {
this.helperText_.showToScreenReader();
Expand Down Expand Up @@ -254,17 +260,15 @@ class MDCTextFieldFoundation extends MDCFoundation {
deactivateFocus() {
this.isFocused_ = false;
this.adapter_.deactivateLineRipple();
const input = this.getNativeInput_();
const shouldRemoveLabelFloat = !input.value && !this.isBadInput_();
const isValid = this.isValid();
this.styleValidity_(isValid);
this.styleFocused_(this.isFocused_);
if (this.adapter_.hasLabel()) {
this.adapter_.shakeLabel(this.shouldShake);
this.adapter_.floatLabel(this.shouldFloat);
this.notchOutline(this.shouldFloat);
this.adapter_.floatLabel(this.shouldFloat);
this.adapter_.shakeLabel(this.shouldShake);
}
if (shouldRemoveLabelFloat) {
if (!this.shouldFloat) {
this.receivedUserInput_ = false;
}
}
Expand All @@ -284,9 +288,9 @@ class MDCTextFieldFoundation extends MDCFoundation {
const isValid = this.isValid();
this.styleValidity_(isValid);
if (this.adapter_.hasLabel()) {
this.adapter_.shakeLabel(this.shouldShake);
this.adapter_.floatLabel(this.shouldFloat);
this.notchOutline(this.shouldFloat);
this.adapter_.floatLabel(this.shouldFloat);
this.adapter_.shakeLabel(this.shouldShake);
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/unit/mdc-textfield/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,3 +792,19 @@ test('label floats on invalid input even if value is empty', () => {
const {mockAdapter} = setupValueTest('', false, true, true);
td.verify(mockAdapter.floatLabel(true));
});

test('label floats when type is date even if value is empty', () => {
const {foundation, mockAdapter} = setupTest();
td.when(mockAdapter.hasLabel()).thenReturn(true);
const nativeInput = {
type: 'date',
value: '',
validity: {
valid: true,
badInput: false,
},
};
td.when(mockAdapter.getNativeInput()).thenReturn(nativeInput);
foundation.init();
td.verify(mockAdapter.floatLabel(true));
});

0 comments on commit 0d02f1f

Please sign in to comment.