From 0a82e7080e539979dcb05df2e001b90c62975539 Mon Sep 17 00:00:00 2001 From: Blesilda Ramirez Date: Wed, 4 Dec 2024 03:40:24 +0800 Subject: [PATCH] pkp/pkp-lib#10624 Add ROR autosuggest component --- src/components/Form/fields/Autosuggest.vue | 105 ++++++++---- .../fields/FieldBaseAutosuggest.stories.js | 4 +- .../Form/fields/FieldBaseAutosuggest.vue | 39 ++--- .../fields/FieldRorAutosuggest.stories.js | 24 +++ .../Form/fields/FieldRorAutosuggest.vue | 155 ++++++++++++++++++ src/mocks/institutions.json | 20 +-- 6 files changed, 279 insertions(+), 68 deletions(-) create mode 100644 src/components/Form/fields/FieldRorAutosuggest.stories.js create mode 100644 src/components/Form/fields/FieldRorAutosuggest.vue diff --git a/src/components/Form/fields/Autosuggest.vue b/src/components/Form/fields/Autosuggest.vue index 72b75907..36fc5790 100644 --- a/src/components/Form/fields/Autosuggest.vue +++ b/src/components/Form/fields/Autosuggest.vue @@ -18,7 +18,7 @@ > - {{ deselectLabel.replace('{$item}', item.label) }} + {{ deselectLabel?.replace('{$item}', item.label) }} @@ -31,48 +31,72 @@ @update:model-value="selectSuggestion" >
  • - {{ localInputValue }} + + {{ t('common.loading') }}
  • - - {{ suggestion.label }} + {{ localInputValue }}
  • +
    @@ -86,6 +110,7 @@ import { } from '@headlessui/vue'; import PkpBadge from '@/components/Badge/Badge.vue'; import Icon from '@/components/Icon/Icon.vue'; +import Spinner from '@/components/Spinner/Spinner.vue'; const slots = useSlots(); @@ -94,10 +119,6 @@ const props = defineProps({ type: String, required: true, }, - inputProps: { - type: Object, - required: true, - }, suggestions: { type: Array, default: () => [], @@ -122,36 +143,54 @@ const props = defineProps({ type: String, default: () => '', }, - isFocused: { + isLoading: { type: Boolean, default: () => false, }, + inputDescribedByIds: { + type: String, + required: true, + }, + inputControlId: { + type: String, + required: true, + }, + inputName: { + type: String, + default: () => 'autosuggest', + }, }); +/** + * Props to pass to the input field + */ +const inputProps = { + 'aria-describedby': props.inputDescribedByIds, + class: 'pkpAutosuggest__input', + id: props.inputControlId, + name: props.inputName, + disabled: props.isDisabled, +}; + const emit = defineEmits([ 'update:inputValue', - 'update:isFocused', + 'focus-changed', 'select-suggestion', 'deselect', ]); const allowCustom = inject('allowCustom', false); const localInputValue = ref(''); -const localIsFocused = ref(props.isFocused); +const isFocused = ref(false); 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 handleFocus(state) { + isFocused.value = state; + emit('focus-changed', state); } function selectSuggestion(suggestion) { @@ -161,4 +200,6 @@ function selectSuggestion(suggestion) { function deselect(item) { emit('deselect', item); } + +defineExpose({handleFocus}); diff --git a/src/components/Form/fields/FieldBaseAutosuggest.stories.js b/src/components/Form/fields/FieldBaseAutosuggest.stories.js index 453ddd11..b5abd178 100644 --- a/src/components/Form/fields/FieldBaseAutosuggest.stories.js +++ b/src/components/Form/fields/FieldBaseAutosuggest.stories.js @@ -112,12 +112,12 @@ const RORExample = { ); const suggestions = items - .filter((u) => u.fullName.match(regex)) + .filter((u) => u.name.match(regex)) .filter((u) => !this.currentValue.includes(u.id)) .map((u) => { return { value: u.id, - label: u.fullName, + label: u.name, hasSlot: u.ror, }; }); diff --git a/src/components/Form/fields/FieldBaseAutosuggest.vue b/src/components/Form/fields/FieldBaseAutosuggest.vue index 086a1f50..4ac7d32a 100644 --- a/src/components/Form/fields/FieldBaseAutosuggest.vue +++ b/src/components/Form/fields/FieldBaseAutosuggest.vue @@ -45,7 +45,7 @@ :class="{ 'pkpAutosuggest__inputWrapper--multilingual': isMultilingual && locales.length > 1, - 'pkpAutosuggest__inputWrapper--focus': isFocused, + 'pkpAutosuggest__inputWrapper--focus': inputFocused, }" @click="setFocusToInput" > @@ -83,11 +83,11 @@