Skip to content
This repository has been archived by the owner on Jun 17, 2020. It is now read-only.

Store reference to the dropdown's active item #125

Merged
merged 1 commit into from
Dec 5, 2017
Merged
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
22 changes: 11 additions & 11 deletions src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type DropdownOptions = {
export default class Dropdown extends EventEmitter {
shown: boolean
items: DropdownItem[]
activeItem: DropdownItem | null
footer: $PropertyType<DropdownOptions, "footer">
header: $PropertyType<DropdownOptions, "header">
maxCount: $PropertyType<DropdownOptions, "maxCount">
Expand All @@ -52,6 +53,7 @@ export default class Dropdown extends EventEmitter {
super()
this.shown = false
this.items = []
this.activeItem = null
this.footer = options.footer
this.header = options.header
this.maxCount = options.maxCount || 10
Expand Down Expand Up @@ -155,8 +157,8 @@ export default class Dropdown extends EventEmitter {
/**
* Retrieve the active item.
*/
getActiveItem(): ?DropdownItem {
return this.items.find(item => item.active)
getActiveItem(): DropdownItem | null {
return this.activeItem
}

/**
Expand Down Expand Up @@ -246,15 +248,13 @@ export default class Dropdown extends EventEmitter {
}

/** @private */
moveActiveItem(name: "next" | "prev", e: CustomEvent) {
const activeItem: any = this.getActiveItem()
let nextActiveItem
if (activeItem) {
nextActiveItem = activeItem[name]
} else {
nextActiveItem =
name === "next" ? this.items[0] : this.items[this.items.length - 1]
}
moveActiveItem(direction: "next" | "prev", e: CustomEvent) {
const nextActiveItem =
direction === "next"
? this.activeItem ? this.activeItem.next : this.items[0]
: this.activeItem
? this.activeItem.prev
: this.items[this.items.length - 1]
if (nextActiveItem) {
nextActiveItem.activate()
e.preventDefault()
Expand Down
5 changes: 5 additions & 0 deletions src/dropdown_item.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export default class DropdownItem {
this.el.removeEventListener("mousedown", this.onClick, false)
this.el.removeEventListener("mouseover", this.onMouseover, false)
this.el.removeEventListener("touchstart", this.onClick, false)
if (this.active) {
this.dropdown.activeItem = null
}
// This element has already been removed by {@link Dropdown#clear}.
this._el = null
}
Expand All @@ -76,6 +79,7 @@ export default class DropdownItem {
if (activeItem) {
activeItem.deactivate()
}
this.dropdown.activeItem = this
this.active = true
this.el.className = ACTIVE_CLASS_NAME
}
Expand Down Expand Up @@ -119,6 +123,7 @@ export default class DropdownItem {
if (this.active) {
this.active = false
this.el.className = CLASS_NAME
this.dropdown.activeItem = null
}
return this
}
Expand Down
12 changes: 2 additions & 10 deletions test/unit/dropdown_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ require("../test_helper")
import Dropdown from "../../src/dropdown"
import DropdownItem from "../../src/dropdown_item"
import { createSearchResult } from "../test_utils"
import isUndefined from "lodash.isundefined"

const assert = require("power-assert")

Expand Down Expand Up @@ -65,7 +64,6 @@ describe("Dropdown", function() {
dropdown = new Dropdown({})
assert.strictEqual(subject(), dropdown)
})

;["render", "rendered"].forEach(name => {
it(`should emit ${name} event`, function() {
var spy = this.sinon.spy()
Expand Down Expand Up @@ -106,7 +104,6 @@ describe("Dropdown", function() {
subject()
assert(dropdown.shown)
})

;["show", "shown"].forEach(eventName => {
it(`should emit ${eventName} event`, function() {
var spy = this.sinon.spy()
Expand All @@ -132,7 +129,6 @@ describe("Dropdown", function() {
dropdown = new Dropdown({})
dropdown.shown = true
})

;["show", "shown"].forEach(eventName => {
it(`should not emit ${eventName} event`, function() {
var spy = this.sinon.spy()
Expand All @@ -156,7 +152,6 @@ describe("Dropdown", function() {
dropdown = new Dropdown({})
dropdown.shown = true
})

;["show", "shown"].forEach(eventName => {
it(`should not emit ${eventName} event`, function() {
var spy = this.sinon.spy()
Expand All @@ -172,7 +167,6 @@ describe("Dropdown", function() {
dropdown = new Dropdown({})
dropdown.shown = false
})

;["show", "shown"].forEach(eventName => {
it(`should emit ${eventName} event`, function() {
var spy = this.sinon.spy()
Expand Down Expand Up @@ -304,7 +298,6 @@ describe("Dropdown", function() {
subject()
assert(!dropdown.shown)
})

;["hide", "hidden"].forEach(eventName => {
it(`should emit ${eventName} event`, function() {
var spy = this.sinon.spy()
Expand Down Expand Up @@ -357,7 +350,6 @@ describe("Dropdown", function() {
subject()
assert(stub.calledOnce)
})

;["select", "selected"].forEach(name => {
it(`should emit a ${name} event`, function() {
var spy = this.sinon.spy()
Expand Down Expand Up @@ -527,8 +519,8 @@ describe("Dropdown", function() {
}

context("without active item", function() {
it("should return undefined", function() {
assert(isUndefined(subject()))
it("should return null", function() {
assert(subject() === null)
})
})

Expand Down