Skip to content

Commit a4cb6d8

Browse files
committed
feat(core): add setupCron and setupSelect
1 parent 54b2443 commit a4cb6d8

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

core/src/components/cron-core.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { AnySegment, EverySegment, NoSpecificSegment, RangeSegment, ValueSegment } from '@/cron'
22
import type { Localization } from '@/locale/types'
3-
import { computed, defineComponent, ref, watch, type ExtractPropTypes, type PropType } from 'vue'
3+
import {
4+
computed,
5+
defineComponent,
6+
ref,
7+
watch,
8+
type ExtractPropTypes,
9+
type PropType,
10+
type SetupContext,
11+
type WatchSource,
12+
} from 'vue'
413
import { getLocale } from '../locale'
514
import { FieldWrapper, TextPosition, type CronFormat, type Field, type Period } from '../types'
615
import { defaultItems } from '../util'
@@ -220,6 +229,34 @@ export function useCron(options: CronOptions) {
220229
}
221230
}
222231

232+
export function setupCron(
233+
options: CronOptions,
234+
modelValue: WatchSource<string | undefined>,
235+
{ emit }: SetupContext<['update:model-value', 'error']>,
236+
) {
237+
const cron = useCron(options)
238+
239+
watch(
240+
modelValue,
241+
(value) => {
242+
if (value) {
243+
cron.cron.value = value
244+
}
245+
},
246+
{ immediate: true },
247+
)
248+
249+
watch(cron.cron, (value) => {
250+
emit('update:model-value', value)
251+
})
252+
253+
watch(cron.error, (error) => {
254+
emit('error', error)
255+
})
256+
257+
return cron
258+
}
259+
223260
export const cronCoreProps = () => ({
224261
/**
225262
* The value of the cron expression

core/src/components/select.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { computed, defineComponent, ref, watch, type PropType, type Ref } from 'vue'
1+
import {
2+
computed,
3+
defineComponent,
4+
ref,
5+
watch,
6+
type PropType,
7+
type Ref,
8+
type SetupContext,
9+
type WatchSource,
10+
} from 'vue'
211
import { splitArray } from '..'
312

413
interface SetOptions<T> {
@@ -173,6 +182,30 @@ export function useSelect<T, V>(options: SelectOptions<T, V>) {
173182
}
174183
}
175184

185+
export function setupSelect<T, V>(
186+
options: SelectOptions<T, V>,
187+
modelValue: WatchSource<V>,
188+
{ emit }: SetupContext<['update:model-value']>,
189+
) {
190+
const s = useSelect(options)
191+
192+
watch(s.selected, () => {
193+
emit('update:model-value', s.selected.value)
194+
})
195+
196+
watch(
197+
modelValue,
198+
(value) => {
199+
if (value) {
200+
s.setValues(value)
201+
}
202+
},
203+
{ immediate: true },
204+
)
205+
206+
return s
207+
}
208+
176209
export const RenderlessSelect = defineComponent({
177210
name: 'RenderlessSelect',
178211
props: {

core/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ import { CronCore } from './components/cron-core'
44
export {
55
CronCore,
66
cronCoreProps,
7+
setupCron,
78
useCron,
89
type CronContext,
910
type CronCoreProps,
1011
type CronOptions,
1112
} from './components/cron-core'
12-
export { RenderlessSelect, selectProps, useSelect, type SelectOptions } from './components/select'
13+
export {
14+
RenderlessSelect,
15+
selectProps,
16+
setupSelect,
17+
useSelect,
18+
type SelectOptions,
19+
} from './components/select'
1320
export { Locale, getLocale } from './locale'
1421
export type * from './locale/types'
1522
export type * from './types'

0 commit comments

Comments
 (0)