datetime-picker-demo.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <view class="datetime-picker-demo">
  3. <view class="demo-title">SsDatetimePicker 组件示例</view>
  4. <!-- 表单示例 -->
  5. <view class="demo-form">
  6. <view class="form-title">表单示例(带校验)</view>
  7. <!-- 日期选择 -->
  8. <view class="form-row">
  9. <view class="label">出生日期*</view>
  10. <Td field="birthDate" :rules="formRules.birthDate">
  11. <SsDatetimePicker
  12. v-model="formData.birthDate"
  13. mode="date"
  14. placeholder="请选择出生日期"
  15. :max-date="new Date()"
  16. />
  17. </Td>
  18. </view>
  19. <!-- 时间选择 -->
  20. <view class="form-row">
  21. <view class="label">上班时间</view>
  22. <Td field="workTime">
  23. <SsDatetimePicker
  24. v-model="formData.workTime"
  25. mode="time"
  26. placeholder="请选择上班时间"
  27. />
  28. </Td>
  29. </view>
  30. <!-- 日期时间选择 -->
  31. <view class="form-row">
  32. <view class="label">预约时间*</view>
  33. <Td field="appointmentTime" :rules="formRules.appointmentTime">
  34. <SsDatetimePicker
  35. v-model="formData.appointmentTime"
  36. mode="datetime"
  37. placeholder="请选择预约时间"
  38. :min-date="new Date()"
  39. />
  40. </Td>
  41. </view>
  42. <!-- 自定义格式 -->
  43. <view class="form-row">
  44. <view class="label">活动日期</view>
  45. <Td field="eventDate">
  46. <SsDatetimePicker
  47. v-model="formData.eventDate"
  48. mode="date"
  49. placeholder="请选择活动日期"
  50. display-format="MM月DD日"
  51. />
  52. </Td>
  53. </view>
  54. <view class="form-actions">
  55. <u-button type="primary" @click="handleSubmit">提交表单</u-button>
  56. <u-button type="default" @click="handleReset">重置</u-button>
  57. </view>
  58. </view>
  59. <!-- 基础示例 -->
  60. <view class="demo-basic">
  61. <view class="form-title">基础示例(无校验)</view>
  62. <view class="basic-item">
  63. <view class="basic-label">日期选择:</view>
  64. <SsDatetimePicker
  65. v-model="basicDate"
  66. mode="date"
  67. placeholder="选择日期"
  68. />
  69. <view class="basic-value">值:{{ basicDate }}</view>
  70. </view>
  71. <view class="basic-item">
  72. <view class="basic-label">时间选择:</view>
  73. <SsDatetimePicker
  74. v-model="basicTime"
  75. mode="time"
  76. placeholder="选择时间"
  77. />
  78. <view class="basic-value">值:{{ basicTime }}</view>
  79. </view>
  80. <view class="basic-item">
  81. <view class="basic-label">日期时间:</view>
  82. <SsDatetimePicker
  83. v-model="basicDatetime"
  84. mode="datetime"
  85. placeholder="选择日期时间"
  86. />
  87. <view class="basic-value">值:{{ basicDatetime }}</view>
  88. </view>
  89. </view>
  90. </view>
  91. </template>
  92. <script setup>
  93. import { ref, provide } from 'vue'
  94. import { useFormValidation } from '@/composables/useFormValidation.js'
  95. import SsDatetimePicker from '@/components/SsDatetimePicker/index.vue'
  96. import Td from '@/components/Td/index.vue'
  97. // 表单数据
  98. const formData = ref({
  99. birthDate: '',
  100. workTime: '',
  101. appointmentTime: '',
  102. eventDate: ''
  103. })
  104. // 表单校验规则
  105. const formRules = {
  106. birthDate: [
  107. { required: true, message: '请选择出生日期' }
  108. ],
  109. appointmentTime: [
  110. { required: true, message: '请选择预约时间' },
  111. {
  112. validator: (value) => {
  113. if (!value) return true
  114. const appointmentDate = new Date(value)
  115. const now = new Date()
  116. if (appointmentDate <= now) {
  117. return '预约时间必须晚于当前时间'
  118. }
  119. return true
  120. },
  121. message: '预约时间必须晚于当前时间'
  122. }
  123. ]
  124. }
  125. // 使用表单校验
  126. const { validateForm, errors, clearErrors } = useFormValidation()
  127. // 为子组件提供数据和方法
  128. provide('formData', formData)
  129. provide('errors', errors)
  130. provide('validateField', (fieldName, value, rules) => {
  131. // 这里可以添加自定义校验逻辑
  132. return true
  133. })
  134. provide('getFieldConfig', () => ({}))
  135. // 基础示例数据
  136. const basicDate = ref('')
  137. const basicTime = ref('')
  138. const basicDatetime = ref('')
  139. // 提交表单
  140. const handleSubmit = () => {
  141. console.log('提交表单', formData.value)
  142. const isValid = validateForm(formData.value, formRules)
  143. if (isValid) {
  144. uni.showToast({
  145. title: '提交成功',
  146. icon: 'success'
  147. })
  148. console.log('表单校验通过', formData.value)
  149. } else {
  150. uni.showToast({
  151. title: '请检查表单信息',
  152. icon: 'none'
  153. })
  154. console.log('表单校验失败', errors.value)
  155. }
  156. }
  157. // 重置表单
  158. const handleReset = () => {
  159. formData.value = {
  160. birthDate: '',
  161. workTime: '',
  162. appointmentTime: '',
  163. eventDate: ''
  164. }
  165. clearErrors()
  166. uni.showToast({
  167. title: '已重置',
  168. icon: 'success'
  169. })
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. .datetime-picker-demo {
  174. padding: 32rpx;
  175. background-color: #f5f5f5;
  176. min-height: 100vh;
  177. }
  178. .demo-title {
  179. font-size: 36rpx;
  180. font-weight: bold;
  181. color: #333;
  182. text-align: center;
  183. margin-bottom: 40rpx;
  184. }
  185. .demo-form, .demo-basic {
  186. background-color: #fff;
  187. border-radius: 16rpx;
  188. padding: 32rpx;
  189. margin-bottom: 32rpx;
  190. }
  191. .form-title {
  192. font-size: 32rpx;
  193. font-weight: bold;
  194. color: #333;
  195. margin-bottom: 32rpx;
  196. padding-bottom: 16rpx;
  197. border-bottom: 2rpx solid #f0f0f0;
  198. }
  199. .form-row {
  200. display: flex;
  201. align-items: center;
  202. margin-bottom: 32rpx;
  203. .label {
  204. width: 200rpx;
  205. font-size: 28rpx;
  206. color: #666;
  207. flex-shrink: 0;
  208. }
  209. }
  210. .form-actions {
  211. display: flex;
  212. gap: 32rpx;
  213. margin-top: 48rpx;
  214. .u-button {
  215. flex: 1;
  216. }
  217. }
  218. .basic-item {
  219. margin-bottom: 32rpx;
  220. padding: 24rpx;
  221. background-color: #f8f9fa;
  222. border-radius: 12rpx;
  223. .basic-label {
  224. font-size: 28rpx;
  225. color: #666;
  226. margin-bottom: 16rpx;
  227. }
  228. .basic-value {
  229. font-size: 24rpx;
  230. color: #999;
  231. margin-top: 16rpx;
  232. word-break: break-all;
  233. }
  234. }
  235. </style>