From 9f2b15ecd2c98a1a4d168300ef11c5ce4b7e9e0d Mon Sep 17 00:00:00 2001 From: Sean McIntyre Date: Fri, 27 Apr 2018 16:35:08 -0400 Subject: [PATCH] Need to refresh suggestions in OptionAssistant when a value is unboxed. --- .../assistants/generic/option-assistant.jsx | 24 +++++++++++++++++-- src/components/search-bar.jsx | 4 ++-- src/lib/state.js | 5 ++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/components/assistants/generic/option-assistant.jsx b/src/components/assistants/generic/option-assistant.jsx index 24e4d44..39ff889 100644 --- a/src/components/assistants/generic/option-assistant.jsx +++ b/src/components/assistants/generic/option-assistant.jsx @@ -26,6 +26,27 @@ export class OptionAssistant extends Assistant { return options.filter(o => !o.hidden && !lookup.has(o.key)).slice(0, this.machineStateTemplate.suggestionLimit); } + cleanupListeners () { + super.cleanupListeners(); + if (this.machineState) { + this.machineState.removeListener('value unarchived', this.onValueUnarchived); + } + } + + connectListeners () { + super.connectListeners(); + if (this.machineState) { + this.machineState.on('value unarchived', this.onValueUnarchived); + } + } + + @Bind + onValueUnarchived () { + if (this.machineStateTemplate) { + setTimeout(() => this.machineStateTemplate.refreshOptions('', this.boxedValue, this.boxedArchive)); + } + } + @Bind onOptionsChanged (newOptions) { this.setState({ @@ -42,7 +63,7 @@ export class OptionAssistant extends Assistant { const result = this.requestArchive(); if (result) { this.machineState.unboxedValue = null; - this.machineStateTemplate.refreshOptions('', this.machine.boxedValue, this.boxedArchive); + this.machineStateTemplate.refreshOptions('', this.boxedValue, this.boxedArchive); this.setState({ suggestions: this.getSuggestions() }); @@ -60,7 +81,6 @@ export class OptionAssistant extends Assistant { @Bind onArchivedRemoved (idx) { this.requestRemoveArchivedValue(idx); - this.machineStateTemplate.refreshOptions('', this.machine.boxedValue, this.boxedArchive); this.setState({ suggestions: this.getSuggestions() }); diff --git a/src/components/search-bar.jsx b/src/components/search-bar.jsx index 9316492..a1f7202 100644 --- a/src/components/search-bar.jsx +++ b/src/components/search-bar.jsx @@ -78,7 +78,7 @@ export class SearchBar extends Component { if (value !== this.state.tokenValues) { this.setState({ tokenValues: value.map(v => { - const m = new TokenStateMachine(this.state.machineTemplate) + const m = new TokenStateMachine(this.state.machineTemplate); m.bindValues(v); return m; }) // box incoming values @@ -374,7 +374,7 @@ export class SearchBar extends Component { return true; } catch (err) { if (err instanceof ValueArchiveError) { - console.error(err.message); + console.error(err.message); // eslint-disable-line no-console return false; } else { throw err; diff --git a/src/lib/state.js b/src/lib/state.js index 7c5b20d..35e9f99 100644 --- a/src/lib/state.js +++ b/src/lib/state.js @@ -259,6 +259,8 @@ export class StateTemplate extends EventEmitter { * * This class is an `EventEmitter`, exposing the following events: * - `on('value changed', (newVal, oldVal) => {})` when the internal value changes. + * - `on('value archived', () => {})` when a value is archived. + * - `on('value unarchived', () => {})` when a value is archived. * - `on('preview value changed', (newVal, oldVal) => {})` when the internal preview value changes. * - `on('unboxed value change attempted', (newUnboxedVal, oldUnboxedVal))` when a user attempts to change the unboxed value. If it cannot be boxed, it may not trigger `value changed`. * @@ -526,6 +528,7 @@ export class State extends EventEmitter { this.value = this.defaultValue; this.previewValue = null; this.emit('value changed', this.value, oldVal, this.unboxedValue, oldUnboxedVal); + this.emit('value archived'); } /** @@ -536,6 +539,7 @@ export class State extends EventEmitter { const oldUnboxedVal = this.unboxedValue; this.value = this.archive.pop(); this.emit('value changed', this.value, oldVal, this.unboxedValue, oldUnboxedVal); + this.emit('value unarchived'); } /** @@ -546,5 +550,6 @@ export class State extends EventEmitter { removeArchivedValue (idx) { this.archive.splice(idx, 1); this.emit('value changed', this.value, this.value, this.unboxedValue, this.unboxedValue); + this.emit('value unarchived'); } }