Skip to content

Commit

Permalink
pkp/pkp-lib#10624 Move autosuggest functions from api to component
Browse files Browse the repository at this point in the history
  • Loading branch information
blesildaramirez committed Dec 2, 2024
1 parent edf49a6 commit a49eace
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 46 deletions.
49 changes: 33 additions & 16 deletions src/components/Form/fields/Autosuggest.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<span class="-screenReader">{{ selectedLabel }}</span>
<span v-if="!currentValue.length" class="-screenReader">
<span v-if="!currentSelected.length" class="-screenReader">
{{ t('common.none') }}
</span>
<PkpBadge
Expand Down Expand Up @@ -34,9 +34,9 @@
ref="autosuggestInput"
class="pkpAutosuggest__input"
v-bind="inputProps"
@change="(event) => handleChange(event, emit)"
@focus="() => handleFocus(emit)"
@blur="() => handleBlur(emit)"
@change="(event) => handleChange(event)"
@focus="() => handleFocus()"
@blur="() => handleBlur()"
/>
<ComboboxOptions
v-if="suggestions.length || (allowCustom && localInputValue?.length)"
Expand Down Expand Up @@ -77,7 +77,7 @@
</Combobox>
</template>
<script setup>
import {useSlots} from 'vue';
import {useSlots, ref, inject} from 'vue';
import {
Combobox,
ComboboxInput,
Expand All @@ -86,11 +86,10 @@ import {
} from '@headlessui/vue';
import PkpBadge from '@/components/Badge/Badge.vue';
import Icon from '@/components/Icon/Icon.vue';
import {useAutosuggest} from '@/composables/useAutosuggest';
const slots = useSlots();
defineProps({
const props = defineProps({
id: {
type: String,
required: true,
Expand All @@ -107,24 +106,26 @@ defineProps({
type: String,
required: true,
},
currentValue: {
type: Array,
default: () => [],
},
currentSelected: {
type: Array,
default: () => [],
},
isDisabled: {
type: Boolean,
default() {
return false;
},
default: () => false,
},
deselectLabel: {
type: String,
required: true,
},
inputValue: {
type: String,
default: () => '',
},
isFocused: {
type: Boolean,
default: () => false,
},
});
const emit = defineEmits([
Expand All @@ -134,8 +135,24 @@ const emit = defineEmits([
'deselect',
]);
const {allowCustom, localInputValue, handleChange, handleFocus, handleBlur} =
useAutosuggest();
const allowCustom = inject('allowCustom', false);
const localInputValue = ref('');
const localIsFocused = ref(props.isFocused);
function handleChange(event) {
localInputValue.value = event.target.value.trim();
emit('update:inputValue', localInputValue.value);
}
function handleFocus() {
localIsFocused.value = true;
emit('update:isFocused', localIsFocused.value);
}
function handleBlur() {
localIsFocused.value = false;
emit('update:isFocused', localIsFocused.value);
}
function selectSuggestion(suggestion) {
emit('select-suggestion', suggestion);
Expand Down
1 change: 0 additions & 1 deletion src/components/Form/fields/FieldBaseAutosuggest.vue
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ export default {
inputProps: this.inputProps,
suggestions: this.suggestions,
selectedLabel: this.selectedLabel,
currentValue: this.currentValue,
currentSelected: this.currentSelected,
isDisabled: this.isDisabled,
deselectLabel: this.deselectLabel,
Expand Down
30 changes: 1 addition & 29 deletions src/composables/useAutosuggest.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
import {ref, inject} from 'vue';

export function useAutosuggest() {
const allowCustom = inject('allowCustom', false);
const localInputValue = ref('');
const isFocused = ref(false);

function handleChange(event, emit) {
localInputValue.value = event.target.value.trim();
emit('update:inputValue', localInputValue.value);
}

function handleFocus(emit) {
isFocused.value = true;
emit('update:isFocused', isFocused.value);
}

function handleBlur(emit) {
isFocused.value = false;
emit('update:isFocused', isFocused.value);
}

return {
allowCustom,
localInputValue,
isFocused,
handleChange,
handleFocus,
handleBlur,
};
return {};
}

0 comments on commit a49eace

Please sign in to comment.