Skip to content

Commit

Permalink
feat(depreciated-components): replaces UnnnicAutocomplete to UnnnicSe…
Browse files Browse the repository at this point in the history
…lectSmart with autocomplete
  • Loading branch information
cristiantela committed Jun 25, 2024
1 parent 95ab759 commit 00cb8cb
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 219 deletions.
36 changes: 36 additions & 0 deletions src/components/Autocomplete.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
<UnnnicSelectSmart
:value="[
finalOptions.find(({ value: insideValue }) => insideValue === value),
]"
@input="$emit('input', $event[0].value)"
:options="finalOptions"
orderedByIndex
autocomplete
autocompleteClearOnFocus
v-bind="$attrs"
/>
</template>

<script>
export default {
props: {
value: String,
placeholder: {
type: String,
default: '',
},
options: Array,
},
computed: {
finalOptions() {
return [{ value: '', label: this.placeholder }].concat(
this.options.map((value) => ({ value, label: value })),
);
},
},
};
</script>
22 changes: 5 additions & 17 deletions src/components/example/NewExampleForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@
</div>

<UnnnicFormElement :message="errors.intent">
<UnnnicAutocomplete
<Autocomplete
v-model="intent"
:data="filteredData"
:options="filteredData"
:placeholder="$t('webapp.example.intent')"
:openWithFocus="true"
@focus="onIntentInputClick"
@blur="onIntentInputClick"
:iconRight="
isIntentInputActive
? 'arrow-button-up-1'
: 'arrow-button-down-1'
"
/>
</UnnnicFormElement>
</div>
Expand Down Expand Up @@ -117,6 +109,7 @@ import { mapActions, mapGetters } from 'vuex';
import { formatters, LANGUAGES } from '@/utils';
import InputWithHightlights from '../InputWithHightlights';
import SelectLanguage from '../SelectLanguage.vue';
import Autocomplete from '../Autocomplete.vue';
export default {
name: 'NewExampleForm',
Expand All @@ -125,6 +118,7 @@ export default {
NewEntitiesInput,
InputWithHightlights,
SelectLanguage,
Autocomplete,
},
props: {
repository: {
Expand All @@ -144,7 +138,6 @@ export default {
entitiesList: [],
blockedNextStepTutorial: false,
hideDropdown: true,
isIntentInputActive: false,
isLanguageInputActive: false,
alertSuccess: false,
addEntity: false,
Expand All @@ -158,9 +151,7 @@ export default {
return this.isValid && !this.submitting;
},
filteredData() {
return (this.repository.intents_list || []).filter((intent) =>
intent.startsWith(this.intent.toLowerCase()),
);
return this.repository.intents_list || [];
},
validationErrors() {
const errors = [];
Expand Down Expand Up @@ -284,9 +275,6 @@ export default {
}
return false;
},
onIntentInputClick() {
this.isIntentInputActive = !this.isIntentInputActive;
},
},
};
</script>
Expand Down
222 changes: 111 additions & 111 deletions src/components/form-generator/inputs/ChoiceInput.vue
Original file line number Diff line number Diff line change
@@ -1,111 +1,111 @@
<template>
<fetch-choice-input
v-if="fetch"
:fetch="fetch"
:label-placeholder="labelPlaceholder"
v-model="value"
@input="update()"/>
<unnnic-autocomplete
v-else-if="compact"
v-model="value"
:placeholder="labelPlaceholder"
:custom-formatter="formatter"
:data="filteredChoices"
dropdown-position="bottom"
openWithFocus
@input="updateInput"
@choose="selectOption"/>
<unnnic-select-smart
v-else
:value="choicesSelectSmart.value"
:options="choicesSelectSmart.options"
@input="update($event[0].value)"
></unnnic-select-smart>
</template>

<script>
import { formatters, useSelectSmart } from '@/utils';
import FetchChoiceInput from './FetchChoiceInput';
export default {
components: {
FetchChoiceInput,
},
props: {
choices: {
type: Array,
default: () => [],
},
fetch: {
type: Function,
default: null,
},
initialData: {
type: [Array, String],
default: null,
},
compact: {
type: Boolean,
default: null,
},
labelPlaceholder: {
type: String,
default: '',
},
},
data() {
return {
value: this.initialData,
formatter: choice => choice.display_name,
};
},
computed: {
filteredChoices() {
if (!this.value || this.value.length === 0) { return this.choices; }
const search = new RegExp(formatters.bothubItemKey()(this.value.toLowerCase()));
return this.choices
.filter(
choice => search.test(formatters.bothubItemKey()(choice.display_name.toLowerCase())),
);
},
choicesSelectSmart() {
return useSelectSmart({
from: this.choices,
value: 'value',
label: 'display_name',
placeholder: '',
currentValue: this.value,
});
},
},
mounted() {
this.update();
},
methods: {
updateInput() {
const search = formatters.bothubItemKey()(this.value.toLowerCase());
const option = this.choices.find(
choice => formatters.bothubItemKey()(choice.display_name.toLowerCase())
=== search,
);
if (option) {
this.$emit('input', option.value);
} else {
this.$emit('input', '');
}
},
selectOption(option) {
if (option) {
this.$emit('input', option.value);
} else {
this.$emit('input', '');
}
},
update(value) {
this.value = value;
this.$emit('input', this.value);
},
},
};
</script>
<template>
<fetch-choice-input
v-if="fetch"
:fetch="fetch"
:label-placeholder="labelPlaceholder"
v-model="value"
@input="update()"/>
<unnnic-autocomplete
v-else-if="compact"
v-model="value"
:placeholder="labelPlaceholder"
:custom-formatter="formatter"
:data="filteredChoices"
dropdown-position="bottom"
openWithFocus
@input="updateInput"
@choose="selectOption"/>
<unnnic-select-smart
v-else
:value="choicesSelectSmart.value"
:options="choicesSelectSmart.options"
@input="update($event[0].value)"
></unnnic-select-smart>
</template>

<script>
import { formatters, useSelectSmart } from '@/utils';
import FetchChoiceInput from './FetchChoiceInput';
export default {
components: {
FetchChoiceInput,
},
props: {
choices: {
type: Array,
default: () => [],
},
fetch: {
type: Function,
default: null,
},
initialData: {
type: [Array, String],
default: null,
},
compact: {
type: Boolean,
default: null,
},
labelPlaceholder: {
type: String,
default: '',
},
},
data() {
return {
value: this.initialData,
formatter: choice => choice.display_name,
};
},
computed: {
filteredChoices() {
if (!this.value || this.value.length === 0) { return this.choices; }
const search = new RegExp(formatters.bothubItemKey()(this.value.toLowerCase()));
return this.choices
.filter(
choice => search.test(formatters.bothubItemKey()(choice.display_name.toLowerCase())),
);
},
choicesSelectSmart() {
return useSelectSmart({
from: this.choices,
value: 'value',
label: 'display_name',
placeholder: '',
currentValue: this.value,
});
},
},
mounted() {
this.update();
},
methods: {
updateInput() {
const search = formatters.bothubItemKey()(this.value.toLowerCase());
const option = this.choices.find(
choice => formatters.bothubItemKey()(choice.display_name.toLowerCase())
=== search,
);
if (option) {
this.$emit('input', option.value);
} else {
this.$emit('input', '');
}
},
selectOption(option) {
if (option) {
this.$emit('input', option.value);
} else {
this.$emit('input', '');
}
},
update(value) {
this.value = value;
this.$emit('input', this.value);
},
},
};
</script>
21 changes: 8 additions & 13 deletions src/components/inputs/EntitiesInput/NewEntitiesInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,15 @@
:closeIcon="false"
>
<div slot="message" class="modal-header text-left">
<unnnic-autocomplete
:label="$t('webapp.trainings.add_entity_field_label')"
ref="entityInputField"
<UnnnicFormElement
:label="$t('webapp.trainings.add_entity_field_label')"
>
<Autocomplete
v-model="entity"
:data="filteredEntities"
:openWithFocus="true"
:iconRight="isEntityInputActive ? 'arrow-button-up-1' : 'arrow-button-down-1'"
@focus="onInputClick()"
@blur="onInputClick()"
/>
ref="entityInputField"
:options="filteredEntities"
/>
</UnnnicFormElement>
<div>
<unnnic-label class="mt-5" :label="$t('webapp.trainings.add_entity_checkbox_title')" />
<div class="words-wrapper">
Expand Down Expand Up @@ -167,7 +166,6 @@ export default {
isOpen: false,
entityModal: false,
entity: '',
isEntityInputActive: false,
selectedEntities: []
};
},
Expand Down Expand Up @@ -282,9 +280,6 @@ export default {
});
this.entities = this.entities.filter(value => !!value);
},
onInputClick() {
this.isEntityInputActive = !this.isEntityInputActive
},
onWordSelected(event) {
const temporaryEntityId = generateTemporaryId();
const start = this.text.indexOf(event.text);
Expand Down
Loading

0 comments on commit 00cb8cb

Please sign in to comment.