Skip to content

Commit

Permalink
feat: add inputAutofocus props
Browse files Browse the repository at this point in the history
  • Loading branch information
caro3801 committed Aug 27, 2024
1 parent d3e8c6c commit a51ecc7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
20 changes: 20 additions & 0 deletions .storybook/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ export const entityCategoriesArgType = {
control: 'select',
options: ENTITY_CATEGORIES
}
export const inputTypeArgType = {
control: 'select',
options: [
'text',
'number',
'email',
'password',
'search',
'url',
'tel',
'date',
'time',
'range',
'color',
'datetime',
'datetime-local',
'month',
'week'
]
}
29 changes: 26 additions & 3 deletions src/components/AppModal/AppModalPrompt.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script setup>
import { useAttrs, useSlots } from 'vue'
import { nextTick, ref, useAttrs, useSlots, watch } from 'vue'
import AppModal from './AppModal'
const inputValue = defineModel('inputValue', { type: String, required: true })
const modelValue = defineModel({ type: Boolean, required: true })
defineProps({
const props = defineProps({
inputType: {
type: String,
default: 'text'
Expand All @@ -25,8 +25,13 @@ defineProps({
},
inputInvalidFeedback: {
type: String
},
inputAutofocus: {
type: Boolean,
default: false
}
})
const inputModal = ref(null)
// Retrieve all slots passed to AppModalPrompt to pass them to AppModal
const slots = useSlots()
const slots_ = Object.fromEntries(Object.entries(slots).filter(([name]) => name !== 'default'))
Expand All @@ -36,6 +41,18 @@ const emits = defineEmits(['submit'])
const onOk = () => {
emits('submit', { value: inputValue.value })
}
watch(modelValue, (show) => {
if (props.inputAutofocus === true && show === true) {
focusInput()
}
})
const focusInput = () => {
if (inputModal.value) {
nextTick(() => {
inputModal.value.focus()
})
}
}
</script>

<template>
Expand All @@ -53,7 +70,13 @@ const onOk = () => {
:invalid-feedback="inputInvalidFeedback"
:state="inputState"
>
<b-form-input id="input-1" v-model="inputValue" :type="inputType" :placeholder="inputPlaceholder" />
<b-form-input
id="input-1"
ref="inputModal"
v-model="inputValue"
:type="inputType"
:placeholder="inputPlaceholder"
/>
</b-form-group>
<p class="text-secondary-emphasis">
<slot name="description">{{ description }}</slot>
Expand Down
19 changes: 16 additions & 3 deletions src/stories/components/AppModal/AppModalPrompt.stories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { breakpointSizeArgType } from '~storybook/utils'
import { breakpointSizeArgType, inputTypeArgType } from '~storybook/utils'
import AppModalPrompt from '@/components/AppModal/AppModalPrompt'
import ImageModeSource from '@/components/ImageMode/ImageModeSource'
import image from '@/assets/images/illustrations/app-modal-default-light.svg'
Expand All @@ -9,13 +9,15 @@ export default {
tags: ['autodocs'],
component: AppModalPrompt,
argTypes: {
size: breakpointSizeArgType
size: breakpointSizeArgType,
inputType: inputTypeArgType
},
args: {
image,
imageDark,
modelValue: false,
inputValue: 'test'
inputValue: 'test',
inputAutofocus: false
},
render(args) {
return {
Expand Down Expand Up @@ -54,6 +56,17 @@ export const Default = {
description: 'You will find your saved searches in the left menu in Explore > Saved Searches'
}
}
export const Autofocus = {
args: {
title: 'Name your saved search',
okTitle: 'Save search',
cancelTitle: 'Cancel',
inputValue: '',
inputAutofocus: true,
inputPlaceholder: 'Type here',
description: 'You will find your saved searches in the left menu in Explore > Saved Searches'
}
}
export const InputError = {
args: {
title: 'Name your alert',
Expand Down

0 comments on commit a51ecc7

Please sign in to comment.