Skip to content

Commit dd33ece

Browse files
committed
feat: core in typescript
- use vite-plugin-dts to generate type declarations - add RenderlessSelect - bugfixes
1 parent 2f8a214 commit dd33ece

File tree

11 files changed

+343
-71
lines changed

11 files changed

+343
-71
lines changed

core-ts/package.json

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
{
2-
"name": "core-ts",
3-
"version": "0.0.0",
4-
"private": true,
2+
"name": "@vue-js-cron/core-ts",
3+
"version": "3.7.1",
4+
"description": "A renderless Vue.js cron editor.",
5+
"repository": "https://github.com/abichinger/vue-js-cron",
6+
"author": "Andreas Bichinger",
7+
"license": "MIT",
8+
"private": false,
9+
"type": "module",
10+
"main": "./dist/core.umd.cjs",
11+
"module": "./dist/core.js",
12+
"types": "./dist/index.d.ts",
13+
"exports": {
14+
".": {
15+
"import": "./dist/core.js",
16+
"require": "./dist/core.umd.cjs"
17+
}
18+
},
519
"scripts": {
620
"dev": "vite",
721
"build": "run-p type-check \"build-only {@}\" --",
@@ -34,7 +48,22 @@
3448
"prettier": "^3.0.3",
3549
"typescript": "~5.2.0",
3650
"vite": "^4.4.9",
51+
"vite-plugin-dts": "^3.6.0",
3752
"vitest": "^0.34.4",
3853
"vue-tsc": "^1.8.11"
39-
}
40-
}
54+
},
55+
"files": [
56+
"package.json",
57+
"dist",
58+
"README.md"
59+
],
60+
"keywords": [
61+
"vue",
62+
"vue.js",
63+
"vue component",
64+
"cron",
65+
"cron expression",
66+
"cron editor",
67+
"renderless"
68+
]
69+
}

core-ts/src/App.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import { useCronComponent } from './components/cron-core'
5454
5555
export default {
5656
components: {
57-
'CronCore': useCronComponent()
57+
CronCore: useCronComponent()
5858
},
5959
props: {},
6060
data() {
@@ -77,4 +77,3 @@ export default {
7777
}
7878
}
7979
</script>
80-
./components/core
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { nextTick, watch } from 'vue'
3+
import { useSelect } from '../select'
4+
5+
describe('select', () => {
6+
it('useSelect updates', async () => {
7+
const items = [
8+
{
9+
value: 0,
10+
text: 'zero'
11+
},
12+
{
13+
value: 1,
14+
text: 'one'
15+
}
16+
]
17+
18+
const s = useSelect({
19+
items: items
20+
})
21+
22+
let counter = 0
23+
const zero = items[0]
24+
const one = items[1]
25+
26+
watch(s.updated, () => {
27+
counter++
28+
})
29+
30+
s.add(zero)
31+
await nextTick()
32+
33+
s.add(one)
34+
await nextTick()
35+
36+
expect(s.has(one)).toEqual(true)
37+
38+
s.remove(one)
39+
await nextTick()
40+
41+
expect(s.has(one)).toEqual(false)
42+
expect(counter).toEqual(3)
43+
})
44+
})

core-ts/src/components/cron-core.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,27 @@ export function useCron(options: CronOptions) {
5656
const cronDefaults = new DefaultCronOptions()
5757

5858
const locale = options.locale ?? 'en'
59-
const {
60-
customLocale,
61-
fields = cronDefaults.fields(locale),
62-
periods = cronDefaults.periods()
63-
} = options
59+
const { customLocale, fields = cronDefaults.fields(locale) } = options
6460
const initialValue = options.initialValue ?? cronDefaults.initialValue(fields.length)
6561

6662
const l10n = getLocale(locale, customLocale)
63+
const periods = (options.periods ?? cronDefaults.periods()).map((p) => {
64+
return {
65+
...p,
66+
text: l10n.getLocaleStr(p.id, TextPosition.Text)
67+
}
68+
})
6769

6870
const cron = ref(initialValue)
6971
const error = ref('')
7072
const period = ref(periods[periods.length - 1])
71-
const periodText = ref('')
7273
const periodPrefix = ref('')
7374
const periodSuffix = ref('')
7475
const segments = fields.map((f) => {
7576
return useCronSegment({ field: new FieldWrapper(f), locale: l10n, period })
7677
})
7778

78-
const segmentMap = new Map(segments.map((s) => [s.field.id, s]))
79+
const segmentMap = new Map(segments.map((s) => [s.id, s]))
7980
const selected = computed(() => {
8081
return period.value.value.map((fieldId) => {
8182
const segment = segmentMap.get(fieldId)
@@ -111,7 +112,7 @@ export function useCron(options: CronOptions) {
111112
const toCron = () => {
112113
cron.value = segments
113114
.map((s) => {
114-
return period.value.value.includes(s.field.id) ? s.cron.value : '*'
115+
return period.value.value.includes(s.id) ? s.cron.value : '*'
115116
})
116117
.join(' ')
117118
}
@@ -123,10 +124,16 @@ export function useCron(options: CronOptions) {
123124
translate()
124125

125126
watch(cron, fromCron)
126-
watch(period, translate)
127+
watch(period, () => {
128+
toCron()
129+
translate()
130+
})
127131

128132
segments.forEach((s) => {
129133
watch(s.cron, toCron)
134+
watch(s.error, (value) => {
135+
error.value = value
136+
})
130137
})
131138

132139
return {
@@ -143,17 +150,10 @@ export function useCron(options: CronOptions) {
143150
period.value = periods[i]
144151
},
145152
selected: period,
146-
items: periods.map((p) => {
147-
return {
148-
...p,
149-
text: l10n.getLocaleStr(p.id, TextPosition.Text)
150-
}
151-
}),
152-
text: periodText,
153+
items: periods,
153154
prefix: periodPrefix,
154155
suffix: periodSuffix
155-
},
156-
fields
156+
}
157157
}
158158
}
159159

@@ -205,8 +205,8 @@ export function useCronComponent() {
205205
error: error,
206206
fields: selected.value.map((s) => {
207207
return {
208-
id: s.field.id,
209-
items: s.field.items,
208+
id: s.id,
209+
items: s.items,
210210
cron: s.cron.value,
211211
selectedStr: s.text.value,
212212
events: {

core-ts/src/components/cron-segment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ export function useCronSegment(options: FieldOptions) {
8282
})
8383

8484
return {
85-
field,
85+
id: field.id,
86+
items: field.items,
8687
cron,
8788
selected,
8889
error,

0 commit comments

Comments
 (0)