index.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { ActionSheet } from 'vant';
  2. import { defineComponent, PropType, reactive, watch } from 'vue';
  3. import styles from './index.module.less';
  4. type actionsType = {
  5. name: string | number;
  6. value?: string | number;
  7. selected?: boolean;
  8. };
  9. export default defineComponent({
  10. name: 'k-action-sheet',
  11. props: {
  12. show: {
  13. type: Boolean,
  14. default: false
  15. },
  16. actions: {
  17. type: Array as PropType<actionsType[]>,
  18. required: true,
  19. default: () => []
  20. },
  21. cancelText: {
  22. type: String,
  23. default: '取消'
  24. },
  25. teleport: {
  26. type: [String, Element],
  27. default: ''
  28. }
  29. },
  30. emits: ['update:show', 'select'],
  31. setup(props, { emit }) {
  32. const form = reactive({
  33. oPopover: props.show
  34. });
  35. const onSelect = (item: any) => {
  36. emit('select', item);
  37. emit('update:show', false);
  38. };
  39. watch(
  40. () => form.oPopover,
  41. () => {
  42. emit('update:show', form.oPopover);
  43. }
  44. );
  45. watch(
  46. () => props.show,
  47. () => {
  48. form.oPopover = props.show;
  49. }
  50. );
  51. return () => (
  52. <ActionSheet v-model:show={form.oPopover} teleport={props.teleport}>
  53. <div class={[styles['k-sheet_content'], 'van-hairline--bottom']}>
  54. {props.actions.map((item: any) => (
  55. <div
  56. class={[
  57. 'van-sheet-item van-ellipsis',
  58. item.selected && 'van-sheet-item-active'
  59. ]}
  60. onClick={() => onSelect(item)}>
  61. {item.name}
  62. </div>
  63. ))}
  64. </div>
  65. <button
  66. type="button"
  67. class={[
  68. 'van-action-sheet__cancel',
  69. styles['k-action-sheet_bottom__cancel']
  70. ]}
  71. onClick={() => emit('update:show', false)}>
  72. {props.cancelText}
  73. </button>
  74. </ActionSheet>
  75. );
  76. }
  77. });