-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(MdChips): formatter #1339
feat(MdChips): formatter #1339
Changes from 1 commit
ebf14c6
08286e1
ca3ac20
e5b57de
e955e99
79f4f41
8a52166
f62a43e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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> | ||
|
||
|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can explicitly say that this function will receive this parameter. There is an example on md-table component There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix ca3ac20 |
||
].join('<br/>'), | ||
defaults: 'null' | ||
} | ||
] | ||
}, | ||
|
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this message correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
</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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just return the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix e955e99 |
||
} | ||
} | ||
} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,9 @@ | |
mdCheckDuplicated: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
mdFormat: { | ||
type: Function | ||
} | ||
}, | ||
data: () => ({ | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a space before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix 79f4f41 |
||
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) { | ||
|
@@ -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: { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better message could be:
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix 8a52166