diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f16c5c2..6717a541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog -## [11.0.2] +## [11.0.3] + +### Bug Fixes (from 11.0.0) +* Fix input text - method setValue didn't work [#1207](https://github.com/Choices-js/Choices/issues/1207) + +## [11.0.2] (2024-09-05) ### Features (from 11.0.0) * Pass `getClassNames` as the 3rd argument to `callbackOnCreateTemplates` callback diff --git a/src/scripts/choices.ts b/src/scripts/choices.ts index 58bdcec3..70babc07 100644 --- a/src/scripts/choices.ts +++ b/src/scripts/choices.ts @@ -19,7 +19,6 @@ import { removeClassesFromElement, resolveNoticeFunction, resolveStringFunction, - sanitise, sortByRank, strToEl, } from './lib/utils'; @@ -1324,7 +1323,7 @@ class Choices { if (this.passedElement.value) { const elementItems: ChoiceFull[] = this.passedElement.value .split(config.delimiter) - .map((e: string) => mapInputToChoice(e, false)); + .map((e: string) => mapInputToChoice(e, false, this.config.allowHtmlUserInput)); this._presetChoices = this._presetChoices.concat(elementItems); } this._presetChoices.forEach((choice: ChoiceFull) => { @@ -1740,21 +1739,7 @@ class Choices { return; } - const sanitisedValue = sanitise(value); - const userValue = - this.config.allowHtmlUserInput || sanitisedValue === value ? value : { escaped: sanitisedValue, raw: value }; - this._addChoice( - mapInputToChoice( - { - value: userValue, - label: userValue, - selected: true, - }, - false, - ), - true, - true, - ); + this._addChoice(mapInputToChoice(value, false, this.config.allowHtmlUserInput), true, true); addedItem = true; } diff --git a/src/scripts/lib/choice-input.ts b/src/scripts/lib/choice-input.ts index d277d192..ca990f94 100644 --- a/src/scripts/lib/choice-input.ts +++ b/src/scripts/lib/choice-input.ts @@ -2,7 +2,7 @@ import { InputChoice } from '../interfaces/input-choice'; import { InputGroup } from '../interfaces/input-group'; import { GroupFull } from '../interfaces/group-full'; import { ChoiceFull } from '../interfaces/choice-full'; -import { unwrapStringForRaw } from './utils'; +import { sanitise, unwrapStringForRaw } from './utils'; type MappedInputTypeToChoiceType = T extends InputGroup ? GroupFull @@ -27,12 +27,17 @@ export const stringToHtmlClass = (input: string | string[] | undefined): string[ export const mapInputToChoice = ( value: T, allowGroup: boolean, + allowRawString: boolean = true, ): MappedInputTypeToChoiceType => { if (typeof value === 'string') { + const sanitisedValue = sanitise(value); + const userValue = allowRawString || sanitisedValue === value ? value : { escaped: sanitisedValue, raw: value }; + const result: ChoiceFull = mapInputToChoice( { value, - label: value, + label: userValue, + selected: true, }, false, );