Skip to content

Commit

Permalink
Fixes for API backcompatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
deh-code committed Jun 26, 2024
1 parent 35b1ec4 commit 8492492
Show file tree
Hide file tree
Showing 21 changed files with 765 additions and 715 deletions.
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class SlimSelect {
getData(): DataArray;
setData(data: DataArrayPartial): void;
getSelected(): string[];
setSelected(id: string | string[], runAfterChange?: boolean): void;
setSelected(values: string | string[], runAfterChange?: boolean): void;
addOption(option: OptionOptional): void;
open(): void;
close(eventType?: string | null): void;
Expand Down
24 changes: 18 additions & 6 deletions dist/slimselect.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class Store {
}
}
getSelected() {
return this.getSelectedOptions().map(option => option.id);
return this.getSelectedOptions().map((option) => option.id);
}
getSelectedOptions() {
return this.filter((opt) => {
Expand Down Expand Up @@ -1399,7 +1399,7 @@ class Select {
return options;
}
getSelectedValues() {
return this.getSelectedOptions().map(option => option.value);
return this.getSelectedOptions().map((option) => option.value);
}
setSelected(ids) {
this.changeListen(false);
Expand Down Expand Up @@ -1636,7 +1636,7 @@ class SlimSelect {
this.select.updateSelect(this.settings.id, this.settings.style, this.settings.class);
this.select.hideUI();
this.select.onValueChange = (options) => {
this.setSelected(options.map(option => option.id));
this.setSelected(options.map((option) => option.id));
};
this.select.onClassChange = (classes) => {
this.settings.class = classes;
Expand Down Expand Up @@ -1726,11 +1726,23 @@ class SlimSelect {
}
}
getSelected() {
return this.store.getSelected();
return this.store.getSelectedOptions().map((option) => option.value);
}
setSelected(id, runAfterChange = true) {
setSelected(values, runAfterChange = true) {
const selected = this.store.getSelected();
this.store.setSelectedBy('id', Array.isArray(id) ? id : [id]);
const options = this.store.getDataOptions();
values = Array.isArray(values) ? values : [values];
const ids = [];
for (const value of values) {
if (options.find((option) => option.id == value)) {
ids.push(value);
continue;
}
for (const option of options.filter((option) => option.value == value)) {
ids.push(option.id);
}
}
this.store.setSelectedBy('id', ids);
const data = this.store.getData();
this.select.updateOptions(data);
this.render.renderValues();
Expand Down
24 changes: 18 additions & 6 deletions dist/slimselect.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class Store {
}
}
getSelected() {
return this.getSelectedOptions().map(option => option.id);
return this.getSelectedOptions().map((option) => option.id);
}
getSelectedOptions() {
return this.filter((opt) => {
Expand Down Expand Up @@ -1397,7 +1397,7 @@ class Select {
return options;
}
getSelectedValues() {
return this.getSelectedOptions().map(option => option.value);
return this.getSelectedOptions().map((option) => option.value);
}
setSelected(ids) {
this.changeListen(false);
Expand Down Expand Up @@ -1634,7 +1634,7 @@ class SlimSelect {
this.select.updateSelect(this.settings.id, this.settings.style, this.settings.class);
this.select.hideUI();
this.select.onValueChange = (options) => {
this.setSelected(options.map(option => option.id));
this.setSelected(options.map((option) => option.id));
};
this.select.onClassChange = (classes) => {
this.settings.class = classes;
Expand Down Expand Up @@ -1724,11 +1724,23 @@ class SlimSelect {
}
}
getSelected() {
return this.store.getSelected();
return this.store.getSelectedOptions().map((option) => option.value);
}
setSelected(id, runAfterChange = true) {
setSelected(values, runAfterChange = true) {
const selected = this.store.getSelected();
this.store.setSelectedBy('id', Array.isArray(id) ? id : [id]);
const options = this.store.getDataOptions();
values = Array.isArray(values) ? values : [values];
const ids = [];
for (const value of values) {
if (options.find((option) => option.id == value)) {
ids.push(value);
continue;
}
for (const option of options.filter((option) => option.value == value)) {
ids.push(option.id);
}
}
this.store.setSelectedBy('id', ids);
const data = this.store.getData();
this.select.updateOptions(data);
this.render.renderValues();
Expand Down
24 changes: 18 additions & 6 deletions dist/slimselect.global.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ var SlimSelect = (function () {
}
}
getSelected() {
return this.getSelectedOptions().map(option => option.id);
return this.getSelectedOptions().map((option) => option.id);
}
getSelectedOptions() {
return this.filter((opt) => {
Expand Down Expand Up @@ -1400,7 +1400,7 @@ var SlimSelect = (function () {
return options;
}
getSelectedValues() {
return this.getSelectedOptions().map(option => option.value);
return this.getSelectedOptions().map((option) => option.value);
}
setSelected(ids) {
this.changeListen(false);
Expand Down Expand Up @@ -1637,7 +1637,7 @@ var SlimSelect = (function () {
this.select.updateSelect(this.settings.id, this.settings.style, this.settings.class);
this.select.hideUI();
this.select.onValueChange = (options) => {
this.setSelected(options.map(option => option.id));
this.setSelected(options.map((option) => option.id));
};
this.select.onClassChange = (classes) => {
this.settings.class = classes;
Expand Down Expand Up @@ -1727,11 +1727,23 @@ var SlimSelect = (function () {
}
}
getSelected() {
return this.store.getSelected();
return this.store.getSelectedOptions().map((option) => option.value);
}
setSelected(id, runAfterChange = true) {
setSelected(values, runAfterChange = true) {
const selected = this.store.getSelected();
this.store.setSelectedBy('id', Array.isArray(id) ? id : [id]);
const options = this.store.getDataOptions();
values = Array.isArray(values) ? values : [values];
const ids = [];
for (const value of values) {
if (options.find((option) => option.id == value)) {
ids.push(value);
continue;
}
for (const option of options.filter((option) => option.value == value)) {
ids.push(option.id);
}
}
this.store.setSelectedBy('id', ids);
const data = this.store.getData();
this.select.updateOptions(data);
this.render.renderValues();
Expand Down
24 changes: 18 additions & 6 deletions dist/slimselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
}
}
getSelected() {
return this.getSelectedOptions().map(option => option.id);
return this.getSelectedOptions().map((option) => option.id);
}
getSelectedOptions() {
return this.filter((opt) => {
Expand Down Expand Up @@ -1403,7 +1403,7 @@
return options;
}
getSelectedValues() {
return this.getSelectedOptions().map(option => option.value);
return this.getSelectedOptions().map((option) => option.value);
}
setSelected(ids) {
this.changeListen(false);
Expand Down Expand Up @@ -1640,7 +1640,7 @@
this.select.updateSelect(this.settings.id, this.settings.style, this.settings.class);
this.select.hideUI();
this.select.onValueChange = (options) => {
this.setSelected(options.map(option => option.id));
this.setSelected(options.map((option) => option.id));
};
this.select.onClassChange = (classes) => {
this.settings.class = classes;
Expand Down Expand Up @@ -1730,11 +1730,23 @@
}
}
getSelected() {
return this.store.getSelected();
return this.store.getSelectedOptions().map((option) => option.value);
}
setSelected(id, runAfterChange = true) {
setSelected(values, runAfterChange = true) {
const selected = this.store.getSelected();
this.store.setSelectedBy('id', Array.isArray(id) ? id : [id]);
const options = this.store.getDataOptions();
values = Array.isArray(values) ? values : [values];
const ids = [];
for (const value of values) {
if (options.find((option) => option.id == value)) {
ids.push(value);
continue;
}
for (const option of options.filter((option) => option.value == value)) {
ids.push(option.id);
}
}
this.store.setSelectedBy('id', ids);
const data = this.store.getData();
this.select.updateOptions(data);
this.render.renderValues();
Expand Down
2 changes: 1 addition & 1 deletion dist/slimselect.min.js

Large diffs are not rendered by default.

24 changes: 18 additions & 6 deletions dist/slimselect.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
}
}
getSelected() {
return this.getSelectedOptions().map(option => option.id);
return this.getSelectedOptions().map((option) => option.id);
}
getSelectedOptions() {
return this.filter((opt) => {
Expand Down Expand Up @@ -1403,7 +1403,7 @@
return options;
}
getSelectedValues() {
return this.getSelectedOptions().map(option => option.value);
return this.getSelectedOptions().map((option) => option.value);
}
setSelected(ids) {
this.changeListen(false);
Expand Down Expand Up @@ -1640,7 +1640,7 @@
this.select.updateSelect(this.settings.id, this.settings.style, this.settings.class);
this.select.hideUI();
this.select.onValueChange = (options) => {
this.setSelected(options.map(option => option.id));
this.setSelected(options.map((option) => option.id));
};
this.select.onClassChange = (classes) => {
this.settings.class = classes;
Expand Down Expand Up @@ -1730,11 +1730,23 @@
}
}
getSelected() {
return this.store.getSelected();
return this.store.getSelectedOptions().map((option) => option.value);
}
setSelected(id, runAfterChange = true) {
setSelected(values, runAfterChange = true) {
const selected = this.store.getSelected();
this.store.setSelectedBy('id', Array.isArray(id) ? id : [id]);
const options = this.store.getDataOptions();
values = Array.isArray(values) ? values : [values];
const ids = [];
for (const value of values) {
if (options.find((option) => option.id == value)) {
ids.push(value);
continue;
}
for (const option of options.filter((option) => option.value == value)) {
ids.push(option.id);
}
}
this.store.setSelectedBy('id', ids);
const data = this.store.getData();
this.select.updateOptions(data);
this.render.renderValues();
Expand Down
2 changes: 1 addition & 1 deletion dist/slimselect.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/index.js

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions src/slim-select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,31 @@ export default class SlimSelect {
}

public getSelected(): string[] {
return this.store.getSelected()
return this.store.getSelectedOptions().map((option) => option.value)
}

public setSelected(id: string | string[], runAfterChange = true): void {
public setSelected(values: string | string[], runAfterChange = true): void {
// Get original selected values
const selected = this.store.getSelected()
const options = this.store.getDataOptions()
values = Array.isArray(values) ? values : [values]
const ids = []

// for back-compatibility support both, set by id and set by value
for (const value of values) {
if (options.find((option) => option.id == value)) {
ids.push(value)
continue
}

// if option with given id is not found try to search by value
for (const option of options.filter((option) => option.value == value)) {
ids.push(option.id)
}
}

// Update the store
this.store.setSelectedBy('id', Array.isArray(id) ? id : [id])
this.store.setSelectedBy('id', ids)
const data = this.store.getData()

// Update the select element
Expand Down
14 changes: 5 additions & 9 deletions src/vue/dist/slim-select/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export declare function generateID(): string
export declare function hasClassInTree(element: HTMLElement, className: string): any
export declare function debounce<T extends (...args: any[]) => void>(
func: T,
wait?: number,
immediate?: boolean,
): () => void
export declare function isEqual(a: any, b: any): boolean
export declare function kebabCase(str: string): string
export declare function generateID(): string;
export declare function hasClassInTree(element: HTMLElement, className: string): any;
export declare function debounce<T extends (...args: any[]) => void>(func: T, wait?: number, immediate?: boolean): () => void;
export declare function isEqual(a: any, b: any): boolean;
export declare function kebabCase(str: string): string;
Loading

0 comments on commit 8492492

Please sign in to comment.