-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use shared highlight component in vue3 (#6051)
* wip: use shared highlight component in vue3 * fragment as nonHighlightedTagName * chore: update deps * docs: rename Vue 3 example * fix build and tests * revert yarn.lock * rename highlighter --------- Co-authored-by: Sarah Dayan <[email protected]>
- Loading branch information
1 parent
cdba2a2
commit 2974343
Showing
12 changed files
with
120 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { createHighlightComponent } from 'instantsearch-ui-components'; | ||
import { | ||
getHighlightedParts, | ||
getPropertyByPath, | ||
unescape, | ||
} from 'instantsearch.js/es/lib/utils'; | ||
|
||
import { createElement, Fragment } from '../util/pragma'; | ||
|
||
const Highlight = createHighlightComponent({ createElement, Fragment }); | ||
|
||
export default { | ||
name: 'AisHighlighter', | ||
props: { | ||
hit: { | ||
type: Object, | ||
required: true, | ||
}, | ||
attribute: { | ||
type: String, | ||
required: true, | ||
}, | ||
highlightedTagName: { | ||
type: String, | ||
default: 'mark', | ||
}, | ||
suit: { | ||
type: Function, | ||
required: true, | ||
}, | ||
highlightProperty: { | ||
type: String, | ||
required: true, | ||
}, | ||
preTag: { | ||
type: String, | ||
required: true, | ||
}, | ||
postTag: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
render() { | ||
const property = | ||
getPropertyByPath(this.hit[this.highlightProperty], this.attribute) || []; | ||
const properties = Array.isArray(property) ? property : [property]; | ||
|
||
const parts = properties.map((singleValue) => | ||
getHighlightedParts(unescape(singleValue.value || '')).map( | ||
({ value, isHighlighted }) => ({ | ||
// We have to do this because Vue gets rid of TextNodes with a single white space | ||
value: value === ' ' ? ' ' : value, | ||
isHighlighted, | ||
}) | ||
) | ||
); | ||
|
||
return createElement(Highlight, { | ||
classNames: { | ||
root: this.suit(), | ||
highlighted: this.suit('highlighted'), | ||
}, | ||
highlightedTagName: this.highlightedTagName, | ||
nonHighlightedTagName: Fragment, | ||
parts, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { h, Fragment } from 'vue'; | ||
|
||
export const createElement = (tag, props, children) => { | ||
if (!children) { | ||
return h(tag, props); | ||
} | ||
|
||
if (tag === Fragment) { | ||
return h(tag, Array.isArray(children) ? children : [children]); | ||
} | ||
|
||
// It does work to just pass a string but outputs a warning about performance issues | ||
const newChildren = | ||
typeof children === 'string' ? { default: () => children } : children; | ||
// Passing a `children` prop to a DOM element outputs a warning | ||
const newProps = | ||
typeof tag === 'string' ? props : Object.assign(props, { children }); | ||
|
||
return h(tag, newProps, newChildren); | ||
}; | ||
|
||
export { Fragment }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/vue-instantsearch/src/util/vue-compat/Highlighter/index-vue2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './Highlighter-vue2.vue'; |
1 change: 1 addition & 0 deletions
1
packages/vue-instantsearch/src/util/vue-compat/Highlighter/index-vue3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '../../../components/Highlighter'; |
1 change: 1 addition & 0 deletions
1
packages/vue-instantsearch/src/util/vue-compat/Highlighter/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './index-vue2'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters