Skip to content
Closed
Changes from 1 commit
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
31 changes: 21 additions & 10 deletions inputTypes/autocomplete/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ AutoForm.addInputType('autocomplete', {
},
contextAdjust: function (context) {
context.atts.autocomplete = 'off'
const itemAtts = { ...context.atts }
const { ...itemAtts } = context.atts
// remove non-essential atts from visible input
const visibleAtts = Object.assign({}, { ...context.atts })
const keys = ['data-schema-key', 'id', 'name']
keys.forEach(key => {
const visibleAtts = Object.assign({}, context.atts)

;['data-schema-key', 'id', 'name'].forEach(key => {
delete visibleAtts[key]
})

// add form-control to remaining classes
context.visibleAtts = AutoForm.Utility.addClass({ ...visibleAtts }, 'form-control')
context.atts = AutoForm.Utility.addClass({ ...itemAtts }, 'form-control')
context.visibleAtts = visibleAtts

// build items list
context.items = []

Expand Down Expand Up @@ -121,6 +122,11 @@ Template.afAutocomplete.onRendered(function () {
}
})

const updateValue = value => {
$hidden.val(value)
$hidden.trigger('change')
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever! Nicely done. Is it safe to assume that the change event triggers validation? on blur too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the blur change below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is used to fire Updates for any reactive getter like AutoForm.getFieldValue

While blur triggers validation.

// clear on blur?
// TODO: Figure out how blur won't block "click"
// $input.blur((e)=>{ clearDropdown(e) })
Expand All @@ -131,7 +137,7 @@ Template.afAutocomplete.onRendered(function () {
if (/ArrowDown|ArrowUp|ArrowLeft|ArrowRight|Enter|Escape/.test(e.originalEvent.key) === false) {
// we're typing
// ensure hidden and visible values match for validation
$hidden.val($input.val())
updateValue($input.val())
// filter results from visible input value
const result = items.get().filter((i) => {
const reg = new RegExp(e.target.value, 'gi')
Expand Down Expand Up @@ -166,7 +172,7 @@ Template.afAutocomplete.onRendered(function () {
const dataValue = me.$(e.target).attr('data-value')
const dataLabel = me.$(e.target).attr('data-label')
$input.val(dataLabel)
$hidden.val(dataValue)
updateValue(dataValue)
clearDropdown(e, false)
$input.focus()
})
Expand All @@ -176,7 +182,7 @@ Template.afAutocomplete.onRendered(function () {
// bc we all make mistakes
if (result.length === 1) {
$input.val(result[0].label)
$hidden.val(result[0].value)
updateValue(result[0].value)
clearDropdown(e, false)
$input.focus()
}
Expand Down Expand Up @@ -214,13 +220,17 @@ Template.afAutocomplete.onRendered(function () {
const enterValue = $suggestions.children('div').eq(currIndex).attr('data-value')
const enterLabel = $suggestions.children('div').eq(currIndex).attr('data-label')
$input.val(enterLabel)
$hidden.val(enterValue)
updateValue(enterValue)
clearDropdown(e, false)
$input.focus()
}
}
}

$input.blur(() => {
$hidden.trigger('blur') // triggers re-validation
})

// detect keystrokes
$input.keyup((e) => {
callback(e)
Expand All @@ -233,6 +243,7 @@ Template.afAutocomplete.onRendered(function () {

// show on double click
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change comment to show on touch
Good catch!

$input.on('touchstart', (e) => {
$hidden.trigger('touchstart')
callback(e)
})
})