select-time.uvue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <cl-page>
  3. <view class="p-3">
  4. <demo-item :label="t('基础用法')">
  5. <cl-select-time v-model="form.time1"></cl-select-time>
  6. </demo-item>
  7. <demo-item :label="t('自定义触发器')">
  8. <view class="flex flex-row">
  9. <cl-button @tap="openSelect2">{{ t("打开选择器") }} </cl-button>
  10. </view>
  11. <cl-select-time
  12. ref="selectRef2"
  13. v-model="form.time2"
  14. :show-trigger="false"
  15. ></cl-select-time>
  16. </demo-item>
  17. <demo-item :label="t('自定义')">
  18. <cl-select-time
  19. v-model="form.time3"
  20. :disabled="isDisabled"
  21. :label-format="labelFormat"
  22. :type="type"
  23. ></cl-select-time>
  24. <cl-list
  25. border
  26. :pt="{
  27. className: 'mt-3'
  28. }"
  29. >
  30. <cl-list-item :label="t('时')">
  31. <cl-switch v-model="isHour"></cl-switch>
  32. </cl-list-item>
  33. <cl-list-item :label="t('时:分')">
  34. <cl-switch v-model="isMinute"></cl-switch>
  35. </cl-list-item>
  36. <cl-list-item :label="t('时:分:秒')">
  37. <cl-switch v-model="isSecond"></cl-switch>
  38. </cl-list-item>
  39. <cl-list-item :label="t('标签格式化')">
  40. <cl-switch v-model="isFormat"></cl-switch>
  41. </cl-list-item>
  42. <cl-list-item :label="t('禁用')">
  43. <cl-switch v-model="isDisabled"></cl-switch>
  44. </cl-list-item>
  45. </cl-list>
  46. </demo-item>
  47. </view>
  48. </cl-page>
  49. </template>
  50. <script lang="ts" setup>
  51. import { computed, reactive, ref } from "vue";
  52. import DemoItem from "../components/item.uvue";
  53. import { useUi } from "@/uni_modules/cool-ui";
  54. import { t } from "@/locale";
  55. const ui = useUi();
  56. type Form = {
  57. time1: string;
  58. time2: string;
  59. time3: string;
  60. };
  61. const form = reactive<Form>({
  62. time1: "",
  63. time2: "",
  64. time3: ""
  65. });
  66. const isDisabled = ref(false);
  67. const isFormat = ref(false);
  68. const isHour = ref(false);
  69. const isMinute = ref(false);
  70. const isSecond = ref(true);
  71. const selectRef2 = ref<ClSelectTimeComponentPublicInstance | null>(null);
  72. function openSelect2() {
  73. selectRef2.value!.open((value) => {
  74. ui.showToast({
  75. message: `你选择了:${value}`
  76. });
  77. });
  78. }
  79. const type = computed(() => {
  80. if (isHour.value) {
  81. return "hour";
  82. }
  83. if (isMinute.value) {
  84. return "minute";
  85. }
  86. return "second";
  87. });
  88. const labelFormat = computed(() => {
  89. if (isFormat.value) {
  90. switch (type.value) {
  91. case "hour":
  92. return "{H}时";
  93. case "minute":
  94. return "{H}时{m}分";
  95. case "second":
  96. return "{H}时{m}分{s}秒";
  97. default:
  98. return null;
  99. }
  100. } else {
  101. return null;
  102. }
  103. });
  104. </script>