Skip to content

Commit

Permalink
feat(MdChips): formatter
Browse files Browse the repository at this point in the history
Formatter before chip insertion. Effects to insertion and duplicated-checking.

fix #1288
  • Loading branch information
VdustR committed Dec 25, 2017
1 parent 8ae249c commit ebf14c6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
17 changes: 17 additions & 0 deletions docs/app/pages/Components/Chips/Chips.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<example src="./examples/Editable.vue" />
<example src="./examples/ChipCustomTemplate.vue" />
<example src="./examples/DuplicatedFeedback.vue" />
<example src="./examples/Format.vue" />
<example src="./examples/Themed.vue" />

<template>
Expand Down Expand Up @@ -53,6 +54,13 @@
<code-example title="Duplicated Feedback" :component="examples['duplicated-feedback']" />
</div>

<div class="page-container-section">
<h2>Formatter</h2>

<p>There could be a rule to be followed by the inserted chips:</p>
<code-example title="Formatted chips" :component="examples['format']" />
</div>

<div class="page-container-section">
<h2>Hue Colors</h2>

Expand Down Expand Up @@ -156,6 +164,15 @@ export default {
type: 'Boolean',
description: 'Always check if there is a duplicated chip while changing the input value, or check it only on insertion',
defaults: 'false'
},
{
name: 'md-format',
type: 'Function',
description: [
'Formatter before chip insertion. Effects to insertion and duplicated-checking.',
'@param {<code>String</code>} input value'
].join('<br/>'),
defaults: 'null'
}
]
},
Expand Down
47 changes: 47 additions & 0 deletions docs/app/pages/Components/Chips/examples/Format.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<template>
<div>
<md-chips class="md-primary" v-model="clubs" md-placeholder="Add club..." :md-format="toUppercase">
<label>La Liga Clubs</label>
<div class="md-helper-text">Three uppercase letters</div>
</md-chips>
<md-chips class="md-primary" v-model="artists" md-placeholder="Add artist..." :md-format="formatName">
<label>Artists</label>
<div class="md-helper-text">Try insert `Eugène Ysaÿe` twice. The formatter will remove diacritics.</div>
</md-chips>
</div>
</template>

<script>
export default {
name: 'DuplicatedFeedback',
data: () => ({
clubs: [
'FCB',
'MAD'
],
artists: [
'Claude Debussy',
'Jules Massenet',
'Gabriel Dupont',
'Emma Bardac',
'Mary Garden'
]
}),
methods: {
toUppercase (str) {
str = str.replace(/\s/g, '').toUpperCase()
if (str.length !== 3) return false
return str
},
formatName (str) {
let words = str.split(' ').filter(str => str !== '')
// remove accents / diacritics
words = words.map(str => str.normalize('NFD').replace(/[\u0300-\u036f]/g, ''))
// capitalize
words = words.map(str => str[0].toUpperCase() + str.slice(1))
let result = words.join(' ')
return result
}
}
}
</script>
29 changes: 20 additions & 9 deletions src/components/MdChips/MdChips.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
mdCheckDuplicated: {
type: Boolean,
default: false
},
mdFormat: {
type: Function
}
},
data: () => ({
Expand All @@ -73,26 +76,34 @@
modelRespectLimit () {
return !this.mdLimit || this.value.length < this.mdLimit
},
formattedInputValue () {
if (!this.mdFormat) {
return this.inputValue
}
return this.mdFormat(this.inputValue)
}
},
methods: {
insertChip ({ target }) {
if (!this.inputValue || !this.modelRespectLimit) {
let inputValue = this.formattedInputValue
if (!inputValue || !this.modelRespectLimit) {
return
}
if (this.value.includes(this.inputValue)) {
if (this.value.includes(inputValue)) {
this.duplicatedChip = null
// to trigger animate
this.$nextTick(() => {
this.duplicatedChip = this.inputValue
this.duplicatedChip = inputValue
})
return
}
this.value.push(this.inputValue)
this.value.push(inputValue)
this.$emit('input', this.value)
this.$emit('md-insert', this.inputValue)
this.$emit('md-insert', inputValue)
this.inputValue = ''
},
removeChip (chip) {
Expand All @@ -116,12 +127,12 @@
}
},
checkDuplicated () {
if (!this.value.includes(this.inputValue)) {
if (!this.value.includes(this.formattedInputValue)) {
this.duplicatedChip = null
return
}
if (!this.mdCheckDuplicated) return
this.duplicatedChip = this.inputValue
this.duplicatedChip = this.formattedInputValue
}
},
watch: {
Expand Down

0 comments on commit ebf14c6

Please sign in to comment.