🌐 AI搜索 & 代理 主页
Skip to content

Commit 720cb11

Browse files
authored
feat(modal): add prop for auto focusing one of the built in buttons on shown
1 parent 249ccfa commit 720cb11

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/components/modal/modal.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import BVTransition from '../../utils/bv-transition'
99
import KeyCodes from '../../utils/key-codes'
1010
import observeDom from '../../utils/observe-dom'
1111
import { BTransporterSingle } from '../../utils/transporter'
12+
import { from as arrayFrom } from '../../utils/array'
1213
import { isBrowser } from '../../utils/env'
13-
import { isString } from '../../utils/inspect'
14+
import { isString, isUndefinedOrNull } from '../../utils/inspect'
1415
import { getComponentConfig } from '../../utils/config'
1516
import { stripTags } from '../../utils/html'
1617
import { contains, eventOff, eventOn, isVisible, select, selectAll } from '../../utils/dom'
@@ -255,6 +256,11 @@ export const props = {
255256
static: {
256257
type: Boolean,
257258
default: false
259+
},
260+
autoFocusButton: {
261+
type: String,
262+
default: null,
263+
validator: val => isUndefinedOrNull(val) || arrayIncludes(['ok', 'cancel', 'close'], val)
258264
}
259265
}
260266

@@ -736,11 +742,24 @@ export const BModal = /*#__PURE__*/ Vue.extend({
736742
const activeElement = this.getActiveElement()
737743
// If the modal contains the activeElement, we don't do anything
738744
if (modal && content && !(activeElement && contains(content, activeElement))) {
745+
const ok = this.$refs['ok-button']
746+
const cancel = this.$refs['cancel-button']
747+
const close = this.$refs['close-button']
739748
// Make sure top of modal is showing (if longer than the viewport)
740-
// and focus the modal content wrapper
749+
// and focus the apropriate button or modal content wrapper
750+
const autoFocus = this.autoFocusButton
751+
const el = autoFocus === 'ok' && ok
752+
? ok.$el || ok
753+
: autoFocus === 'cancel' && cancel
754+
? cancel.$el || cancel
755+
: autoFocus === 'close' && close
756+
? close.$el || close
757+
: content
741758
this.$nextTick(() => {
742-
modal.scrollTop = 0
743-
content.focus()
759+
if (el === content) {
760+
modal.scrollTop = 0
761+
}
762+
el.focus()
744763
})
745764
}
746765
}
@@ -777,6 +796,7 @@ export const BModal = /*#__PURE__*/ Vue.extend({
777796
closeButton = h(
778797
BButtonClose,
779798
{
799+
ref: 'close-button',
780800
props: {
781801
disabled: this.isTransitioning,
782802
ariaLabel: this.headerCloseLabel,
@@ -840,6 +860,7 @@ export const BModal = /*#__PURE__*/ Vue.extend({
840860
cancelButton = h(
841861
BButton,
842862
{
863+
ref: 'cancel-button',
843864
props: {
844865
variant: this.cancelVariant,
845866
size: this.buttonSize,
@@ -857,6 +878,7 @@ export const BModal = /*#__PURE__*/ Vue.extend({
857878
const okButton = h(
858879
BButton,
859880
{
881+
ref: 'ok-button',
860882
props: {
861883
variant: this.okVariant,
862884
size: this.buttonSize,

0 commit comments

Comments
 (0)