Skip to content

Commit c0568d5

Browse files
committed
feat(date-input): add validation for reversed date range input and allow equal endpoints
1 parent d88ebb9 commit c0568d5

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

packages/varlet-ui/src/date-input/DateInput.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,10 @@ export default defineComponent({
708708
709709
const validDayjsObjects = dayjsObjects.filter(isTruthy)
710710
711+
if (props.range && validDayjsObjects[0].isAfter(validDayjsObjects[1])) {
712+
return
713+
}
714+
711715
if (
712716
validDayjsObjects.some((dayjsObject, index) => {
713717
const position = props.range ? (index === 0 ? 'start' : 'end') : undefined

packages/varlet-ui/src/date-input/__tests__/index.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,56 @@ describe('test dateInput input behavior', () => {
180180
wrapper.unmount()
181181
})
182182

183+
test.each([
184+
{
185+
type: 'date',
186+
modelValue: ['2021-04-08', '2021-04-10'],
187+
input: '2021-04-12 ~ 2021-04-09',
188+
},
189+
{
190+
type: 'datetime',
191+
modelValue: ['2021-04-08 09:00:00', '2021-04-08 18:00:00'],
192+
input: '2021-04-08 20:00:00 ~ 2021-04-08 10:00:00',
193+
},
194+
])('dateInput rejects a manually entered reversed $type range', async ({ type, modelValue, input }) => {
195+
const onUpdateModelValue = vi.fn()
196+
const wrapper = mount(VarDateInput, {
197+
props: {
198+
type,
199+
range: true,
200+
modelValue,
201+
'onUpdate:modelValue': onUpdateModelValue,
202+
},
203+
})
204+
205+
await triggerInput(wrapper, input)
206+
207+
expect(wrapper.find('input').element.value).toBe(input)
208+
expect(onUpdateModelValue).not.toHaveBeenCalled()
209+
210+
await wrapper.find('input').trigger('change')
211+
expect(wrapper.find('input').element.value).toBe(modelValue.join(' ~ '))
212+
213+
wrapper.unmount()
214+
})
215+
216+
test('dateInput accepts a manually entered range with equal endpoints', async () => {
217+
const onUpdateModelValue = vi.fn()
218+
const wrapper = mount(VarDateInput, {
219+
props: {
220+
range: true,
221+
modelValue: [],
222+
'onUpdate:modelValue': onUpdateModelValue,
223+
},
224+
})
225+
226+
await triggerInput(wrapper, '2021-04-08 ~ 2021-04-08')
227+
228+
expect(onUpdateModelValue).lastCalledWith(['2021-04-08', '2021-04-08'])
229+
230+
wrapper.unmount()
231+
})
232+
183233
test('dateInput validates allowedTimes with range positions on manual input', async () => {
184234
const onUpdateModelValue = vi.fn()
185235
const allowedTimes = vi.fn((_date, position) => ({

0 commit comments

Comments
 (0)