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

feat(text-field): add character counter #226

Merged
merged 1 commit into from
May 7, 2019
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
56 changes: 56 additions & 0 deletions components/text-field/TextFieldCharacterCounter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<template>
<div class="mdc-text-field-character-counter">
{{ currentLength }} / {{ maxLength }}
</div>
</template>

<script>
import { MDCTextFieldCharacterCounter } from '@material/textfield/character-counter'

import { baseComponentMixin, themeClassMixin } from '../base'

export default {
name: 'TextFieldCharacterCounter',
mixins: [baseComponentMixin, themeClassMixin],
props: {
currentLength: {
type: Number,
default: 0
},
maxLength: {
type: Number,
default: 0
}
},
data () {
return {
mdcTextFieldCharacterCounter: undefined,
cl: this.currentLength,
ml: this.maxLength
}
},
watch: {
currentLength () {
this.cl = this.currentLength
this.mdcTextFieldCharacterCounter.foundation.setCounterValue(this.currentLength, this.maxLength)
},
maxLength () {
this.ml = this.maxLength
this.mdcTextFieldCharacterCounter.foundation.setCounterValue(this.currentLength, this.maxLength)
}
},
mounted () {
let input = this.$el.parentElement.previousElementSibling.querySelector('.mdc-text-field__input')
if (input) {
this.ml = input.getAttribute('maxlength')
this.cl = input.value.length
}
this.mdcTextFieldCharacterCounter = MDCTextFieldCharacterCounter.attachTo(this.$el)
this.mdcTextFieldCharacterCounter.foundation.setCounterValue(this.cl, this.ml)
}
}
</script>

<style scoped>

</style>
1 change: 1 addition & 0 deletions components/text-field/TextFieldHelperText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
>
<slot />
</div>
<slot name="characterCounter" />
</div>
</template>

Expand Down
2 changes: 2 additions & 0 deletions components/text-field/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import TextField from './TextField.vue'
import TextFieldHelperText from './TextFieldHelperText.vue'
import TextFieldCharacterCounter from './TextFieldCharacterCounter'
import './styles.scss'

import { initPlugin } from '../'
Expand All @@ -8,6 +9,7 @@ const plugin = {
install (vm) {
vm.component('m-text-field', TextField)
vm.component('m-text-field-helper-text', TextFieldHelperText)
vm.component('m-text-field-character-counter', TextFieldCharacterCounter)
}
}
export default plugin
Expand Down
57 changes: 57 additions & 0 deletions docs/.vuepress/components/TextfieldDemo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,63 @@
Helper Text
</m-text-field-helper-text>
</div>

<m-typography>Filled with Helper Text and Character Counter</m-typography>
<div>
<m-text-field
:disabled="checkboxProps[0].value"
:focused="checkboxProps[2].value"
:required="checkboxProps[5].value"
:upgraded="checkboxProps[1].value"
:use-native-validation="checkboxProps[4].value"
:valid="checkboxProps[3].value"
aria-controls="helper-text3"
aria-describedby="helper-text3"
id="text-field10"
maxlength="20"
>
<m-floating-label
for="text-field10">Label
</m-floating-label>
<m-line-ripple slot="bottomLine"/>
</m-text-field>
<m-text-field-helper-text :persistent="checkboxProps[6].value" :validation-msg="checkboxProps[7].value"
id="helper-text3">
Helper Text
<template v-slot:characterCounter>
<m-text-field-character-counter/>
</template>
</m-text-field-helper-text>
</div>

<m-typography>Outlined with Helper Text and Character Counter</m-typography>
<div>
<m-text-field
:disabled="checkboxProps[0].value"
:focused="checkboxProps[2].value"
:required="checkboxProps[5].value"
:upgraded="checkboxProps[1].value"
:use-native-validation="checkboxProps[4].value"
:valid="checkboxProps[3].value"
aria-controls="helper-text4"
aria-describedby="helper-text4"
id="text-field11"
maxlength="20"
outlined
>
<m-floating-label
for="text-field11">Label
</m-floating-label>
<m-line-ripple slot="bottomLine"/>
</m-text-field>
<m-text-field-helper-text :persistent="checkboxProps[6].value" :validation-msg="checkboxProps[7].value"
id="helper-text4">
Helper Text
<template v-slot:characterCounter>
<m-text-field-character-counter/>
</template>
</m-text-field-helper-text>
</div>
</ComponentSection>
<ComponentProps
:checkboxProps="checkboxProps"
Expand Down