Skip to content

Commit ed521f1

Browse files
committed
refactor(NcDateTime): migrate to Typescript and script-setup
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 3e0fc46 commit ed521f1

File tree

2 files changed

+36
-53
lines changed

2 files changed

+36
-53
lines changed

src/components/NcDateTime/NcDateTime.vue

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -96,59 +96,42 @@ h4 {
9696
v-text="formattedTime" />
9797
</template>
9898

99-
<script>
100-
import { computed } from 'vue'
99+
<script setup lang="ts">
100+
import { toRef } from 'vue'
101101
import { useFormatDateTime } from '../../composables/useFormatDateTime.ts'
102102
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)
154137
</script>
File renamed without changes.

0 commit comments

Comments
 (0)