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

Create a native 'autocomplete' type for afQuickfield in AutoForm that is Bootstrap themed. #1700

Open
4Z4T4R opened this issue Dec 20, 2020 · 1 comment · May be fixed by #1701
Open

Create a native 'autocomplete' type for afQuickfield in AutoForm that is Bootstrap themed. #1700

4Z4T4R opened this issue Dec 20, 2020 · 1 comment · May be fixed by #1701
Milestone

Comments

@4Z4T4R
Copy link

4Z4T4R commented Dec 20, 2020

Problem

In 2020 and beyond, autocomplete input fields are a common pattern for data-intensive form completion, but there is no native solution in the AutoForm boilerplate. Many of today's applications require "rich" input experience that can quickly provide suggestions filtered from already existing entries in the database, or an enumerable list of options. Using a "select" for everything is very cumbersome, especially as the list of possible solutions to the text matching enumerable entries exceeds the available area of the UI viewport, such as on Mobile devices. The existing autocomplete and typeahead "add-ons" are outdated and don't conform to the v7 API design, pull in external dependencies, and don't support Bootstrap 4 and beyond.

Solution

Generally, rather than rely exclusively on select dropdown elements, a proper experience leverages a "text" input field that offers a list of autocomplete solutions matching the input text (the needle) with a datum (the haystack) using one or more user-defined patterns (e.g. start, global, case sensitive, case insensitive, all via RegEx).

Specifically, we need to create the /inputTypes/autocomplete/ directory that contains autocomplete.js and autocomplete.html. The basic code for these is included below.

// autocomplete.js
AutoForm.addInputType('autocomplete', {
  template: 'afAutocomplete',
  valueOut: function () {
    return this.val()
  },
  valueConverters: {
    stringArray: AutoForm.valueConverters.stringToStringArray,
    number: AutoForm.valueConverters.stringToNumber,
    numberArray: AutoForm.valueConverters.stringToNumberArray,
    boolean: AutoForm.valueConverters.stringToBoolean,
    booleanArray: AutoForm.valueConverters.stringToBooleanArray,
    date: AutoForm.valueConverters.stringToDate,
    dateArray: AutoForm.valueConverters.stringToDateArray
  },
  contextAdjust: function (context) {
    context.atts.autocomplete = 'off'
    return context
  }
})

// autocomplete.html
<template name="afAutocomplete">
  <input type="text" value="{{this.value}}" {{this.atts}} />
</template>

Moreover, the autocomplete.js should include helpers and UI code that populates Mtext inside the input field plus a floating list of elements below or above the text input field, which the user can highlight using arrow keys and select using Return, touch, or mouse click.

Options for the autocomplete can be set two ways:

  1. Via static autoform.options inside a simpl-schema definition. Much like the options used in a select type.
  2. Via ReactiveVar or function helper set on data-autocomplete-options in the user's afQuickfield definition. This offers the power to tie a capped collection, or optimized query from MongoDB into the UX, allowing for a dynamic autocomplete experience.

CSS for the autocomplete field should come from Bootstrap 4 (or later) and implement a structured set of class selector definitions for the elements that make up the autocomplete box, which could be structured as follows:

Lastly, we need to allow for theme implementations by adding a Bootstrap 4 theme in meteor-autoform-themes in /bootstrap4/templates/bootstrap4/inputTypes/ so that the form-control class and styles get applied.

<template name="afAutocomplete_bootstrap4">
  <input type="hidden" value="{{this.label}}" {{ this.visibleAtts }} />
  <input type="text" value="{{this.value}}" {{ this.visibleAtts }} />
  <div class="autocomplete dropdown">
    <div class="ac-option">First</div>
    <div class="ac-option ac-hilite">Second</div>
  </div>
</template>

With as common and data-enriched applications are becoming, this feature could also revive a little more popularity for the Meteor framework and AutoForm by adding an elegant solution to the quiver of modern application and UX development tools.

Alternatives Considered

All the suggested add-on packages were taken into consideration and evaluated prior to deciding to pursue this. None are up-to-date and all rely on external libraries for functionality, such as the old typeahead solution from Bootstrap 3 (now deprecated). The use of a select was also taken into consideration, but as mentioned, proved inefficient. In the use case that we are implementing in our venture, we require a live list of currency "asset pairs" that varies between exchanges, so we demand a reactive list of autocomplete options to speed the process of populating Orders.

Additional context

This issue is the product of a conversation started on Stack Overflow relating to issues associated with wanting to implement a custom autocomplete for AutoForm.

I'm going to move forward with this in the next few days for our project. I'd be humbled to contribute this back to the community if others find this useful and appropriate.

@jankapunkt jankapunkt linked a pull request Dec 30, 2020 that will close this issue
@jankapunkt
Copy link
Member

Related PR: #1701

@jankapunkt jankapunkt added this to the v7 milestone Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants