feat(ActionSheet): add option slot (#10065)

This commit is contained in:
neverland 2021-12-19 21:13:16 +08:00 committed by GitHub
parent f6a835721c
commit 5f48560142
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 24 deletions

View File

@ -94,18 +94,23 @@ export default defineComponent({
} }
}; };
const renderOption = (item: ActionSheetAction, index: number) => { const renderOptionContent = (action: ActionSheetAction, index: number) => {
const { name, color, subname, loading, callback, disabled, className } = if (action.loading) {
item; return <Loading class={bem('loading-icon')} />;
}
const Content = loading ? ( if (slots.option) {
<Loading class={bem('loading-icon')} /> return slots.option({ action, index });
) : ( }
[
<span class={bem('name')}>{name}</span>, return [
subname && <div class={bem('subname')}>{subname}</div>, <span class={bem('name')}>{action.name}</span>,
] action.subname && <div class={bem('subname')}>{action.subname}</div>,
); ];
};
const renderOption = (action: ActionSheetAction, index: number) => {
const { color, loading, callback, disabled, className } = action;
const onClick = () => { const onClick = () => {
if (disabled || loading) { if (disabled || loading) {
@ -113,14 +118,14 @@ export default defineComponent({
} }
if (callback) { if (callback) {
callback(item); callback(action);
} }
if (props.closeOnClickAction) { if (props.closeOnClickAction) {
updateShow(false); updateShow(false);
} }
nextTick(() => emit('select', item, index)); nextTick(() => emit('select', action, index));
}; };
return ( return (
@ -130,7 +135,7 @@ export default defineComponent({
class={[bem('item', { loading, disabled }), className]} class={[bem('item', { loading, disabled }), className]}
onClick={onClick} onClick={onClick}
> >
{Content} {renderOptionContent(action, index)}
</button> </button>
); );
}; };

View File

@ -218,11 +218,12 @@ export default {
### Slots ### Slots
| Name | Description | | Name | Description | SlotProps |
| ---------------- | ------------------------------------ | | --- | --- | --- |
| default | Custom content | | default | Custom content |
| description | Custom description above the options | | description | Custom description above the options |
| cancel `v3.0.10` | Custom the content of cancel button | | cancel `v3.0.10` | Custom the content of cancel button |
| option `v3.3.8` | Custom the content of option | _{ action: Action, index: number }_ |
### Types ### Types

View File

@ -230,11 +230,12 @@ export default {
### Slots ### Slots
| 名称 | 说明 | | 名称 | 说明 | 参数 |
| ---------------- | -------------------- | | --- | --- | --- |
| default | 自定义面板的展示内容 | | default | 自定义面板的展示内容 | - |
| description | 自定义描述文案 | | description | 自定义描述文案 | - |
| cancel `v3.0.10` | 自定义取消按钮内容 | | cancel `v3.0.10` | 自定义取消按钮内容 | - |
| option `v3.3.8` | 自定义选项内容 | _{ action: Action, index: number }_ |
### 类型定义 ### 类型定义

View File

@ -44,6 +44,14 @@ exports[`should render description slot when match snapshot 1`] = `
</div> </div>
`; `;
exports[`should render option slot correctly 1`] = `
<button type="button"
class="van-action-sheet__item"
>
name: Option, index: 0
</button>
`;
exports[`should render subname correctly 1`] = ` exports[`should render subname correctly 1`] = `
<button type="button" <button type="button"
class="van-action-sheet__item" class="van-action-sheet__item"

View File

@ -261,3 +261,17 @@ test('should allow to control safe-area with safe-area-inset-bottom prop', async
'van-safe-area-bottom' 'van-safe-area-bottom'
); );
}); });
test('should render option slot correctly', () => {
const wrapper = mount(ActionSheet, {
props: {
show: true,
actions: [{ name: 'Option' }],
},
slots: {
option: ({ action, index }) => `name: ${action.name}, index: ${index}`,
},
});
expect(wrapper.find('.van-action-sheet__item').html()).toMatchSnapshot();
});