Skip to content
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

Merged
merged 8 commits into from
Jan 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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>
Copy link
Member

@marcosmoura marcosmoura Dec 26, 2017

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix 8a52166

<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'
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix ca3ac20

].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>
Copy link
Member

@marcosmoura marcosmoura Dec 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this message correct? Try insert 'Eugène Ysaÿe' twice. I think a good one could be:
Try inserting a chip with 'Eugène Ysaÿe'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I expect developers see this:

image

</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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just return the words.join(' ') instead of creating a new var

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix e955e99

}
}
}
</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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space before if statement

Copy link
Member Author

Choose a reason for hiding this comment

The 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) {
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