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

Resolve #751/#773 #787

Open
wants to merge 8 commits into
base: master
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
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -824,12 +824,6 @@ choices.setValue(['Set value 1', 'Set value 2']);
choices.disable();
```

### destroy();

**Input types affected:** `text`, `select-multiple`, `select-one`

**Usage:** Kills the instance of Choices, removes all event listeners and returns passed input to its initial state.

### init();

**Input types affected:** `text`, `select-multiple`, `select-one`
Expand All @@ -838,6 +832,12 @@ choices.disable();

**Note:** This is called implicitly when a new instance of Choices is created. This would be used after a Choices instance had already been destroyed (using `destroy()`).

### destroy();

**Input types affected:** `text`, `select-multiple`, `select-one`

**Usage:** Kills the instance of Choices, removes all event listeners and returns passed input to its initial state.

### highlightAll();

**Input types affected:** `text`, `select-multiple`
Expand Down Expand Up @@ -880,7 +880,7 @@ choices.disable();

**Usage:** Hide option list dropdown (only affects select inputs).

### setChoices(choices, value, label, replaceChoices);
### setChoices(choices[, value[, label[, replaceChoices]]]);

**Input types affected:** `select-one`, `select-multiple`

Expand Down Expand Up @@ -966,7 +966,13 @@ example.setChoices(

**Input types affected:** `select-one`, `select-multiple`

**Usage:** Clear all choices from select
**Usage:** Clear all choices

### clearItems();

**Input types affected:** `text`, `select-one`, `select-multiple`

**Usage:** Clear all items

### getValue(valueOnly)

Expand Down
7 changes: 7 additions & 0 deletions src/scripts/actions/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ export const highlightItem = (id, highlighted) => ({
id,
highlighted,
});

/**
* @returns {Action}
*/
export const clearItems = () => ({
type: ACTION_TYPES.CLEAR_ITEMS,
});
10 changes: 10 additions & 0 deletions src/scripts/actions/items.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ describe('actions/items', () => {
expect(actions.highlightItem(id, highlighted)).to.eql(expectedAction);
});
});

describe('clearItems action', () => {
it('returns CLEAR_ITEMS action', () => {
const expectedAction = {
type: 'CLEAR_ITEMS',
};

expect(actions.clearItems()).to.eql(expectedAction);
});
});
});
17 changes: 14 additions & 3 deletions src/scripts/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import {
activateChoices,
clearChoices,
} from './actions/choices';
import { addItem, removeItem, highlightItem } from './actions/items';
import {
addItem,
removeItem,
highlightItem,
clearItems,
} from './actions/items';
import { addGroup } from './actions/groups';
import { clearAll, resetTo, setIsLoading } from './actions/misc';
import {
Expand Down Expand Up @@ -496,7 +501,7 @@ class Choices {
* @param {T} [choicesArrayOrFetcher]
* @param {string} [value = 'value'] - name of `value` field
* @param {string} [label = 'label'] - name of 'label' field
* @param {boolean} [replaceChoices = false] - whether to replace of add choices
* @param {boolean} [replaceChoices = false] - whether to clear existing choices
* @returns {this | Promise<this>}
*
* @example
Expand Down Expand Up @@ -574,9 +579,9 @@ class Choices {
);
}

// Clear choices if needed
if (replaceChoices) {
this.clearChoices();
this.clearItems();
}

if (typeof choicesArrayOrFetcher === 'function') {
Expand Down Expand Up @@ -651,6 +656,12 @@ class Choices {
return this;
}

clearItems() {
this._store.dispatch(clearItems());

return this;
}

clearStore() {
this._store.dispatch(clearAll());

Expand Down
Loading