Skip to content

Commit

Permalink
feat(SubmitBar): add placeholder prop (#10725)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jun 18, 2022
1 parent a1c79f4 commit 7ca4db9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 27 deletions.
1 change: 0 additions & 1 deletion packages/vant-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"@types/fs-extra": "^9.0.13",
"@types/less": "^3.0.3",
"@types/markdown-it": "^12.2.3",
"@types/react": "^18",
"@jest/types": "^27",
"vue": "^3.2.27",
"react": "^18",
Expand Down
1 change: 1 addition & 0 deletions packages/vant/src/submit-bar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default {
| disabled | Whether to disable button | _boolean_ | `false` |
| loading | Whether to show loading icon | _boolean_ | `false` |
| safe-area-inset-bottom | Whether to enable bottom safe area adaptation | _boolean_ | `true` |
| placeholder `v3.5.1` | Whether to generate a placeholder element | _boolean_ | `false` |

### Events

Expand Down
1 change: 1 addition & 0 deletions packages/vant/src/submit-bar/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default {
| disabled | 是否禁用按钮 | _boolean_ | `false` |
| loading | 是否显示将按钮显示为加载中状态 | _boolean_ | `false` |
| safe-area-inset-bottom | 是否开启[底部安全区适配](#/zh-CN/advanced-usage#di-bu-an-quan-qu-gua-pei) | _boolean_ | `true` |
| placeholder `v3.5.1` | 是否在标签位置生成一个等高的占位元素 | _boolean_ | `false` |

### Events

Expand Down
22 changes: 20 additions & 2 deletions packages/vant/src/submit-bar/SubmitBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { defineComponent, type PropType, type ExtractPropTypes } from 'vue';
import {
ref,
defineComponent,
type PropType,
type ExtractPropTypes,
} from 'vue';
import {
truthProp,
makeStringProp,
Expand All @@ -9,6 +14,7 @@ import {
// Components
import { Icon } from '../icon';
import { Button, ButtonType } from '../button';
import { usePlaceholder } from '../composables/use-placeholder';

const [name, bem, t] = createNamespace('submit-bar');

Expand All @@ -27,6 +33,7 @@ const submitBarProps = {
buttonType: makeStringProp<ButtonType>('danger'),
buttonColor: String,
suffixLabel: String,
placeholder: Boolean,
decimalLength: makeNumericProp(2),
safeAreaInsetBottom: truthProp,
};
Expand All @@ -41,6 +48,9 @@ export default defineComponent({
emits: ['submit'],

setup(props, { emit, slots }) {
const root = ref<HTMLElement>();
const renderPlaceholder = usePlaceholder(root, bem);

const renderText = () => {
const { price, label, currency, textAlign, suffixLabel, decimalLength } =
props;
Expand Down Expand Up @@ -99,8 +109,9 @@ export default defineComponent({
);
};

return () => (
const renderSubmitBar = () => (
<div
ref={root}
class={[bem(), { 'van-safe-area-bottom': props.safeAreaInsetBottom }]}
>
{slots.top?.()}
Expand All @@ -112,5 +123,12 @@ export default defineComponent({
</div>
</div>
);

return () => {
if (props.placeholder) {
return renderPlaceholder(renderSubmitBar);
}
return renderSubmitBar();
};
},
});
17 changes: 17 additions & 0 deletions packages/vant/src/submit-bar/test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ exports[`should render disabled submit button correctly 1`] = `
</button>
`;

exports[`should render placeholder element when using placeholder prop 1`] = `
<div class="van-submit-bar__placeholder"
style="height: 50px;"
>
<div class="van-submit-bar van-safe-area-bottom">
<div class="van-submit-bar__bar">
<button type="button"
class="van-button van-button--danger van-button--normal van-button--round van-submit-bar__button van-submit-bar__button--danger"
>
<div class="van-button__content">
</div>
</button>
</div>
</div>
</div>
`;

exports[`should render suffix-label correctly 1`] = `
<div class="van-submit-bar__text">
<span>
Expand Down
15 changes: 14 additions & 1 deletion packages/vant/src/submit-bar/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SubmitBar } from '..';
import { mount } from '../../../test';
import { later, mockGetBoundingClientRect, mount } from '../../../test';

test('should emit submit event when submit button is clicked', () => {
const wrapper = mount(SubmitBar);
Expand Down Expand Up @@ -107,3 +107,16 @@ test('should render button slot correctly', () => {
});
expect(wrapper.html()).toMatchSnapshot();
});

test('should render placeholder element when using placeholder prop', async () => {
const restore = mockGetBoundingClientRect({ height: 50 });
const wrapper = mount(SubmitBar, {
props: {
placeholder: true,
},
});

await later();
expect(wrapper.html()).toMatchSnapshot();
restore();
});
27 changes: 4 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7ca4db9

Please sign in to comment.