|
96 | 96 | v-text="formattedTime" /> |
97 | 97 | </template> |
98 | 98 |
|
99 | | -<script> |
100 | | -import { computed } from 'vue' |
| 99 | +<script setup lang="ts"> |
| 100 | +import { toRef } from 'vue' |
101 | 101 | import { useFormatDateTime } from '../../composables/useFormatDateTime.ts' |
102 | 102 |
|
103 | | -export default { |
104 | | - name: 'NcDateTime', |
105 | | -
|
106 | | - props: { |
107 | | - /** |
108 | | - * The timestamp to display, either an unix timestamp (in milliseconds) or a Date object |
109 | | - */ |
110 | | - timestamp: { |
111 | | - type: [Date, Number], |
112 | | - required: true, |
113 | | - }, |
114 | | - /** |
115 | | - * The format used for displaying, or if relative time is used the format used for the title (optional) |
116 | | - * |
117 | | - * @type {Intl.DateTimeFormatOptions} |
118 | | - */ |
119 | | - format: { |
120 | | - type: Object, |
121 | | - default: () => ({ timeStyle: 'medium', dateStyle: 'short' }), |
122 | | - }, |
123 | | - /** |
124 | | - * Wether to display the timestamp as time from now (optional) |
125 | | - * |
126 | | - * - `false`: Disable relative time |
127 | | - * - `'long'`: Long text, like *2 seconds ago* (default) |
128 | | - * - `'short'`: Short text, like *2 sec. ago* |
129 | | - * - `'narrow'`: Even shorter text (same as `'short'` on some languages) |
130 | | - */ |
131 | | - relativeTime: { |
132 | | - type: [Boolean, String], |
133 | | - default: 'long', |
134 | | - validator: (v) => v === false || ['long', 'short', 'narrow'].includes(v), |
135 | | - }, |
136 | | - /** |
137 | | - * Ignore seconds when displaying the relative time and just show `a few seconds ago` |
138 | | - */ |
139 | | - ignoreSeconds: { |
140 | | - type: Boolean, |
141 | | - default: false, |
142 | | - }, |
143 | | - }, |
144 | | -
|
145 | | - setup(props) { |
146 | | - const timestamp = computed(() => props.timestamp) |
147 | | - const { formattedTime, formattedFullTime } = useFormatDateTime(timestamp, props) |
148 | | - return { |
149 | | - formattedTime, |
150 | | - formattedFullTime, |
151 | | - } |
152 | | - }, |
153 | | -} |
| 103 | +const props = withDefaults(defineProps<{ |
| 104 | + /** |
| 105 | + * The timestamp to display, either an unix timestamp (in milliseconds) or a Date object |
| 106 | + */ |
| 107 | + timestamp: Date | number |
| 108 | +
|
| 109 | + /** |
| 110 | + * The format used for displaying, or if relative time is used the format used for the title (optional) |
| 111 | + */ |
| 112 | + format?: Intl.DateTimeFormatOptions |
| 113 | +
|
| 114 | + /** |
| 115 | + * Wether to display the timestamp as time from now (optional) |
| 116 | + * |
| 117 | + * - `false`: Disable relative time |
| 118 | + * - `'long'`: Long text, like *2 seconds ago* (default) |
| 119 | + * - `'short'`: Short text, like *2 sec. ago* |
| 120 | + * - `'narrow'`: Even shorter text (same as `'short'` on some languages) |
| 121 | + */ |
| 122 | + relativeTime?: false | 'long' | 'short' | 'narrow' |
| 123 | +
|
| 124 | + /** |
| 125 | + * Ignore seconds when displaying the relative time and just show `a few seconds ago` |
| 126 | + */ |
| 127 | + ignoreSeconds?: boolean |
| 128 | +}>(), { |
| 129 | + format: () => ({ timeStyle: 'medium', dateStyle: 'short' }), |
| 130 | + relativeTime: 'long', |
| 131 | +}) |
| 132 | +
|
| 133 | +const { |
| 134 | + formattedTime, |
| 135 | + formattedFullTime, |
| 136 | +} = useFormatDateTime(toRef(() => props.timestamp), props) |
154 | 137 | </script> |
0 commit comments