-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from Robert-2/feature/36-select-search
Ajoute un système de recherche dans le choix des bénéficiaires et techniciens (#36)
- Loading branch information
Showing
23 changed files
with
366 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import VueSelect from 'vue-select'; | ||
import { debounce } from 'debounce'; | ||
import { DEBOUNCE_WAIT } from '@/config/constants'; | ||
|
||
export default { | ||
name: 'MultipleItem', | ||
components: { VueSelect }, | ||
props: { | ||
fetchEntity: String, | ||
fetchParams: Object, | ||
label: String, | ||
field: String, | ||
selectedItems: Array, | ||
createItemPath: String, | ||
formatOptions: Function, | ||
getItemLabel: Function, | ||
}, | ||
data() { | ||
const defaultItem = { value: null, label: this.$t('please-choose') }; | ||
|
||
return { | ||
itemsIds: this.selectedItems.map((item) => item.id), | ||
notSavedSelectedItems: [...this.selectedItems], | ||
minSearchCharacters: 2, | ||
askNewItem: false, | ||
fieldOptions: [], | ||
newItem: defaultItem, | ||
defaultItem, | ||
}; | ||
}, | ||
computed: { | ||
selectableOptions() { | ||
return this.fieldOptions.filter( | ||
(option) => !this.itemsIds.includes(option.value), | ||
); | ||
}, | ||
}, | ||
methods: { | ||
handleSearch(searchTerm, loading) { | ||
if (searchTerm.length < this.minSearchCharacters) { | ||
return; | ||
} | ||
loading(true); | ||
this.search(loading, searchTerm); | ||
}, | ||
|
||
// - We're not using arrow function here because we need access to 'this' | ||
// eslint-disable-next-line func-names | ||
search: debounce(function (loading, search) { | ||
const params = { ...this.fetchParams, search, limit: 10 }; | ||
this.$http.get(this.fetchEntity, { params }) | ||
.then(({ data }) => { | ||
this.fieldOptions = this.formatOptions(data.data); | ||
}) | ||
.catch(console.error) | ||
.finally(() => { | ||
loading(false); | ||
}); | ||
}, DEBOUNCE_WAIT), | ||
|
||
startAddItem(e) { | ||
e.preventDefault(); | ||
this.askNewItem = true; | ||
}, | ||
|
||
insertNewItem() { | ||
if (!this.newItem || !this.newItem.value) { | ||
return; | ||
} | ||
|
||
const { value, label } = this.newItem; | ||
this.itemsIds.push(value); | ||
this.notSavedSelectedItems.push({ id: value, label }); | ||
|
||
this.askNewItem = false; | ||
this.newItem = this.defaultItem; | ||
|
||
this.$emit('itemsUpdated', this.itemsIds); | ||
}, | ||
|
||
cancelNewItem(e) { | ||
e.preventDefault(); | ||
this.askNewItem = false; | ||
this.newItem = this.defaultItem; | ||
}, | ||
|
||
removeItem(id) { | ||
this.itemsIds = this.itemsIds.filter( | ||
(_id) => _id !== id, | ||
); | ||
this.notSavedSelectedItems = this.notSavedSelectedItems.filter( | ||
(item) => item.id !== id, | ||
); | ||
this.$emit('itemsUpdated', this.itemsIds); | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.