Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components): [message-box] add autofocus attribute #8445

Merged
merged 5 commits into from Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/.vitepress/vitepress/styles/css-vars.scss
Expand Up @@ -55,7 +55,7 @@
--content-min-width: 16rem;
--content-max-width: 48rem;

--nav-z-index: 20;
--nav-z-index: 10;
--sub-nav-z-index: 10;
--sidebar-z-index: 40;
--overlay-z-index: 30;
Expand Down
1 change: 1 addition & 0 deletions docs/en-US/component/message-box.md
Expand Up @@ -154,6 +154,7 @@ The corresponding methods are: `ElMessageBox`, `ElMessageBox.alert`, `ElMessageB

| Attribute | Description | Type | Accepted Values | Default |
| ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- | ------------------------------------------------ |
| autofocus | auto focus when open MessageBox | boolean | — | true |
| title | title of the MessageBox | string | — | — |
| message | content of the MessageBox | string | — | — |
| dangerouslyUseHTMLString | whether `message` is treated as HTML string | boolean | — | false |
Expand Down
2 changes: 2 additions & 0 deletions docs/examples/message-box/alert.vue
Expand Up @@ -8,6 +8,8 @@ import type { Action } from 'element-plus'

const open = () => {
ElMessageBox.alert('This is a message', 'Title', {
// if you want to disable its autofocus
// autofocus: false,
confirmButtonText: 'OK',
callback: (action: Action) => {
ElMessage({
Expand Down
13 changes: 13 additions & 0 deletions packages/components/message-box/__tests__/message-box.test.ts
Expand Up @@ -144,6 +144,19 @@ describe('MessageBox', () => {
expect(msgbox).toBe(null)
})

test('autofocus', async () => {
MessageBox.alert('这是一段内容', {
autofocus: false,
title: '标题名称',
})
await rAF()
const btnElm = document.querySelector(
'.el-message-box__btns .el-button--primary'
)
const haveFocus = btnElm.isSameNode(document.activeElement)
expect(haveFocus).toBe(false)
})

test('prompt', async () => {
MessageBox.prompt('这是一段内容', {
title: '标题名称',
Expand Down
14 changes: 12 additions & 2 deletions packages/components/message-box/src/index.vue
Expand Up @@ -259,6 +259,8 @@ export default defineComponent({
const { nextZIndex } = useZIndex()
// s represents state
const state = reactive<MessageBoxState>({
// autofocus element when open message-box
autofocus: true,
beforeClose: null,
callback: null,
cancelButtonText: '',
Expand Down Expand Up @@ -337,15 +339,23 @@ export default defineComponent({
(val) => {
if (val) {
if (props.boxType !== 'prompt') {
focusStartRef.value = confirmRef.value?.$el ?? rootRef.value
if (state.autofocus) {
focusStartRef.value = confirmRef.value?.$el ?? rootRef.value
} else {
focusStartRef.value = rootRef.value
}
}
state.zIndex = nextZIndex()
}
if (props.boxType !== 'prompt') return
if (val) {
nextTick().then(() => {
if (inputRef.value && inputRef.value.$el) {
focusStartRef.value = getInputElement() ?? rootRef.value
if (state.autofocus) {
focusStartRef.value = getInputElement() ?? rootRef.value
} else {
focusStartRef.value = rootRef.value
}
}
})
} else {
Expand Down
6 changes: 6 additions & 0 deletions packages/components/message-box/src/message-box.type.ts
Expand Up @@ -16,6 +16,7 @@ export interface MessageBoxInputValidator {
}

export declare interface MessageBoxState {
autofocus: boolean
title: string
message: string
type: MessageType
Expand Down Expand Up @@ -62,6 +63,11 @@ export type Callback =

/** Options used in MessageBox */
export interface ElMessageBoxOptions {
/**
* auto focus when open message-box
*/
autofocus?: boolean
YunYouJun marked this conversation as resolved.
Show resolved Hide resolved

/** Callback before MessageBox closes, and it will prevent MessageBox from closing */
beforeClose?: (
action: Action,
Expand Down