index.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { use } from '../utils';
  2. import { emit, inherit } from '../utils/functional';
  3. import Icon from '../icon';
  4. import Cell from '../cell';
  5. import Button from '../button';
  6. import Radio from '../radio';
  7. import RadioGroup from '../radio-group';
  8. // Types
  9. import { CreateElement, RenderContext } from 'vue/types';
  10. import { DefaultSlots } from '../utils/use/sfc';
  11. export type ContactListItem = {
  12. id: string | number;
  13. tel: string | number;
  14. name: string;
  15. };
  16. export type ContactListProps = {
  17. value?: any;
  18. list: ContactListItem[];
  19. addText?: string;
  20. };
  21. const [sfc, bem, t] = use('contact-list');
  22. function ContactList(
  23. h: CreateElement,
  24. props: ContactListProps,
  25. slots: DefaultSlots,
  26. ctx: RenderContext<ContactListProps>
  27. ) {
  28. const List = props.list.map((item, index) => {
  29. const onClick = () => {
  30. emit(ctx, 'input', item.id);
  31. emit(ctx, 'select', item, index);
  32. };
  33. return (
  34. <Cell
  35. key={item.id}
  36. isLink
  37. class={bem('item')}
  38. valueClass={bem('item-value')}
  39. scopedSlots={{
  40. default: () => (
  41. <Radio name={item.id} onClick={onClick}>
  42. <div class={bem('name')}>{`${item.name},${item.tel}`}</div>
  43. </Radio>
  44. ),
  45. 'right-icon': () => (
  46. <Icon
  47. name="edit"
  48. class={bem('edit')}
  49. onClick={event => {
  50. event.stopPropagation();
  51. emit(ctx, 'edit', item, index);
  52. }}
  53. />
  54. )
  55. }}
  56. onClick={onClick}
  57. />
  58. );
  59. });
  60. return (
  61. <div class={bem()} {...inherit(ctx)}>
  62. <RadioGroup value={props.value} class={bem('group')}>
  63. {List}
  64. </RadioGroup>
  65. <Button
  66. square
  67. size="large"
  68. type="danger"
  69. class={bem('add')}
  70. text={props.addText || t('addText')}
  71. onClick={() => {
  72. emit(ctx, 'add');
  73. }}
  74. />
  75. </div>
  76. );
  77. }
  78. ContactList.props = {
  79. value: null as any,
  80. list: Array,
  81. addText: String
  82. };
  83. export default sfc(ContactList);