Files
sundynix-pets/pets-fe/components/pet-picker/index.js
T

27 lines
657 B
JavaScript

Component({
properties: {
pets: { type: Array, value: [] },
selected: { type: Array, value: [] },
},
data: { viewPets: [] },
observers: {
"pets,selected": function (pets, selected) {
this.setData({
viewPets: (pets || []).map((p) => ({
...p,
chosen: (selected || []).includes(p.id),
})),
});
},
},
methods: {
toggle(e) {
const id = e.currentTarget.dataset.id;
const selected = this.data.selected.includes(id)
? this.data.selected.filter((x) => x !== id)
: [...this.data.selected, id];
this.triggerEvent("change", { selected });
},
},
});