index.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { use } from '../utils';
  2. import { emit, inherit } from '../utils/functional';
  3. import Icon from '../icon';
  4. // Types
  5. import { CreateElement, RenderContext } from 'vue/types';
  6. import { DefaultSlots } from '../utils/use/sfc';
  7. export type TreeSelectItem = {
  8. text: string;
  9. disabled?: boolean;
  10. children: TreeSelectChildren[];
  11. };
  12. export type TreeSelectChildren = {
  13. id: number;
  14. text: string;
  15. disabled?: boolean;
  16. };
  17. export type TreeSelectProps = {
  18. height: number;
  19. items: TreeSelectItem[];
  20. activeId: number | string;
  21. mainActiveIndex: number;
  22. };
  23. const [sfc, bem] = use('tree-select');
  24. function TreeSelect(
  25. h: CreateElement,
  26. props: TreeSelectProps,
  27. slots: DefaultSlots,
  28. ctx: RenderContext<TreeSelectProps>
  29. ) {
  30. const { height, items, mainActiveIndex, activeId } = props;
  31. const selectedItem = items[mainActiveIndex] || {};
  32. const subItems = selectedItem.children || [];
  33. return (
  34. <div class={bem()} style={{ height: `${height}px` }} {...inherit(ctx)}>
  35. <div class={bem('nav')}>
  36. {items.map((item, index) => (
  37. <div
  38. key={index}
  39. class={[
  40. 'van-ellipsis',
  41. bem('nitem', {
  42. active: mainActiveIndex === index,
  43. disabled: item.disabled
  44. })
  45. ]}
  46. onClick={() => {
  47. if (!item.disabled) {
  48. emit(ctx, 'navclick', index);
  49. }
  50. }}
  51. >
  52. {item.text}
  53. </div>
  54. ))}
  55. </div>
  56. <div class={bem('content')}>
  57. {subItems.map(item => (
  58. <div
  59. key={item.id}
  60. class={[
  61. 'van-ellipsis',
  62. bem('item', {
  63. active: activeId === item.id,
  64. disabled: item.disabled
  65. })
  66. ]}
  67. onClick={() => {
  68. if (!item.disabled) {
  69. emit(ctx, 'itemclick', item);
  70. }
  71. }}
  72. >
  73. {item.text}
  74. {activeId === item.id && (
  75. <Icon name="checked" size="16px" class={bem('selected')} />
  76. )}
  77. </div>
  78. ))}
  79. </div>
  80. </div>
  81. );
  82. }
  83. TreeSelect.props = {
  84. items: Array,
  85. mainActiveIndex: Number,
  86. activeId: {
  87. type: [Number, String],
  88. default: 0
  89. },
  90. height: {
  91. type: Number,
  92. default: 300
  93. }
  94. };
  95. export default sfc<TreeSelectProps>(TreeSelect);