Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 28fd54c

Browse files
vinaypaitychenjiajun
authored andcommitted
fix: Restore broken fullwidth textarea with label (#523)
Fullwidth textareas are allowed (upstream demo has an example) but wasn't working Write a console warning instead of silently ignoring invalid config options. The old behavior had potential to cause confusion. Add tests of text area and the new warnings. Fixes: #509
1 parent 877bb23 commit 28fd54c

File tree

6 files changed

+117
-9
lines changed

6 files changed

+117
-9
lines changed

components/text-field/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ data() {
142142
| dense | Boolean | false | Styles the text field as a dense text field, will be removed in an upcoming release |
143143
| focused | Boolean | false | Styles the text field as a text field in focus. |
144144
| textarea | Boolean | false | Indicates the text field is a <textarea>. |
145-
| useNativeValidation | Boolean | true | Sets whether to check native HTML validity state (true, default) or custom validity state when updating styles (false). |
145+
| useNativeValidation | Boolean | true | Sets whether to check native HTML validity state (true, default) or custom validity state when updating styles (false). |
146146
| valid | Boolean | true | Sets custom validity and updates styles accordingly. Note that native validation will still be honored subsequently unless useNativeValidation is also false. |
147147

148148
### Slots
@@ -210,4 +210,4 @@ Character counter is used if there is a character limit. It displays the ratio o
210210

211211
### Reference
212212

213-
- https://github.com/material-components/material-components-web/tree/master/packages/mdc-text field
213+
- https://github.com/material-components/material-components-web/tree/master/packages/mdc-textfield

components/text-field/TextField.vue

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</div>
3838
<div class="mdc-notched-outline__trailing" />
3939
</div>
40-
<slot v-if="$slots.default && !fullWidth && !textarea && !outlined" />
40+
<slot v-if="$slots.default && !textarea && !outlined" />
4141
<slot name="trailingIcon" />
4242
<slot
4343
v-if="!outlined"
@@ -119,14 +119,14 @@ export default {
119119
computed: {
120120
classes () {
121121
return {
122-
'mdc-text-field--fullwidth': this.fullWidth && this.noLabel && !this.outlined,
122+
'mdc-text-field--fullwidth': this.fullWidth,
123123
'mdc-text-field--with-leading-icon': this.hasLeadingIcon,
124124
'mdc-text-field--with-trailing-icon': this.hasTrailingIcon,
125-
'mdc-text-field--outlined': this.outlined && !this.fullWidth,
125+
'mdc-text-field--outlined': this.outlined,
126126
'mdc-text-field--dense': this.dense,
127127
'mdc-text-field--focused': this.focused, // won't change the actual activeElement
128128
'mdc-text-field--textarea': this.textarea,
129-
'mdc-text-field--no-label': this.noLabel && !this.fullWidth
129+
'mdc-text-field--no-label': this.noLabel
130130
}
131131
}
132132
},
@@ -165,6 +165,7 @@ export default {
165165
this.noLabel = this.$el.querySelector('.mdc-floating-label') == null
166166
this.hasLeadingIcon = this.$slots.leadingIcon != null
167167
this.hasTrailingIcon = this.$slots.trailingIcon != null
168+
168169
// to make our icons compatible with version 0.x.y
169170
if (this.hasLeadingIcon) {
170171
this.$slots.leadingIcon.forEach(n => {
@@ -180,6 +181,8 @@ export default {
180181
}
181182
})
182183
}
184+
185+
this.checkConfig()
183186
},
184187
reInstantiate () {
185188
this.mdcTextField.destroy()
@@ -215,6 +218,21 @@ export default {
215218
}
216219
})
217220
},
221+
checkConfig () {
222+
if (this.fullWidth && !this.noLabel && !this.textarea) {
223+
console.warn(
224+
'Do not use floating label with a full width text input. ' +
225+
'See https://github.com/material-components/material-components-web/tree/master/packages/mdc-textfield#full-width'
226+
)
227+
}
228+
229+
if (this.fullWidth && this.outlined && !this.textarea) {
230+
console.warn(
231+
'Do not use outlined style on full width text input. ' +
232+
'See: https://github.com/material-components/material-components-web/tree/master/packages/mdc-textfield#full-width'
233+
)
234+
}
235+
},
218236
getLabel () {
219237
return this.mdcTextField.label_
220238
},

components/text-field/TextFieldCharacterCounter.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<template>
2-
<div class="mdc-text-field-character-counter" @_init="onParentInit">
2+
<div
3+
class="mdc-text-field-character-counter"
4+
@_init="onParentInit"
5+
>
36
{{ currentLength }} / {{ maxLength }}
47
</div>
58
</template>

components/text-field/__test__/TextField.spec.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'mutationobserver-shim'
22
import { mount } from '@vue/test-utils'
33
import TextField from '../TextField.vue'
44
import TextFieldIcon from '../TextFieldIcon.vue'
5+
import FloatingLabel from '../../floating-label/FloatingLabel.vue'
56

67
describe('Text Field', () => {
78
it('should mount', () => {
@@ -68,6 +69,37 @@ describe('Text Field', () => {
6869
expect(wrapper.classes()).toContain('mdc-text-field--fullwidth')
6970
})
7071

72+
it('should warn of fullWidth text input with floating label', () => {
73+
const warnfn = console.warn
74+
console.warn = jest.fn()
75+
mount(TextField, {
76+
propsData: {
77+
fullWidth: true
78+
},
79+
slots: {
80+
default: '<m-floating-label>My Label</m-floating-label>'
81+
},
82+
stubs: {
83+
'm-floating-label': FloatingLabel
84+
}
85+
})
86+
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('floating label'))
87+
console.warn = warnfn
88+
})
89+
90+
it('should warn of fullWidth text input with outlined style', () => {
91+
const warnfn = console.warn
92+
console.warn = jest.fn()
93+
mount(TextField, {
94+
propsData: {
95+
fullWidth: true,
96+
outlined: true
97+
}
98+
})
99+
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('outlined style'))
100+
console.warn = warnfn
101+
})
102+
71103
it('should render and emit', () => {
72104
const wrapper = mount(TextField, {
73105
propsData: {
@@ -100,4 +132,35 @@ describe('Text Field', () => {
100132
expect(wrapper).toMatchSnapshot()
101133
expect(wrapper.classes()).toContain('mdc-text-field--with-trailing-icon')
102134
})
135+
136+
it('should render textarea', () => {
137+
const wrapper = mount(TextField, {
138+
propsData: {
139+
textarea: true
140+
}
141+
})
142+
143+
expect(wrapper).toMatchSnapshot()
144+
expect(wrapper.classes()).toContain('mdc-text-field--textarea')
145+
})
146+
147+
it('should render full-width textarea with outlined label', () => {
148+
const wrapper = mount(TextField, {
149+
propsData: {
150+
textarea: true,
151+
fullWidth: true
152+
},
153+
slots: {
154+
default: '<m-floating-label>My Label</m-floating-label>'
155+
},
156+
stubs: {
157+
'm-floating-label': FloatingLabel
158+
}
159+
})
160+
161+
expect(wrapper).toMatchSnapshot()
162+
expect(wrapper.classes()).toContain('mdc-text-field--fullwidth')
163+
expect(wrapper.classes()).toContain('mdc-text-field--textarea')
164+
expect(wrapper.find('.mdc-notched-outline').exists()).toBe(true)
165+
})
103166
})

components/text-field/__test__/__snapshots__/TextField.spec.js.snap

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ exports[`Text Field should render as focused 1`] = `
2828
`;
2929
3030
exports[`Text Field should render as fullWidth 1`] = `
31-
<div class="mdc-text-field mdc-text-field--fullwidth">
31+
<div class="mdc-text-field mdc-text-field--fullwidth mdc-text-field--no-label">
3232
<!----> <input class="mdc-text-field__input">
3333
<!---->
3434
<!---->
@@ -50,6 +50,30 @@ exports[`Text Field should render as outlined 1`] = `
5050
</div>
5151
`;
5252
53+
exports[`Text Field should render full-width textarea with outlined label 1`] = `
54+
<div class="mdc-text-field mdc-text-field--fullwidth mdc-text-field--textarea">
55+
<!----> <textarea class="mdc-text-field__input"></textarea>
56+
<div class="mdc-notched-outline mdc-notched-outline--upgraded">
57+
<div class="mdc-notched-outline__leading"></div>
58+
<div class="mdc-notched-outline__notch"><label class="mdc-floating-label" style="transition-duration: 0s;">My Label</label></div>
59+
<div class="mdc-notched-outline__trailing"></div>
60+
</div>
61+
<!---->
62+
</div>
63+
`;
64+
65+
exports[`Text Field should render textarea 1`] = `
66+
<div class="mdc-text-field mdc-text-field--textarea mdc-text-field--no-label">
67+
<!----> <textarea class="mdc-text-field__input"></textarea>
68+
<div class="mdc-notched-outline mdc-notched-outline--no-label">
69+
<div class="mdc-notched-outline__leading"></div>
70+
<!---->
71+
<div class="mdc-notched-outline__trailing"></div>
72+
</div>
73+
<!---->
74+
</div>
75+
`;
76+
5377
exports[`Text Field should render with leading icon 1`] = `
5478
<div class="mdc-text-field mdc-text-field--with-leading-icon mdc-text-field--no-label"><i class="mdc-text-field__icon" tabindex="0" role="button"></i>
5579
<!----> <input class="mdc-text-field__input">

docs/.vuepress/components/TextfieldDemo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
>
1818
<m-text-field-character-counter v-if="checkboxProps[15].value && radioProps[3].value" slot="characterCounter"/>
1919
<m-text-field-icon icon="favorite" slot="leadingIcon" v-if="checkboxProps[8].value" :clickable="checkboxProps[10].value"></m-text-field-icon>
20-
<m-floating-label v-if="!checkboxProps[11].value || radioProps[1].value"
20+
<m-floating-label v-if="!checkboxProps[11].value"
2121
for="text-field1">Label
2222
</m-floating-label>
2323
<m-text-field-icon icon="favorite" slot="trailingIcon" v-if="checkboxProps[9].value" :clickable="checkboxProps[10].value"></m-text-field-icon>

0 commit comments

Comments
 (0)