Skip to content

Commit 247a840

Browse files
VdustRmarcosmoura
authored andcommittedJan 12, 2018
feat(MdChips): add formatter for individual chips (#1339)
* feat(MdChips): formatter Formatter before chip insertion. Effects to insertion and duplicated-checking. fix #1288 * style(MdChips): always return a value * docs(Chips): fix `md-format` API description #1339 (comment) * docs(ChipsFormat): fix component name and example message * docs(ChipsFormat): `formatName` return result directly #1339 (comment) * style(MdChips): add a space before `if` statement #1339 (comment) * docs(Chips): better message for formatter #1339 (comment) * fix: code style
1 parent d3953f7 commit 247a840

File tree

3 files changed

+91
-11
lines changed

3 files changed

+91
-11
lines changed
 

‎docs/app/pages/Components/Chips/Chips.vue

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<example src="./examples/Editable.vue" />
44
<example src="./examples/ChipCustomTemplate.vue" />
55
<example src="./examples/DuplicatedFeedback.vue" />
6+
<example src="./examples/Format.vue" />
67
<example src="./examples/Themed.vue" />
78

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

57+
<div class="page-container-section">
58+
<h2>Formatter</h2>
59+
60+
<p>Sometimes you may need to format a chip value before adding it, and for this case you can use a custom formatter function. This function will receive the chip value and must return the formatted value.</p>
61+
<code-example title="Formatted chips" :component="examples['format']" />
62+
</div>
63+
5664
<div class="page-container-section">
5765
<h2>Hue Colors</h2>
5866

@@ -156,6 +164,15 @@ export default {
156164
type: 'Boolean',
157165
description: 'Always check if there is a duplicated chip while changing the input value, or check it only on insertion',
158166
defaults: 'false'
167+
},
168+
{
169+
name: 'md-format',
170+
type: 'Function',
171+
description: [
172+
'Formatter before chip insertion. Effects to insertion and duplicated-checking.',
173+
'The Chips will pass the inputted value as a parameter of this function. This function returns the formatted result.'
174+
].join('<br/>'),
175+
defaults: 'null'
159176
}
160177
]
161178
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<template>
2+
<div>
3+
<md-chips class="md-primary" v-model="clubs" md-placeholder="Add club..." :md-format="toUppercase">
4+
<label>La Liga Clubs</label>
5+
<div class="md-helper-text">Three uppercase letters</div>
6+
</md-chips>
7+
8+
<md-chips class="md-primary" v-model="artists" md-placeholder="Add artist..." :md-format="formatName">
9+
<label>Artists</label>
10+
<div class="md-helper-text">Try inserting `Eugène Ysaÿe`. The formatter will remove diacritics.</div>
11+
</md-chips>
12+
</div>
13+
</template>
14+
15+
<script>
16+
export default {
17+
name: 'Format',
18+
data: () => ({
19+
clubs: [
20+
'FCB',
21+
'MAD'
22+
],
23+
artists: [
24+
'Claude Debussy',
25+
'Jules Massenet',
26+
'Gabriel Dupont',
27+
'Emma Bardac',
28+
'Mary Garden'
29+
]
30+
}),
31+
methods: {
32+
toUppercase (str) {
33+
str = str.replace(/\s/g, '').toUpperCase()
34+
if (str.length !== 3) return false
35+
return str
36+
},
37+
formatName (str) {
38+
let words = str.split(' ').filter(str => str !== '')
39+
// remove accents / diacritics
40+
words = words.map(str => str.normalize('NFD').replace(/[\u0300-\u036f]/g, ''))
41+
// capitalize
42+
words = words.map(str => str[0].toUpperCase() + str.slice(1))
43+
return words.join(' ')
44+
}
45+
}
46+
}
47+
</script>

‎src/components/MdChips/MdChips.vue

+27-11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
mdCheckDuplicated: {
5959
type: Boolean,
6060
default: false
61+
},
62+
mdFormat: {
63+
type: Function
6164
}
6265
},
6366
data: () => ({
@@ -73,26 +76,35 @@
7376
7477
modelRespectLimit () {
7578
return !this.mdLimit || this.value.length < this.mdLimit
79+
},
80+
81+
formattedInputValue () {
82+
if (!this.mdFormat) {
83+
return this.inputValue
84+
}
85+
return this.mdFormat(this.inputValue)
7686
}
7787
},
7888
methods: {
7989
insertChip ({ target }) {
80-
if (!this.inputValue || !this.modelRespectLimit) {
90+
let inputValue = this.formattedInputValue
91+
92+
if (!inputValue || !this.modelRespectLimit) {
8193
return
8294
}
83-
84-
if (this.value.includes(this.inputValue)) {
95+
96+
if (this.value.includes(inputValue)) {
8597
this.duplicatedChip = null
8698
// to trigger animate
8799
this.$nextTick(() => {
88-
this.duplicatedChip = this.inputValue
100+
this.duplicatedChip = inputValue
89101
})
90102
return
91103
}
92-
93-
this.value.push(this.inputValue)
104+
105+
this.value.push(inputValue)
94106
this.$emit('input', this.value)
95-
this.$emit('md-insert', this.inputValue)
107+
this.$emit('md-insert', inputValue)
96108
this.inputValue = ''
97109
},
98110
removeChip (chip) {
@@ -116,12 +128,16 @@
116128
}
117129
},
118130
checkDuplicated () {
119-
if (!this.value.includes(this.inputValue)) {
131+
if (!this.value.includes(this.formattedInputValue)) {
120132
this.duplicatedChip = null
121-
return
133+
return false
122134
}
123-
if (!this.mdCheckDuplicated) return
124-
this.duplicatedChip = this.inputValue
135+
136+
if (!this.mdCheckDuplicated) {
137+
return false
138+
}
139+
140+
this.duplicatedChip = this.formattedInputValue
125141
}
126142
},
127143
watch: {

0 commit comments

Comments
 (0)