Skip to content

Commit d177272

Browse files
committed
feat(VDateInput): add new display-format prop
1 parent 5ce8ec8 commit d177272

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"props": {
3-
"hideActions": "Hide the Cancel and OK buttons, and automatically update the value when a date is selected."
3+
"hideActions": "Hide the Cancel and OK buttons, and automatically update the value when a date is selected.",
4+
"displayFormat": "The format of the date that is displayed in the input. Can use any format [here](/features/dates/#format-options) or a custom function."
45
}
56
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template>
2+
<div class="d-flex justify-center">
3+
<v-date-input
4+
v-model="model"
5+
:display-format="format"
6+
max-width="368"
7+
prefix="ISO Date:"
8+
></v-date-input>
9+
</div>
10+
</template>
11+
12+
<script setup>
13+
import { computed, shallowRef } from 'vue'
14+
import { useDate } from 'vuetify'
15+
16+
const adapter = useDate()
17+
const model = shallowRef(adapter.parseISO('2025-02-25'))
18+
19+
function format (date) {
20+
return adapter.toISO(date)
21+
}
22+
</script>

packages/docs/src/pages/en/components/date-inputs.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ At its core, the `v-date-input` component is a basic container that extends [v-t
6262

6363
The `v-date-input` component is a replacement for the standard date input. It provides a clean interface for selecting dates and shows detailed selection information.
6464

65+
::: tip
66+
67+
Use the built in parseISO and toISO methods available as part of the [date composable](/features/dates/) to format and parse the date input. Internally, `v-date-input` transforms the model into a plain JS Date object.
68+
69+
:::
70+
6571
### Props
6672

6773
The `v-date-input` component extends the [v-text-field](/components/text-fields/) and [v-date-picker](/components/date-pickers/) component; and supports all of their props.
@@ -90,6 +96,12 @@ You can move the calendar icon within the input or entirely by utilizing the **p
9096

9197
<ExamplesExample file="v-date-input/prop-prepend-icon" />
9298

99+
#### Display format
100+
101+
You can use the **display-format** prop in conjunction with the [date composable](/features/dates/) to change the displayed format of the date in the input.
102+
103+
<ExamplesExample file="v-date-input/prop-display-format" />
104+
93105
## Examples
94106

95107
The following are a collection of examples that demonstrate more advanced and real world use of the `v-date-input` component.

packages/vuetify/src/labs/VDateInput/VDateInput.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type VDateInputSlots = Omit<VTextFieldSlots, 'default'> & {
3333
}
3434

3535
export const makeVDateInputProps = propsFactory({
36+
displayFormat: [Function, String],
3637
hideActions: Boolean,
3738
location: {
3839
type: String as PropType<StrategyProps['location']>,
@@ -76,6 +77,14 @@ export const VDateInput = genericComponent<VDateInputSlots>()({
7677
const menu = shallowRef(false)
7778
const vDateInputRef = ref()
7879

80+
function format (date: unknown) {
81+
if (typeof props.displayFormat === 'function') {
82+
return props.displayFormat(date)
83+
}
84+
85+
return adapter.format(date, props.displayFormat ?? 'keyboardDate')
86+
}
87+
7988
const display = computed(() => {
8089
const value = wrapInArray(model.value)
8190

@@ -89,12 +98,12 @@ export const VDateInput = genericComponent<VDateInputSlots>()({
8998
const start = value[0]
9099
const end = value[value.length - 1]
91100

92-
return adapter.isValid(start) && adapter.isValid(end)
93-
? `${adapter.format(adapter.date(start), 'keyboardDate')} - ${adapter.format(adapter.date(end), 'keyboardDate')}`
94-
: ''
101+
if (!adapter.isValid(start) || !adapter.isValid(end)) return ''
102+
103+
return `${format(adapter.date(start))} - ${format(adapter.date(end))}`
95104
}
96105

97-
return adapter.isValid(model.value) ? adapter.format(adapter.date(model.value), 'keyboardDate') : ''
106+
return adapter.isValid(model.value) ? format(adapter.date(model.value)) : ''
98107
})
99108

100109
const isInteractive = computed(() => !props.disabled && !props.readonly)

0 commit comments

Comments
 (0)