diff --git a/components.js b/components.js index 5d91abde8..2b50e7e54 100644 --- a/components.js +++ b/components.js @@ -67,5 +67,6 @@ module.exports = [ 'image-preview', 'index-bar', 'index-anchor', + 'address-list', 'area', ]; diff --git a/src/address-list/Item.js b/src/address-list/Item.js new file mode 100644 index 000000000..8cf4833ab --- /dev/null +++ b/src/address-list/Item.js @@ -0,0 +1,91 @@ +// Utils +import { createNamespace } from '../utils'; + +// Components +import Tag from '../tag'; +import Icon from '../icon'; +import Cell from '../cell'; +import Radio from '../radio'; + +const [createComponent, bem] = createNamespace('address-item'); + +export default createComponent({ + props: { + data: Object, + disabled: Boolean, + switchable: Boolean, + defaultTagText: String, + }, + + emits: ['edit', 'click', 'select'], + + setup(props, { slots, emit }) { + function onClick() { + if (props.switchable) { + emit('select'); + } + emit('click'); + } + + const genRightIcon = () => ( + { + event.stopPropagation(); + emit('edit'); + emit('click'); + }} + /> + ); + + function genTag() { + if (props.data.isDefault && props.defaultTagText) { + return ( + + {props.defaultTagText} + + ); + } + } + + function genContent() { + const { data } = props; + const Info = [ +
+ {`${data.name} ${data.tel}`} + {genTag()} +
, +
{data.address}
, + ]; + + if (props.switchable && !props.disabled) { + return ( + + {Info} + + ); + } + + return Info; + } + + return () => { + const { disabled } = props; + + return ( +
+ + {slots.bottom?.({ ...props.data, disabled })} +
+ ); + }; + }, +}); diff --git a/src/address-list/Item.tsx b/src/address-list/Item.tsx deleted file mode 100644 index 8aa233297..000000000 --- a/src/address-list/Item.tsx +++ /dev/null @@ -1,126 +0,0 @@ -// Utils -import { createNamespace } from '../utils'; -import { emit, inherit } from '../utils/functional'; - -// Components -import Tag from '../tag'; -import Icon from '../icon'; -import Cell from '../cell'; -import Radio from '../radio'; - -// Types -import { CreateElement, RenderContext } from 'vue/types'; -import { DefaultSlots, ScopedSlot } from '../utils/types'; - -export type AddressItemData = { - id: string | number; - tel: string | number; - name: string; - address: string; - isDefault: boolean; -}; - -export type AddressItemProps = { - data: AddressItemData; - disabled?: boolean; - switchable?: boolean; - defaultTagText?: string; -}; - -export type AddressItemSlots = DefaultSlots & { - bottom?: ScopedSlot; -}; - -export type AddressItemEvents = { - onEdit(): void; - onClick(): void; - onSelect(): void; -}; - -const [createComponent, bem] = createNamespace('address-item'); - -function AddressItem( - h: CreateElement, - props: AddressItemProps, - slots: AddressItemSlots, - ctx: RenderContext -) { - const { disabled, switchable } = props; - - function onClick() { - if (switchable) { - emit(ctx, 'select'); - } - - emit(ctx, 'click'); - } - - const genRightIcon = () => ( - { - event.stopPropagation(); - emit(ctx, 'edit'); - emit(ctx, 'click'); - }} - /> - ); - - function genTag() { - if (props.data.isDefault && props.defaultTagText) { - return ( - - {props.defaultTagText} - - ); - } - } - - function genContent() { - const { data } = props; - const Info = [ -
- {`${data.name} ${data.tel}`} - {genTag()} -
, -
{data.address}
, - ]; - - if (switchable && !disabled) { - return ( - - {Info} - - ); - } - - return Info; - } - - return ( -
- - {slots.bottom?.({ ...props.data, disabled })} -
- ); -} - -AddressItem.props = { - data: Object, - disabled: Boolean, - switchable: Boolean, - defaultTagText: String, -}; - -export default createComponent( - AddressItem -); diff --git a/src/address-list/index.js b/src/address-list/index.js new file mode 100644 index 000000000..da15e1a37 --- /dev/null +++ b/src/address-list/index.js @@ -0,0 +1,97 @@ +// Utils +import { createNamespace } from '../utils'; + +// Components +import Button from '../button'; +import RadioGroup from '../radio-group'; +import AddressItem from './Item'; + +const [createComponent, bem, t] = createNamespace('address-list'); + +export default createComponent({ + props: { + list: Array, + modelValue: [Number, String], + disabledList: Array, + disabledText: String, + addButtonText: String, + defaultTagText: String, + switchable: { + type: Boolean, + default: true, + }, + }, + + emits: [ + 'add', + 'edit', + 'select', + 'click-item', + 'edit-disabled', + 'select-disabled', + 'update:modelValue', + ], + + setup(props, { slots, emit }) { + return () => { + function genList(list, disabled) { + if (!list) { + return; + } + + return list.map((item, index) => ( + { + emit(disabled ? 'select-disabled' : 'select', item, index); + + if (!disabled) { + emit('update:modelValue', item.id); + } + }} + onEdit={() => { + emit(disabled ? 'edit-disabled' : 'edit', item, index); + }} + onClick={() => { + emit('click-item', item, index); + }} + /> + )); + } + + const List = genList(props.list); + const DisabledList = genList(props.disabledList, true); + + return ( +
+ {slots.top?.()} + {List} + {props.disabledText && ( +
{props.disabledText}
+ )} + {DisabledList} + {slots.default?.()} +
+
+
+ ); + }; + }, +}); diff --git a/src/address-list/index.tsx b/src/address-list/index.tsx deleted file mode 100644 index 1726e0508..000000000 --- a/src/address-list/index.tsx +++ /dev/null @@ -1,110 +0,0 @@ -// Utils -import { createNamespace } from '../utils'; -import { emit, inherit } from '../utils/functional'; - -// Components -import Button from '../button'; -import RadioGroup from '../radio-group'; -import AddressItem, { AddressItemData } from './Item'; - -// Types -import { CreateElement, RenderContext } from 'vue/types'; -import { ScopedSlot, DefaultSlots } from '../utils/types'; - -export type AddressListProps = { - value?: string | number; - switchable: boolean; - disabledText?: string; - addButtonText?: string; - list?: AddressItemData[]; - disabledList?: AddressItemData[]; - defaultTagText?: string; -}; - -export type AddressListSlots = DefaultSlots & { - top?: ScopedSlot; - 'item-bottom'?: ScopedSlot; -}; - -const [createComponent, bem, t] = createNamespace('address-list'); - -function AddressList( - h: CreateElement, - props: AddressListProps, - slots: AddressListSlots, - ctx: RenderContext -) { - function genList(list?: AddressItemData[], disabled?: boolean) { - if (!list) { - return; - } - - return list.map((item, index) => ( - { - emit(ctx, disabled ? 'select-disabled' : 'select', item, index); - - if (!disabled) { - emit(ctx, 'input', item.id); - } - }} - onEdit={() => { - emit(ctx, disabled ? 'edit-disabled' : 'edit', item, index); - }} - onClick={() => { - emit(ctx, 'click-item', item, index); - }} - /> - )); - } - - const List = genList(props.list); - const DisabledList = genList(props.disabledList, true); - - return ( -
- {slots.top?.()} - {List} - {props.disabledText && ( -
{props.disabledText}
- )} - {DisabledList} - {slots.default?.()} -
-
-
- ); -} - -AddressList.props = { - list: Array, - value: [Number, String], - disabledList: Array, - disabledText: String, - addButtonText: String, - defaultTagText: String, - switchable: { - type: Boolean, - default: true, - }, -}; - -export default createComponent(AddressList); diff --git a/vant.config.js b/vant.config.js index ea4b609f8..85cdfc04d 100644 --- a/vant.config.js +++ b/vant.config.js @@ -327,10 +327,10 @@ module.exports = { // path: 'address-edit', // title: 'AddressEdit 地址编辑', // }, - // { - // path: 'address-list', - // title: 'AddressList 地址列表', - // }, + { + path: 'address-list', + title: 'AddressList 地址列表', + }, { path: 'area', title: 'Area 省市区选择', @@ -661,10 +661,10 @@ module.exports = { // path: 'address-edit', // title: 'AddressEdit', // }, - // { - // path: 'address-list', - // title: 'AddressList', - // }, + { + path: 'address-list', + title: 'AddressList', + }, { path: 'area', title: 'Area',