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

Feature/document tagify component #10

Open
wants to merge 2 commits into
base: documentation-revision
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default {
collapsible: true,
items: [
{text: 'Filter Components', link: '/examples/filter-components'},
{text: 'Tagify Component Snippet', link: '/examples/tagify-component-snippet.md'},
]
},
{
Expand Down
Binary file added docs/assets/Tagify_Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 167 additions & 0 deletions docs/examples/tagify-component-snippet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# 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 conjunction with the Harness-Vue state flow.

![Tagify Component](https://repository-images.githubusercontent.com/70848287/a1ace900-bb67-11e9-8ae3-c15d59ad19e5 "Tagify Component")

[[toc]]





## Example Use Case

This component is particularly useful when a user must choose multiple options from a list that is too large 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 and update the current value of the filter.

This component utilizes the `onMounted` lifecycle hook to initialize the Tagify input with the current state of the Harness-Vue filter. The component initializes a watcher on the filter options, updating the Tagify input's suggested options (whitelist). 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";
Copy link
Author

@puckybreg puckybreg Jul 26, 2024

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.

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>
```
Loading