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

Commit 05b8ffd

Browse files
feat(b-form-tags) added focus & blur events (closes #5947) (#6390)
* adds focus and blur events to b-form-tags * fixed clicks near margin of wrapper not focusing input * fixed merging error * Update form-tags.js * Update form-tags.js * Update _form-tags.scss * Update form-tags.js * Update form-tags.js * Update form-tags.js Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
1 parent 9f045d4 commit 05b8ffd

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

src/components/form-tags/form-tags.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@ import { RX_SPACES } from '../../constants/regex'
1818
import { SLOT_NAME_DEFAULT, SLOT_NAME_ADD_BUTTON_TEXT } from '../../constants/slots'
1919
import { arrayIncludes, concat } from '../../utils/array'
2020
import { cssEscape } from '../../utils/css-escape'
21-
import {
22-
attemptBlur,
23-
attemptFocus,
24-
closest,
25-
isActiveElement,
26-
matches,
27-
requestAF,
28-
select
29-
} from '../../utils/dom'
21+
import { attemptBlur, attemptFocus, closest, matches, requestAF, select } from '../../utils/dom'
3022
import { eventOn, eventOff, stopEvent } from '../../utils/events'
3123
import { identity } from '../../utils/identity'
3224
import { isEvent, isNumber, isString } from '../../utils/inspect'
@@ -39,6 +31,7 @@ import { formControlMixin, props as formControlProps } from '../../mixins/form-c
3931
import { formSizeMixin, props as formSizeProps } from '../../mixins/form-size'
4032
import { formStateMixin, props as formStateProps } from '../../mixins/form-state'
4133
import { idMixin, props as idProps } from '../../mixins/id'
34+
import { listenersMixin } from '../../mixins/listeners'
4235
import { normalizeSlotMixin } from '../../mixins/normalize-slot'
4336
import { BButton } from '../button/button'
4437
import { BFormInvalidFeedback } from '../form/form-invalid-feedback'
@@ -144,6 +137,7 @@ const props = makePropsConfigurable(
144137
export const BFormTags = /*#__PURE__*/ Vue.extend({
145138
name: NAME_FORM_TAGS,
146139
mixins: [
140+
listenersMixin,
147141
idMixin,
148142
modelMixin,
149143
formControlMixin,
@@ -186,6 +180,7 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
186180
},
187181
computedInputHandlers() {
188182
return {
183+
...this.bvListeners,
189184
input: this.onInputInput,
190185
change: this.onInputChange,
191186
keydown: this.onInputKeydown,
@@ -338,10 +333,6 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
338333
// will prevent the tag from being removed (i.e. confirmation)
339334
// Or emit cancelable `BvEvent`
340335
this.tags = this.tags.filter(t => t !== tag)
341-
// Return focus to the input (if possible)
342-
this.$nextTick(() => {
343-
this.focus()
344-
})
345336
},
346337
reset() {
347338
this.newTag = ''
@@ -413,13 +404,8 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
413404
},
414405
// --- Wrapper event handlers ---
415406
onClick(event) {
416-
const ignoreFocusSelector = this.computeIgnoreInputFocusSelector
417-
const { target } = event
418-
if (
419-
!this.disabled &&
420-
!isActiveElement(target) &&
421-
(!ignoreFocusSelector || !closest(ignoreFocusSelector, target, true))
422-
) {
407+
const { computeIgnoreInputFocusSelector: ignoreFocusSelector } = this
408+
if (!ignoreFocusSelector || !closest(ignoreFocusSelector, event.target, true)) {
423409
this.$nextTick(() => {
424410
this.focus()
425411
})
@@ -434,7 +420,7 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
434420
handleAutofocus() {
435421
this.$nextTick(() => {
436422
requestAF(() => {
437-
if (this.autofocus && !this.disabled) {
423+
if (this.autofocus) {
438424
this.focus()
439425
}
440426
})

src/components/form-tags/form-tags.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,4 +842,41 @@ describe('form-tags', () => {
842842

843843
wrapper.destroy()
844844
})
845+
846+
it('emits focus and blur events', async () => {
847+
const onFocus = jest.fn()
848+
const onBlur = jest.fn()
849+
const wrapper = mount(BFormTags, {
850+
propsData: {
851+
value: ['apple', 'orange']
852+
},
853+
listeners: {
854+
focus: onFocus,
855+
blur: onBlur
856+
}
857+
})
858+
859+
expect(onFocus).not.toHaveBeenCalled()
860+
expect(onBlur).not.toHaveBeenCalled()
861+
862+
const $input = wrapper.find('input')
863+
expect(typeof wrapper.vm.$listeners.focus).toBe('function')
864+
865+
$input.trigger('focus')
866+
$input.trigger('focusin')
867+
await waitNT(wrapper.vm)
868+
await waitRAF()
869+
870+
expect(onFocus).toHaveBeenCalled()
871+
expect(onBlur).not.toHaveBeenCalled()
872+
873+
$input.trigger('blur')
874+
$input.trigger('focusout')
875+
await waitNT(wrapper.vm)
876+
await waitRAF()
877+
878+
expect(onBlur).toHaveBeenCalled()
879+
880+
wrapper.destroy()
881+
})
845882
})

0 commit comments

Comments
 (0)