mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-08-18 02:00:10 +08:00
[improvement] ContactCard: functional (#2696)
This commit is contained in:
parent
1b60e4df80
commit
3e0ac872c8
@ -4,6 +4,8 @@ import Cell from '../cell';
|
|||||||
const [sfc, bem, t] = use('contact-card');
|
const [sfc, bem, t] = use('contact-card');
|
||||||
|
|
||||||
export default sfc({
|
export default sfc({
|
||||||
|
functional: true,
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
tel: String,
|
tel: String,
|
||||||
name: String,
|
name: String,
|
||||||
@ -18,28 +20,27 @@ export default sfc({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
render(h, context, inherit) {
|
||||||
onClick(event) {
|
const { props, listeners } = context;
|
||||||
if (this.editable) {
|
const { type, editable } = props;
|
||||||
this.$emit('click', event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
render(h) {
|
|
||||||
const { type } = this;
|
|
||||||
return (
|
return (
|
||||||
<Cell
|
<Cell
|
||||||
center
|
center
|
||||||
border={false}
|
border={false}
|
||||||
class={bem([type])}
|
class={bem([type])}
|
||||||
isLink={this.editable}
|
isLink={editable}
|
||||||
icon={type === 'edit' ? 'contact' : 'add-square'}
|
icon={type === 'edit' ? 'contact' : 'add-square'}
|
||||||
onClick={this.onClick}
|
onClick={event => {
|
||||||
|
if (editable && listeners.click) {
|
||||||
|
listeners.click(event);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
{...inherit}
|
||||||
>
|
>
|
||||||
{type === 'add'
|
{type === 'add'
|
||||||
? this.addText || t('addText')
|
? props.addText || t('addText')
|
||||||
: [<div>{`${t('name')}:${this.name}`}</div>, <div>{`${t('tel')}:${this.tel}`}</div>]}
|
: [<div>{`${t('name')}:${props.name}`}</div>, <div>{`${t('tel')}:${props.tel}`}</div>]}
|
||||||
</Cell>
|
</Cell>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -10,18 +10,34 @@ const contactInfo = {
|
|||||||
|
|
||||||
describe('ContactCard', () => {
|
describe('ContactCard', () => {
|
||||||
test('click event', () => {
|
test('click event', () => {
|
||||||
|
const click = jest.fn();
|
||||||
const wrapper = mount(ContactCard, {
|
const wrapper = mount(ContactCard, {
|
||||||
propsData: {
|
context: {
|
||||||
editable: false
|
on: {
|
||||||
|
click
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
wrapper.trigger('click');
|
wrapper.trigger('click');
|
||||||
expect(wrapper.emitted('click')).toBeFalsy();
|
expect(click.mock.calls.length).toEqual(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('not editable', () => {
|
||||||
|
const click = jest.fn();
|
||||||
|
const wrapper = mount(ContactCard, {
|
||||||
|
propsData: {
|
||||||
|
editable: false
|
||||||
|
},
|
||||||
|
context: {
|
||||||
|
on: {
|
||||||
|
click
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
wrapper.setProps({ editable: true });
|
|
||||||
wrapper.trigger('click');
|
wrapper.trigger('click');
|
||||||
expect(wrapper.emitted('click')).toBeTruthy();
|
expect(click.mock.calls.length).toEqual(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user