Skip to content

Commit

Permalink
Fix highlight on multiselect slots (#539)
Browse files Browse the repository at this point in the history
Fix highlight on multiselect slots
  • Loading branch information
skjnldsv authored Aug 29, 2019
2 parents 3996f13 + efbf586 commit 832fafa
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 25 deletions.
33 changes: 13 additions & 20 deletions src/components/Multiselect/AvatarSelectOption.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ export default {
class="option__avatar" />
<div class="option__desc">
<span class="option__desc--lineone"
v-html="highlightPhrase(escapedDisplayName)" />
v-html="highlightedDisplayName" />
<span v-if="desc !== ''" class="option__desc--linetwo"
v-html="highlightPhrase(escapedDesc)" />
v-html="highlightedDesc" />
</div>
<span v-if="icon !== ''" class="icon option__icon" :class="icon" />
</span>
Expand All @@ -80,12 +80,14 @@ export default {
<script>
import escapeHtml from 'escape-html'
import Avatar from 'Components/Avatar'
import highlightText from 'Mixins/highlightText'
export default {
name: 'AvatarSelectOption',
components: {
Avatar
},
mixins: [highlightText],
props: {
/**
* Secondary optional line
Expand Down Expand Up @@ -121,25 +123,19 @@ export default {
isNoUser: {
type: Boolean,
default: false
},
search: {
type: String,
default: ''
}
},
computed: {
search() {
return this.$parent.search
},
escapedDisplayName() {
return escapeHtml(this.displayName)
highlightedDisplayName() {
return this.highlightText(escapeHtml(this.displayName), this.search)
},
escapedDesc() {
return escapeHtml(this.desc)
}
},
methods: {
highlightPhrase(text) {
if (!this.search.length) return text
return text.replace(new RegExp(this.search, 'gi'), `<strong>${this.search}</strong>`)
highlightedDesc() {
return this.highlightText(escapeHtml(this.desc), this.search)
}
}
}
Expand All @@ -165,9 +161,6 @@ export default {
min-width: 0;
&--lineone {
color: var(--color-text-light);
&--highlight {
font-weight: 600;
}
}
&--linetwo {
opacity: $opacity_normal;
Expand Down
25 changes: 22 additions & 3 deletions src/components/Multiselect/EllipsisedOption.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@

<template>
<div class="name-parts" :title="name">
<span class="name-parts__first">{{ part1 }}</span>
<span v-if="part2" class="name-parts__last">{{ part2 }}</span>
<span class="name-parts__first" v-html="highlightedPart1" />
<span v-if="part2" class="name-parts__last" v-html="highlightedPart2" />
</div>
</template>
<script>
import escapeHtml from 'escape-html'
import highlightText from 'Mixins/highlightText'
export default {
name: 'EllipsisedOption',
mixins: [highlightText],
props: {
option: {
type: [String, Object],
Expand All @@ -38,8 +44,13 @@ export default {
label: {
type: String,
default: ''
},
search: {
type: String,
default: ''
}
},
computed: {
name() {
return this.$parent.getOptionLabel(this.option)
Expand All @@ -61,8 +72,13 @@ export default {
return this.name.substr(this.name.length - split)
}
return ''
},
highlightedPart1() {
return this.highlightText(escapeHtml(this.part1), this.search)
},
highlightedPart2() {
return this.highlightText(escapeHtml(this.part2), this.search)
}
}
}
</script>
Expand All @@ -78,6 +94,9 @@ export default {
&__last {
// prevent whitespace from being trimmed
white-space: pre;
strong {
font-weight: bold;
}
}
}
</style>
4 changes: 2 additions & 2 deletions src/components/Multiselect/Multiselect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ Please see the [AvatarSelectOption](#AvatarSelectOption) component
<!-- Avatar display select slot override.
You CANNOT use this scope, we will replace it by this -->
<AvatarSelectOption v-if="userSelect && !$scopedSlots['option']"
v-bind="scope.option" />
v-bind="scope.option" :search="scope.search" />

<!-- Ellipsis in the middle if no option slot
is defined in the parent -->
<EllipsisedOption v-else-if="!$scopedSlots['option']"
:option="scope.option" :label="label" />
:option="scope.option" :search="scope.search" :label="label" />

<!-- Passing the singleLabel slot -->
<slot v-else name="option" v-bind="scope" />
Expand Down
37 changes: 37 additions & 0 deletions src/mixins/highlightText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

export default {
methods: {
/**
* Highlight a string with html <strong>
*
* @param {string} text the string to process
* @param {string} match the string to match
* @returns {string}
*/
highlightText(text, match) {
if (!match.length) return text
return text.replace(new RegExp(match, 'gi'), `<strong>${match}</strong>`)
}
}
}

0 comments on commit 832fafa

Please sign in to comment.