Skip to content

Commit

Permalink
feat(DropdownMenu): add disabled for option (#12785)
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Apr 12, 2024
1 parent c857d1f commit cecfc45
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 8 deletions.
15 changes: 12 additions & 3 deletions packages/vant/src/dropdown-item/DropdownItem.tsx
Expand Up @@ -126,9 +126,14 @@ export default defineComponent({

const renderOption = (option: DropdownItemOption) => {
const { activeColor } = parent.props;
const { disabled } = option;
const active = option.value === props.modelValue;

const onClick = () => {
if (disabled) {
return;
}

state.showPopup = false;

if (option.value !== props.modelValue) {
Expand All @@ -140,7 +145,11 @@ export default defineComponent({
const renderIcon = () => {
if (active) {
return (
<Icon class={bem('icon')} color={activeColor} name="success" />
<Icon
class={bem('icon')}
color={disabled ? undefined : activeColor}
name="success"
/>
);
}
};
Expand All @@ -152,10 +161,10 @@ export default defineComponent({
key={String(option.value)}
icon={option.icon}
title={option.text}
class={bem('option', { active })}
class={bem('option', { active, disabled })}
style={{ color: active ? activeColor : '' }}
tabindex={active ? 0 : -1}
clickable
clickable={!disabled}
onClick={onClick}
/>
);
Expand Down
8 changes: 8 additions & 0 deletions packages/vant/src/dropdown-item/index.less
Expand Up @@ -25,6 +25,14 @@
color: var(--van-dropdown-menu-option-active-color);
}
}

&--disabled {
color: var(--van-dropdown-menu-option-disabled-color);

.van-dropdown-item__icon {
color: var(--van-dropdown-menu-option-disabled-color);
}
}
}

&--up {
Expand Down
1 change: 1 addition & 0 deletions packages/vant/src/dropdown-item/types.ts
Expand Up @@ -5,6 +5,7 @@ import type { Numeric } from '../utils';
export type DropdownItemOptionValue = Numeric | boolean;

export type DropdownItemOption = {
disabled?: boolean;
text: string;
icon?: string;
value: DropdownItemOptionValue;
Expand Down
12 changes: 7 additions & 5 deletions packages/vant/src/dropdown-menu/README.md
Expand Up @@ -249,11 +249,12 @@ dropdownItemRef.value?.toggle();

### Data Structure of Option

| Key | Description | Type |
| ----- | ----------- | ----------------------------- |
| text | Text | _string_ |
| value | Value | _number \| string \| boolean_ |
| icon | Left icon | _string_ |
| Key | Description | Type |
| -------- | ------------------------- | ----------------------------- |
| text | Text | _string_ |
| value | Value | _number \| string \| boolean_ |
| disabled | Whether to disable option | _boolean_ |
| icon | Left icon | _string_ |

## Theming

Expand All @@ -273,5 +274,6 @@ The component provides the following CSS variables, which can be used to customi
| --van-dropdown-menu-title-padding | _0 var(--van-padding-xs)_ | - |
| --van-dropdown-menu-title-line-height | _var(--van-line-height-lg)_ | - |
| --van-dropdown-menu-option-active-color | _var(--van-primary-color)_ | - |
| --van-dropdown-menu-option-disabled-color | _var(--van-text-color-3)_ | - |
| --van-dropdown-menu-content-max-height | _80%_ | - |
| --van-dropdown-item-z-index | _10_ | - |
2 changes: 2 additions & 0 deletions packages/vant/src/dropdown-menu/README.zh-CN.md
Expand Up @@ -257,6 +257,7 @@ dropdownItemRef.value?.toggle();
| --- | --- | --- |
| text | 文字 | _string_ |
| value | 标识符 | _number \| string \| boolean_ |
| disabled | 是否禁用选项 | _boolean_ |
| icon | 左侧图标名称或图片链接,等同于 Icon 组件的 [name 属性](#/zh-CN/icon#props) | _string_ |

## 主题定制
Expand All @@ -277,6 +278,7 @@ dropdownItemRef.value?.toggle();
| --van-dropdown-menu-title-padding | _0 var(--van-padding-xs)_ | - |
| --van-dropdown-menu-title-line-height | _var(--van-line-height-lg)_ | - |
| --van-dropdown-menu-option-active-color | _var(--van-primary-color)_ | - |
| --van-dropdown-menu-option-disabled-color | _var(--van-text-color-3)_ | - |
| --van-dropdown-menu-content-max-height | _80%_ | - |
| --van-dropdown-item-z-index | _10_ | - |

Expand Down
1 change: 1 addition & 0 deletions packages/vant/src/dropdown-menu/index.less
Expand Up @@ -10,6 +10,7 @@
--van-dropdown-menu-title-padding: 0 var(--van-padding-xs);
--van-dropdown-menu-title-line-height: var(--van-line-height-lg);
--van-dropdown-menu-option-active-color: var(--van-primary-color);
--van-dropdown-menu-option-disabled-color: var(--van-text-color-3);
--van-dropdown-menu-content-max-height: 80%;
}

Expand Down
40 changes: 40 additions & 0 deletions packages/vant/src/dropdown-menu/test/index.spec.tsx
Expand Up @@ -367,3 +367,43 @@ test('auto-locate prop', async () => {

vi.doUnmock('../../utils/dom');
});

test('disable dropdown option', async () => {
const onChange = vi.fn();
const optionsRef = ref([
{ text: 'A', value: 0 },
{ text: 'B', value: 1, disabled: true },
{ text: 'C', value: 2 },
]);
const wrapper = mount({
setup() {
return () => (
<DropdownMenu>
<DropdownItem
modelValue={0}
options={optionsRef.value}
onChange={onChange}
/>
</DropdownMenu>
);
},
});

await later();
const title = wrapper.find('.van-dropdown-menu__title');
await title.trigger('click');

const options = wrapper.findAll('.van-dropdown-item .van-cell');

await options[1].trigger('click');
expect(onChange).not.toHaveBeenCalled();

await options[2].trigger('click');
expect(onChange).toHaveBeenCalledTimes(1);

optionsRef.value[1].disabled = false;

await later();
await options[1].trigger('click');
expect(onChange).toHaveBeenCalledTimes(2);
});
1 change: 1 addition & 0 deletions packages/vant/src/dropdown-menu/types.ts
Expand Up @@ -30,5 +30,6 @@ export type DropdownMenuThemeVars = {
dropdownMenuTitlePadding?: string;
dropdownMenuTitleLineHeight?: number | string;
dropdownMenuOptionActiveColor?: string;
dropdownMenuOptionDisabledColor?: string;
dropdownMenuContentMaxHeight?: string;
};

0 comments on commit cecfc45

Please sign in to comment.