-
Notifications
You must be signed in to change notification settings - Fork 1
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
Feature/document tagify component #10
Open
puckybreg
wants to merge
2
commits into
documentation-revision
Choose a base branch
from
feature/document-tagify-component
base: documentation-revision
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,165 @@ | ||
# Tagify Input Component | ||
[Tagify](https://github.com/yairEO/tagify) offers a handy component for selecting multiple items via search from a large list of options. In this component we connect our Harness-Vue filter data to this component to allow for the Tagify tagging component to be used in conjuction with the Harness-Vue state flow. | ||
|
||
[[toc]] | ||
|
||
|
||
|
||
|
||
|
||
## Example Use Case | ||
|
||
This component is particularly useful when a user must choose multiple options from a list that is too largy to scroll through. Consider a scenario, where a user must select from a large list of individual product options. The Tagify component allows the user to quickly search and select multiple products of interest for data visualization. | ||
|
||
![Example Tagify Screenshot](../assets/Tagify_Screenshot.png "Tagify Screenshot") | ||
|
||
## Connecting to Harness-Vue State | ||
|
||
|
||
As discussed in the [Filter Components]() section, any custom input controlling a Harness-Vue filter must be able to both retrieve the current value of the filter and update it. | ||
|
||
This component utilizes the `onMounted` lifecycle hook to initialize the Tagify input with the current state of the Harness-Vue filter. It also sets up a watcher on the filter options, updating the Tagify input's whitelist (the list of suggested options displayed to the user) whenever these options change. This setup allows the suggestion options to be dynamically updated based on changes to another filter. | ||
|
||
Additionally, the component establishes event listeners for the add and remove events of the Tagify input. Any addition or removal of a tag triggers an update to the Harness-Vue filter and a subsequent data reload. | ||
|
||
## Component Code | ||
```vue | ||
/** | ||
* @typedef {Object} Props | ||
* @property {Object} filter - The filter object used for data filtering. | ||
* @property {String} inputDescription - The description of the input field. | ||
* @property {String} label - The label for the input field. | ||
*/ | ||
<script setup> | ||
import { useHarnessComposable } from "@rtidatascience/harness-vue"; | ||
import { onMounted, watch, ref } from "vue"; | ||
import { arraysAreEqual } from "@/utils/util"; | ||
import Tagify from "@yaireo/tagify"; | ||
|
||
/** | ||
* The harness composable instance. | ||
*/ | ||
const harness = useHarnessComposable(); | ||
|
||
/** | ||
* The component props. | ||
* @type {Props} | ||
*/ | ||
const props = defineProps({ | ||
filter: { | ||
required: true, | ||
type: Object, | ||
validator: (f) => f.key, | ||
}, | ||
inputDescription: { | ||
required: true, | ||
type: String, | ||
}, | ||
label: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
|
||
/** | ||
* The reference to the tagify input element. | ||
*/ | ||
const tagifyInput = ref(null); | ||
|
||
/** | ||
* Lifecycle hook that runs after the component is mounted. | ||
*/ | ||
onMounted(async () => { | ||
// Initialize tagify | ||
var whitelist = []; | ||
|
||
var tagify = new Tagify(tagifyInput.value, { | ||
whitelist: whitelist, | ||
enforceWhitelist: true, | ||
dropdown: { | ||
enabled: 0, | ||
maxItems: Infinity, | ||
}, | ||
}); | ||
|
||
/** | ||
* Watcher for changes in the filter options. | ||
*/ | ||
watch( | ||
() => harness.getOptionsForFilter(props.filter.key), | ||
(newVal, oldVal) => { | ||
if ( | ||
!arraysAreEqual( | ||
newVal.map((item) => item.key), | ||
oldVal.map((item) => item.key), | ||
) | ||
) { | ||
// Remove current tags | ||
tagify.removeAllTags(); | ||
// Convert the array of objects to an array of strings | ||
var stringTags = newVal | ||
.map((item) => item.label) | ||
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); | ||
whitelist = stringTags; | ||
tagify.settings.whitelist = stringTags; | ||
} | ||
}, | ||
); | ||
|
||
// Chainable event listeners | ||
tagify.on("add", onAddTag).on("remove", onRemoveTag); | ||
|
||
/** | ||
* Callback function for when a tag is added. | ||
* @param {Event} _e - The event object. | ||
*/ | ||
function onAddTag(_e) { | ||
// Update Harness filter | ||
updateFilter(); | ||
} | ||
|
||
/** | ||
* Callback function for when a tag is removed. | ||
* @param {Event} _e - The event object. | ||
*/ | ||
function onRemoveTag(_e) { | ||
updateFilter(); | ||
} | ||
|
||
/** | ||
* Updates the Harness filter based on the selected tags. | ||
*/ | ||
function updateFilter() { | ||
// Update Harness filter | ||
const options = harness.getOptionsForFilter(props.filter.key); | ||
const keys = tagify.value | ||
.map((value) => { | ||
const option = options.find((item) => item.label === value.value); | ||
return option ? option.key : null; | ||
}) | ||
.filter((key) => key !== null); | ||
harness.setFilter(props.filter.key, keys); | ||
harness.loadData(); | ||
} | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div class="col" id="tagify-col"> | ||
<div class="form-group"> | ||
<label for="tagifyInput" class="form-label">{{ label }}</label> | ||
<input | ||
class="form-control" | ||
id="tagifyInput" | ||
ref="tagifyInput" | ||
aria-describedby="tagifyInputHelpBlock" | ||
/> | ||
<small | ||
id="tagifyInputHelpBlock" | ||
class="form-text text-muted" | ||
v-html="props.inputDescription" | ||
></small> | ||
</div> | ||
</div> | ||
</template> | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This utility function
arraysAreEqual
is used to prevent the watcher from triggering unnecessary updates to the tagify component. It does this by ensuring that updates only occur when there are actual changes to the harness filter options.I did not document the exact code for this utility function within the component, but feel that it is a self-explanatory function that doesn't need to be included in this code snippet for the code to be re-usable.
Let me know if you think I should move it into the component itself for reproducibility.