Dropdown: add disabled property (#21235)

pull/20282/head^2
SHIODA Masaharu 2021-08-19 18:40:59 +09:00 committed by GitHub
parent eeb9a93e86
commit c50a0bf978
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 73 additions and 17 deletions

View File

@ -279,6 +279,7 @@ Besides default size, Dropdown component provides three additional sizes for you
| show-timeout | Delay time before show a dropdown (only works when trigger is `hover`) | number | — | 250 | | show-timeout | Delay time before show a dropdown (only works when trigger is `hover`) | number | — | 250 |
| hide-timeout | Delay time before hide a dropdown (only works when trigger is `hover`) | number | — | 150 | | hide-timeout | Delay time before hide a dropdown (only works when trigger is `hover`) | number | — | 150 |
| tabindex | [tabindex](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex) of Dropdown | number | — | 0 | | tabindex | [tabindex](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex) of Dropdown | number | — | 0 |
| disabled | whether the Dropdown is disabled | boolean | — | false |
### Dropdown Slots ### Dropdown Slots

View File

@ -281,6 +281,7 @@ Además del tamaño predeterminado, el componente Dropdown proporciona tres tama
| show-timeout | Tiempo de retardo antes de mostrar un dropdown (solamente trabaja cuando se dispara `hover`) | number | — | 250 | | show-timeout | Tiempo de retardo antes de mostrar un dropdown (solamente trabaja cuando se dispara `hover`) | number | — | 250 |
| hide-timeout | Tiempo de retardo antes de ocultar un dropdown (solamente trabaja cuando se dispara `hover`) | number | — | 150 | | hide-timeout | Tiempo de retardo antes de ocultar un dropdown (solamente trabaja cuando se dispara `hover`) | number | — | 150 |
| tabindex | [tabindex](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex) de Dropdown | number | — | 0 | | tabindex | [tabindex](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex) de Dropdown | number | — | 0 |
| disabled | si el desplegable está desactivado | boolean | — | false |
### Dropdown Slots ### Dropdown Slots

View File

@ -281,6 +281,7 @@ En plus de la taille par défaut, le composant Dropdown propose trois autres tai
| show-timeout | Délai avant d'afficher le menu (ne marche que si `trigger` est `hover`) | number | — | 250 | | show-timeout | Délai avant d'afficher le menu (ne marche que si `trigger` est `hover`) | number | — | 250 |
| hide-timeout | Délai avant de cacher le menu (ne marche que si `trigger` est `hover`) | number | — | 150 | | hide-timeout | Délai avant de cacher le menu (ne marche que si `trigger` est `hover`) | number | — | 150 |
| tabindex | [tabindex](https://developer.mozilla.org/fr/docs/Web/HTML/Attributs_universels/tabindex) de Dropdown | number | — | 0 | | tabindex | [tabindex](https://developer.mozilla.org/fr/docs/Web/HTML/Attributs_universels/tabindex) de Dropdown | number | — | 0 |
| disabled | Si le Dropdown est désactivé | boolean | — | false |
### Dropdown Slots ### Dropdown Slots

View File

@ -283,6 +283,7 @@ Dropdown 组件提供除了默认值以外的三种尺寸,可以在不同场
| show-timeout | 展开下拉菜单的延时(仅在 trigger 为 hover 时有效)| number | — | 250 | | show-timeout | 展开下拉菜单的延时(仅在 trigger 为 hover 时有效)| number | — | 250 |
| hide-timeout | 收起下拉菜单的延时(仅在 trigger 为 hover 时有效)| number | — | 150 | | hide-timeout | 收起下拉菜单的延时(仅在 trigger 为 hover 时有效)| number | — | 150 |
| tabindex | Dropdown 组件的 [tabindex](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex) | number | — | 0 | | tabindex | Dropdown 组件的 [tabindex](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex) | number | — | 0 |
| disabled | 是否禁用 | boolean | — | false |
### Dropdown Slots ### Dropdown Slots

View File

@ -59,6 +59,10 @@
tabindex: { tabindex: {
type: Number, type: Number,
default: 0 default: 0
},
disabled: {
type: Boolean,
default: false
} }
}, },
@ -111,14 +115,14 @@
}; };
}, },
show() { show() {
if (this.triggerElm.disabled) return; if (this.disabled) return;
clearTimeout(this.timeout); clearTimeout(this.timeout);
this.timeout = setTimeout(() => { this.timeout = setTimeout(() => {
this.visible = true; this.visible = true;
}, this.trigger === 'click' ? 0 : this.showTimeout); }, this.trigger === 'click' ? 0 : this.showTimeout);
}, },
hide() { hide() {
if (this.triggerElm.disabled) return; if (this.disabled) return;
this.removeTabindex(); this.removeTabindex();
if (this.tabindex >= 0) { if (this.tabindex >= 0) {
this.resetTabindex(this.triggerElm); this.resetTabindex(this.triggerElm);
@ -129,7 +133,7 @@
}, this.trigger === 'click' ? 0 : this.hideTimeout); }, this.trigger === 'click' ? 0 : this.hideTimeout);
}, },
handleClick() { handleClick() {
if (this.triggerElm.disabled) return; if (this.disabled) return;
if (this.visible) { if (this.visible) {
this.hide(); this.hide();
} else { } else {
@ -250,28 +254,38 @@
}, },
render(h) { render(h) {
let { hide, splitButton, type, dropdownSize } = this; let { hide, splitButton, type, dropdownSize, disabled } = this;
const handleMainButtonClick = (event) => { const handleMainButtonClick = (event) => {
this.$emit('click', event); this.$emit('click', event);
hide(); hide();
}; };
let triggerElm = !splitButton let triggerElm = null;
? this.$slots.default if (splitButton) {
: (<el-button-group> triggerElm = <el-button-group>
<el-button type={type} size={dropdownSize} nativeOn-click={handleMainButtonClick}> <el-button type={type} size={dropdownSize} nativeOn-click={handleMainButtonClick} disabled={disabled}>
{this.$slots.default} {this.$slots.default}
</el-button> </el-button>
<el-button ref="trigger" type={type} size={dropdownSize} class="el-dropdown__caret-button"> <el-button ref="trigger" type={type} size={dropdownSize} class="el-dropdown__caret-button" disabled={disabled}>
<i class="el-dropdown__icon el-icon-arrow-down"></i> <i class="el-dropdown__icon el-icon-arrow-down"></i>
</el-button> </el-button>
</el-button-group>); </el-button-group>;
} else {
triggerElm = this.$slots.default;
const vnodeData = triggerElm[0].data || {};
let { attrs = {} } = vnodeData;
if (disabled && !attrs.disabled) {
attrs.disabled = true;
vnodeData.attrs = attrs;
}
}
const menuElm = disabled ? null : this.$slots.dropdown;
return ( return (
<div class="el-dropdown" v-clickoutside={hide}> <div class="el-dropdown" v-clickoutside={hide} aria-disabled={disabled}>
{triggerElm} {triggerElm}
{this.$slots.dropdown} {menuElm}
</div> </div>
); );
} }

View File

@ -224,10 +224,12 @@
margin-right: -1px; margin-right: -1px;
} }
&:hover, &:not(.is-disabled) {
&:focus, &:hover,
&:active { &:focus,
z-index: 1; &:active {
z-index: 1;
}
} }
@include when(active) { @include when(active) {

View File

@ -40,7 +40,7 @@
} }
&:hover { &:hover {
&::before { &:not(.is-disabled)::before {
top: 0; top: 0;
bottom: 0; bottom: 0;
} }
@ -60,6 +60,11 @@
outline-width: 0; outline-width: 0;
} }
} }
[disabled] {
cursor: not-allowed;
color: $--font-color-disabled-base;
}
} }
@include b(dropdown-menu) { @include b(dropdown-menu) {

View File

@ -280,4 +280,32 @@ describe('Dropdown', () => {
}, 100); }, 100);
}, 300); }, 300);
}); });
it('dropdown disabled', done => {
vm = createVue({
template: `
<el-dropdown ref="dropdown" disabled>
<span class="el-dropdown-link">
下拉菜单<i class="el-icon-caret-bottom el-icon-right"></i>
</span>
<el-dropdown-menu slot="dropdown" class="dropdown-test-creat">
<el-dropdown-item>黄金糕</el-dropdown-item>
<el-dropdown-item>狮子头</el-dropdown-item>
<el-dropdown-item>螺蛳粉</el-dropdown-item>
<el-dropdown-item>双皮奶</el-dropdown-item>
<el-dropdown-item>蚵仔煎</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
`
}, true);
let dropdown = vm.$refs.dropdown;
let dropdownElm = dropdown.$el;
let triggerElm = dropdownElm.children[0];
triggerEvent(triggerElm, 'mouseenter');
setTimeout(() => {
expect(dropdownElm.querySelectorAll('.el-dropdown-link[disabled]').length).equal(1);
expect(dropdown.visible).to.be.false;
done();
}, 10);
});
}); });

3
types/dropdown.d.ts vendored
View File

@ -32,4 +32,7 @@ export declare class ElDropdown extends ElementUIComponent {
/** Dropdown tabindex */ /** Dropdown tabindex */
tabindex: number tabindex: number
/** Whether Dropdown is disabled */
disabled: boolean
} }