Skip to content

Commit

Permalink
Fix input text - method setValue #1207
Browse files Browse the repository at this point in the history
  • Loading branch information
Xon committed Sep 26, 2024
1 parent ace9500 commit dfffed0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 2 additions & 17 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
removeClassesFromElement,
resolveNoticeFunction,
resolveStringFunction,
sanitise,
sortByRank,
strToEl,
} from './lib/utils';
Expand Down Expand Up @@ -1324,7 +1323,7 @@ class Choices {
if (this.passedElement.value) {
const elementItems: ChoiceFull[] = this.passedElement.value
.split(config.delimiter)
.map((e: string) => mapInputToChoice<string>(e, false));
.map((e: string) => mapInputToChoice<string>(e, false, this.config.allowHtmlUserInput));
this._presetChoices = this._presetChoices.concat(elementItems);
}
this._presetChoices.forEach((choice: ChoiceFull) => {
Expand Down Expand Up @@ -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<InputChoice>(
{
value: userValue,
label: userValue,
selected: true,
},
false,
),
true,
true,
);
this._addChoice(mapInputToChoice<string>(value, false, this.config.allowHtmlUserInput), true, true);
addedItem = true;
}

Expand Down
9 changes: 7 additions & 2 deletions src/scripts/lib/choice-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 string | InputChoice | InputGroup> = T extends InputGroup
? GroupFull
Expand All @@ -27,12 +27,17 @@ export const stringToHtmlClass = (input: string | string[] | undefined): string[
export const mapInputToChoice = <T extends string | InputChoice | InputGroup>(
value: T,
allowGroup: boolean,
allowRawString: boolean = true,
): MappedInputTypeToChoiceType<T> => {
if (typeof value === 'string') {
const sanitisedValue = sanitise(value);
const userValue = allowRawString || sanitisedValue === value ? value : { escaped: sanitisedValue, raw: value };

const result: ChoiceFull = mapInputToChoice<InputChoice>(
{
value,
label: value,
label: userValue,
selected: true,
},
false,
);
Expand Down

0 comments on commit dfffed0

Please sign in to comment.