[improvement] AddressList: functional (#2757)

This commit is contained in:
neverland 2019-02-16 08:41:03 +08:00 committed by GitHub
parent 880f574b9a
commit 9aee819c29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 96 deletions

View File

@ -1,46 +1,40 @@
import { use } from '../utils'; import { use } from '../utils';
import { emit, inherit } from '../utils/functional';
import Icon from '../icon'; import Icon from '../icon';
import Cell from '../cell'; import Cell from '../cell';
import Radio from '../radio'; import Radio from '../radio';
const [sfc, bem] = use('address-item'); const [sfc, bem] = use('address-item');
export default sfc({ function AddressItem(h, props, slots, ctx) {
props: { const { disabled, switchable } = props;
data: Object,
disabled: Boolean,
switchable: Boolean
},
methods: { const renderRightIcon = () => (
onSelect() { <Icon
if (this.switchable) { name="edit"
this.$emit('select'); class={bem('edit')}
} onClick={event => {
},
onClickRightIcon(event) {
event.stopPropagation(); event.stopPropagation();
this.$emit('edit'); emit(ctx, 'edit');
}, }}
/>
);
renderRightIcon() { const renderContent = () => {
return <Icon name="edit" class={bem('edit')} onClick={this.onClickRightIcon} />; const { data } = props;
},
renderContent() {
const { data } = this;
const Info = [ const Info = [
<div class={bem('name')}>{`${data.name}${data.tel}`}</div>, <div class={bem('name')}>{`${data.name}${data.tel}`}</div>,
<div class={bem('address')}>{data.address}</div> <div class={bem('address')}>{data.address}</div>
]; ];
return this.disabled ? Info : <Radio name={data.id}>{Info}</Radio>; return props.disabled ? Info : <Radio name={data.id}>{Info}</Radio>;
} };
},
render(h) { const onSelect = () => {
const { disabled, switchable } = this; if (props.switchable) {
emit(ctx, 'select');
}
};
return ( return (
<Cell <Cell
@ -48,11 +42,19 @@ export default sfc({
valueClass={bem('value')} valueClass={bem('value')}
isLink={!disabled && switchable} isLink={!disabled && switchable}
scopedSlots={{ scopedSlots={{
default: this.renderContent, default: renderContent,
'right-icon': this.renderRightIcon 'right-icon': renderRightIcon
}} }}
onClick={this.onSelect} onClick={onSelect}
{...inherit(ctx)}
/> />
); );
} }
});
AddressItem.props = {
data: Object,
disabled: Boolean,
switchable: Boolean
};
export default sfc(AddressItem);

View File

@ -1,12 +1,62 @@
import { use } from '../utils'; import { use } from '../utils';
import { emit, inherit } from '../utils/functional';
import Button from '../button'; import Button from '../button';
import RadioGroup from '../radio-group'; import RadioGroup from '../radio-group';
import AddressItem from './Item'; import AddressItem from './Item';
const [sfc, bem, t] = use('address-list'); const [sfc, bem, t] = use('address-list');
export default sfc({ function AddressList(h, props, slots, ctx) {
props: { const getList = (list, disabled) =>
list.map((item, index) => (
<AddressItem
data={item}
key={item.id}
disabled={disabled}
switchable={props.switchable && !disabled}
onSelect={() => {
emit(ctx, disabled ? 'select-disabled' : 'select', item, index);
}}
onEdit={() => {
emit(ctx, disabled ? 'edit-disabled' : 'edit', item, index);
}}
/>
));
const List = getList(props.list);
const DisabledList = getList(props.disabledList, true);
return (
<div class={bem()} {...inherit(ctx)}>
{slots.top && slots.top()}
<RadioGroup
value={props.value}
onInput={event => {
emit(ctx, 'input', event);
}}
>
{List}
</RadioGroup>
{props.disabledText && (
<div class={bem('disabled-text')}>{props.disabledText}</div>
)}
{DisabledList}
{slots.default && slots.default()}
<Button
square
size="large"
type="danger"
class={bem('add')}
text={props.addButtonText || t('add')}
onClick={() => {
emit(ctx, 'add');
}}
/>
</div>
);
}
AddressList.props = {
list: Array, list: Array,
disabledList: Array, disabledList: Array,
disabledText: String, disabledText: String,
@ -16,48 +66,6 @@ export default sfc({
type: Boolean, type: Boolean,
default: true default: true
} }
}, };
render(h) { export default sfc(AddressList);
const getList = (list, disabled) =>
list.map((item, index) => (
<AddressItem
data={item}
key={item.id}
disabled={disabled}
switchable={this.switchable && !disabled}
onSelect={() => {
this.$emit(disabled ? 'select-disabled' : 'select', item, index);
}}
onEdit={() => {
this.$emit(disabled ? 'edit-disabled' : 'edit', item, index);
}}
/>
));
const List = getList(this.list);
const DisabledList = getList(this.disabledList, true);
return (
<div class={bem()}>
{this.slots('top')}
<RadioGroup value={this.value} onInput={event => this.$emit('input', event)}>
{List}
</RadioGroup>
{this.disabledText && <div class={bem('disabled-text')}>{this.disabledText}</div>}
{DisabledList}
{this.slots()}
<Button
square
size="large"
type="danger"
class={bem('add')}
text={this.addButtonText || t('add')}
onClick={() => {
this.$emit('add');
}}
/>
</div>
);
}
});