Skip to content

Commit

Permalink
before: addItems was basically just an alias for disable, now: addIte…
Browse files Browse the repository at this point in the history
…ms controls if user can add choices/items
  • Loading branch information
devkral committed Jul 25, 2023
1 parent cf34edd commit 36a5a0b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
24 changes: 21 additions & 3 deletions cypress/e2e/text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,28 @@ describe('Choices - text element', () => {
});

describe('adding items disabled', () => {
it('does not allow me to input data', () => {
cy.get('[data-test-hook=adding-items-disabled]')
/*
{
addItems: false,
}
*/
beforeEach(() => {
cy.get('[data-test-hook=add-items-disabled]')
.find('.choices')
.click();
});
it('disables adding new items', () => {
const newChoice = 'New Choice';
cy.get('[data-test-hook=add-items-disabled]')
.find('.choices__input--cloned')
.should('be.disabled');
.type(newChoice)
.type('{enter}');
cy.get('[data-test-hook=add-items-disabled]')
.find('.choices__list--multiple')
.last()
.should($el => {
expect($el).to.not.contain(newChoice);
});
});
});

Expand Down
6 changes: 3 additions & 3 deletions public/test/text/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ <h2>Text inputs</h2>
/>
</div>

<div data-test-hook="adding-items-disabled">
<label for="choices-adding-items-disabled">Add items disabled</label>
<div data-test-hook="add-items-disabled">
<label for="choices-add-items-disabled">Add items disabled</label>
<input
class="form-control"
id="choices-adding-items-disabled"
id="choices-add-items-disabled"
type="text"
/>
</div>
Expand Down
8 changes: 3 additions & 5 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,7 @@ class Choices implements Choices {
this._render();
this._addEventListeners();

let shouldDisable = this.passedElement.element.hasAttribute('disabled');
if (this._isTextElement) {
shouldDisable = shouldDisable || !this.config.addItems;
}
const shouldDisable = this.passedElement.element.hasAttribute('disabled');

if (shouldDisable) {
this.disable();
Expand Down Expand Up @@ -1562,7 +1559,8 @@ class Choices implements Choices {
// We are typing into a text input and have a value, we want to show a dropdown
// notice. Otherwise hide the dropdown
if (this._isTextElement) {
const canShowDropdownNotice = canAddItem.notice && value;
const canShowDropdownNotice =
this.config.addItems && canAddItem.notice && value;

if (canShowDropdownNotice) {
const dropdownItem = this._getTemplate('notice', canAddItem.notice);
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/interfaces/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export interface Options {
maxItemCount: number;

/**
* Whether a user can add items.
* Whether a user can add new items.
*
* **Input types affected:** text, select, select-multiple
*
Expand Down

0 comments on commit 36a5a0b

Please sign in to comment.