Blame view

泰额版/Food Labeling Management App UniApp/src/components/AppDatePicker.vue 6.96 KB
540ac0e3   杨鑫   前端修改bug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  <template>
    <view class="app-date-picker">
      <view class="app-date-trigger" @click="openDialog">
        <text class="app-date-trigger-text">{{ displayText }}</text>
      </view>
  
      <view v-if="dialogVisible" class="app-date-mask" @click="cancelDialog">
        <view class="app-date-dialog" @click.stop>
          <text class="app-date-dialog-title">{{ dialogTitle }}</text>
          <picker-view
            class="app-date-picker-view"
            :indicator-style="indicatorStyle"
            :value="selection"
            @change="onPickerChange"
          >
            <picker-view-column>
              <view v-for="y in years" :key="'y-' + y" class="app-date-picker-item">{{ y }}</view>
            </picker-view-column>
            <picker-view-column>
              <view v-for="(m, idx) in monthLabels" :key="'m-' + idx" class="app-date-picker-item">{{ m }}</view>
            </picker-view-column>
            <picker-view-column>
              <view v-for="d in daysInMonth" :key="'d-' + d" class="app-date-picker-item">{{ d }}</view>
            </picker-view-column>
          </picker-view>
          <view class="app-date-actions">
            <view class="app-date-btn app-date-btn-cancel" @click="cancelDialog">
              <text>Cancel</text>
            </view>
            <view class="app-date-btn app-date-btn-confirm" @click="confirmDialog">
              <text>Confirm</text>
            </view>
          </view>
        </view>
      </view>
    </view>
  </template>
  
  <script setup lang="ts">
  import { computed, ref, watch } from 'vue'
  
  const MONTH_LABELS = [
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
  ] as const
  
  const props = withDefaults(
    defineProps<{
      modelValue: string
      /** yyyy-MM-dd */
      min?: string
      max?: string
      placeholder?: string
      dialogTitle?: string
    }>(),
    {
      modelValue: '',
      min: '2000-01-01',
      max: '2099-12-31',
      placeholder: 'Select',
      dialogTitle: 'Select date',
    },
  )
  
  const emit = defineEmits<{
    'update:modelValue': [value: string]
  }>()
  
  const dialogVisible = ref(false)
  const selection = ref<[number, number, number]>([0, 0, 0])
  const draftY = ref(2026)
  const draftM = ref(1)
  const draftD = ref(1)
  
  const indicatorStyle = 'height: 44px;'
  
  const years = computed(() => {
    const minY = parseYmd(props.min).year
    const maxY = parseYmd(props.max).year
    const out: number[] = []
    for (let y = minY; y <= maxY; y++) out.push(y)
    return out.length ? out : [new Date().getFullYear()]
  })
  
  const monthLabels = MONTH_LABELS
  
  const daysInMonth = computed(() => {
    const max = daysInMonthCount(draftY.value, draftM.value)
    return Array.from({ length: max }, (_, i) => String(i + 1).padStart(2, '0'))
  })
  
  const displayText = computed(() => {
    const v = (props.modelValue || '').trim()
    if (!v) return props.placeholder
    const p = parseYmd(v)
    if (!p.valid) return v
    return `${String(p.month).padStart(2, '0')}/${String(p.day).padStart(2, '0')}/${p.year}`
  })
  
  function parseYmd(raw: string | undefined): { year: number; month: number; day: number; valid: boolean } {
    const s = (raw || '').trim()
    const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s)
    if (!m) {
      const d = new Date()
      return { year: d.getFullYear(), month: d.getMonth() + 1, day: d.getDate(), valid: false }
    }
    return {
      year: Number(m[1]),
      month: Number(m[2]),
      day: Number(m[3]),
      valid: true,
    }
  }
  
  function daysInMonthCount(year: number, month: number): number {
    return new Date(year, month, 0).getDate()
  }
  
  function clampDate(y: number, mo: number, d: number): { y: number; mo: number; d: number } {
    const minP = parseYmd(props.min)
    const maxP = parseYmd(props.max)
    let year = y
    let month = mo
    let day = d
    const maxDay = daysInMonthCount(year, month)
    if (day > maxDay) day = maxDay
  
    const cur = new Date(year, month - 1, day)
    const minD = new Date(minP.year, minP.month - 1, minP.day)
    const maxD = new Date(maxP.year, maxP.month - 1, maxP.day)
    let clamped = cur
    if (cur < minD) clamped = minD
    if (cur > maxD) clamped = maxD
  
    return {
      y: clamped.getFullYear(),
      mo: clamped.getMonth() + 1,
      d: clamped.getDate(),
    }
  }
  
  function toYmd(y: number, mo: number, d: number): string {
    return `${y}-${String(mo).padStart(2, '0')}-${String(d).padStart(2, '0')}`
  }
  
  function syncSelectionFromDate(y: number, mo: number, d: number) {
    const yi = Math.max(0, years.value.indexOf(y))
    const mi = Math.max(0, Math.min(11, mo - 1))
    const maxD = daysInMonthCount(y, mo)
    const day = Math.min(d, maxD)
    const di = Math.max(0, day - 1)
    draftY.value = y
    draftM.value = mo
    draftD.value = day
    selection.value = [yi, mi, di]
  }
  
  function openDialog() {
    const base = parseYmd(props.modelValue)
    const clamped = clampDate(base.year, base.month, base.day)
    syncSelectionFromDate(clamped.y, clamped.mo, clamped.d)
    dialogVisible.value = true
  }
  
  function onPickerChange(e: { detail: { value: number[] } }) {
    const arr = e.detail.value || [0, 0, 0]
    const y = years.value[arr[0]] ?? draftY.value
    const mo = (arr[1] ?? 0) + 1
    let d = (arr[2] ?? 0) + 1
    const clamped = clampDate(y, mo, d)
    syncSelectionFromDate(clamped.y, clamped.mo, clamped.d)
  }
  
  function cancelDialog() {
    dialogVisible.value = false
  }
  
  function confirmDialog() {
    const v = toYmd(draftY.value, draftM.value, draftD.value)
    emit('update:modelValue', v)
    dialogVisible.value = false
  }
  
  watch(
    () => props.modelValue,
    (v) => {
      if (dialogVisible.value) return
      const p = parseYmd(v)
      if (p.valid) {
        const c = clampDate(p.year, p.month, p.day)
        draftY.value = c.y
        draftM.value = c.mo
        draftD.value = c.d
      }
    },
  )
  </script>
  
  <style scoped>
  .app-date-trigger {
    height: 72rpx;
    padding: 0 24rpx;
    background: #fff;
    border: 2rpx solid #e5e7eb;
    border-radius: 12rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
  }
  
  .app-date-trigger-text {
    font-size: 28rpx;
    color: #111827;
  }
  
  .app-date-mask {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: flex-end;
    justify-content: center;
  }
  
  .app-date-dialog {
    width: 100%;
    background: #fff;
    border-radius: 24rpx 24rpx 0 0;
    padding: 28rpx 32rpx calc(28rpx + env(safe-area-inset-bottom));
    box-sizing: border-box;
  }
  
  .app-date-dialog-title {
    display: block;
    text-align: center;
    font-size: 32rpx;
    font-weight: 600;
    color: #111827;
    margin-bottom: 16rpx;
  }
  
  .app-date-picker-view {
    width: 100%;
    height: 440rpx;
  }
  
  .app-date-picker-item {
    height: 44px;
    line-height: 44px;
    text-align: center;
    font-size: 30rpx;
    color: #111827;
  }
  
  .app-date-actions {
    display: flex;
    gap: 24rpx;
    margin-top: 20rpx;
  }
  
  .app-date-btn {
    flex: 1;
    height: 88rpx;
    border-radius: 16rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 30rpx;
    font-weight: 600;
  }
  
  .app-date-btn-cancel {
    border: 2rpx solid #d1d5db;
    color: #374151;
    background: #fff;
  }
  
  .app-date-btn-confirm {
    background: var(--theme-primary, #4f46e5);
    color: #fff;
  }
  </style>