sanity-plugin-recurring-dates 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +186 -0
  3. package/dist/index.d.ts +29 -0
  4. package/dist/index.esm.js +9173 -0
  5. package/dist/index.esm.js.map +1 -0
  6. package/dist/index.js +9197 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +93 -0
  9. package/sanity.json +8 -0
  10. package/src/components/CustomRule/CustomRule.tsx +230 -0
  11. package/src/components/CustomRule/Monthly.tsx +67 -0
  12. package/src/components/CustomRule/Weekly.tsx +62 -0
  13. package/src/components/CustomRule/index.tsx +1 -0
  14. package/src/components/DateInputs/CommonDateTimeInput.tsx +111 -0
  15. package/src/components/DateInputs/DateInput.tsx +81 -0
  16. package/src/components/DateInputs/DateTimeInput.tsx +122 -0
  17. package/src/components/DateInputs/base/DatePicker.tsx +34 -0
  18. package/src/components/DateInputs/base/DateTimeInput.tsx +104 -0
  19. package/src/components/DateInputs/base/LazyTextInput.tsx +77 -0
  20. package/src/components/DateInputs/base/calendar/Calendar.tsx +381 -0
  21. package/src/components/DateInputs/base/calendar/CalendarDay.tsx +47 -0
  22. package/src/components/DateInputs/base/calendar/CalendarMonth.tsx +52 -0
  23. package/src/components/DateInputs/base/calendar/YearInput.tsx +22 -0
  24. package/src/components/DateInputs/base/calendar/constants.ts +33 -0
  25. package/src/components/DateInputs/base/calendar/features.ts +4 -0
  26. package/src/components/DateInputs/base/calendar/utils.ts +34 -0
  27. package/src/components/DateInputs/index.ts +2 -0
  28. package/src/components/DateInputs/types.ts +4 -0
  29. package/src/components/DateInputs/utils.ts +4 -0
  30. package/src/components/RecurringDate.tsx +139 -0
  31. package/src/constants.ts +36 -0
  32. package/src/index.ts +2 -0
  33. package/src/plugin.tsx +19 -0
  34. package/src/schema/recurringDates.tsx +44 -0
  35. package/src/types.ts +27 -0
  36. package/src/utils.ts +16 -0
  37. package/v2-incompatible.js +11 -0
@@ -0,0 +1,139 @@
1
+ import {Box, Flex, Grid, Select, Stack, Text} from '@sanity/ui'
2
+ import {upperFirst} from 'lodash'
3
+ import React, {useCallback, useState} from 'react'
4
+ import {rrulestr} from 'rrule'
5
+ import {ObjectInputMember, type ObjectInputProps, set} from 'sanity'
6
+ import {Feedback} from 'sanity-plugin-utils'
7
+
8
+ import type {PluginConfig, WithRequiredProperty} from '../types'
9
+ import {validateRRuleStrings} from '../utils'
10
+ import {CustomRule} from './CustomRule'
11
+
12
+ type RecurringDatesProps = ObjectInputProps & {
13
+ pluginConfig: WithRequiredProperty<PluginConfig, 'defaultRecurrences'>
14
+ }
15
+
16
+ export function RecurringDates(props: RecurringDatesProps) {
17
+ const {onChange, members, value: currentValue, schemaType, pluginConfig} = props
18
+ const {options}: {options?: PluginConfig} = schemaType
19
+ const {defaultRecurrences, hideEndDate, hideCustom, dateTimeOptions} = {
20
+ ...pluginConfig,
21
+ ...options,
22
+ }
23
+
24
+ // For the custom RRULE modal
25
+ const [open, setOpen] = useState(false)
26
+ const onClose = useCallback(() => setOpen(false), [])
27
+ const onOpen = useCallback(() => setOpen(true), [])
28
+
29
+ // Update the RRULE field when the select changes
30
+ const handleChange = useCallback(
31
+ (event: React.ChangeEvent<HTMLSelectElement>) => {
32
+ const {value} = event.currentTarget
33
+
34
+ if (value == 'custom') {
35
+ // open modal...
36
+ onOpen()
37
+ } else {
38
+ // update the field
39
+ onChange(set(value, ['rrule']))
40
+ }
41
+ },
42
+ [onChange, onOpen],
43
+ )
44
+
45
+ // Validate the default recurrences
46
+ const invalidRecurrences = validateRRuleStrings(defaultRecurrences)
47
+
48
+ // Grab the fields we want to output from the members array
49
+ const startDateMember = members.find(
50
+ (member) => member.kind === 'field' && member.name === 'startDate',
51
+ )
52
+ const endDateMember = members.find(
53
+ (member) => member.kind === 'field' && member.name === 'endDate',
54
+ )
55
+ const rruleMember = members.find((member) => member.kind === 'field' && member.name === 'rrule')
56
+
57
+ // If we have a custom RRULE, add it to the list of options
58
+ const availableRecurrences = [...defaultRecurrences]
59
+ if (currentValue && !availableRecurrences.includes(currentValue?.rrule)) {
60
+ availableRecurrences.push(currentValue?.rrule)
61
+ }
62
+
63
+ // Pass along functions to each member so that it knows how to render
64
+ const renderProps = {
65
+ renderField: props.renderField,
66
+ renderInput: props.renderInput,
67
+ renderItem: props.renderItem,
68
+ renderPreview: props.renderPreview,
69
+ }
70
+
71
+ if (startDateMember?.kind == 'field') {
72
+ startDateMember.field.schemaType.options = {
73
+ ...startDateMember?.field?.schemaType.options,
74
+ ...dateTimeOptions,
75
+ }
76
+ }
77
+
78
+ if (endDateMember?.kind == 'field') {
79
+ endDateMember.field.schemaType.options = {
80
+ ...endDateMember?.field?.schemaType.options,
81
+ ...dateTimeOptions,
82
+ }
83
+ }
84
+
85
+ return (
86
+ <Stack space={3}>
87
+ <Grid columns={hideEndDate ? 1 : 2} gap={3}>
88
+ <Flex align="flex-end" gap={2}>
89
+ <Box flex={1}>
90
+ {startDateMember && <ObjectInputMember member={startDateMember} {...renderProps} />}
91
+ </Box>
92
+ </Flex>
93
+ {!hideEndDate && (
94
+ <Flex align="flex-end" gap={2}>
95
+ <Box flex={1}>
96
+ {endDateMember && <ObjectInputMember member={endDateMember} {...renderProps} />}
97
+ </Box>
98
+ </Flex>
99
+ )}
100
+ </Grid>
101
+ {invalidRecurrences ? (
102
+ <Feedback tone="critical">
103
+ <Text size={1}>
104
+ <strong>Error:</strong> An invalid RRULE string was provided in the{' '}
105
+ <code>defaultRecurrences</code> array. Check plugin configuration.
106
+ </Text>
107
+ </Feedback>
108
+ ) : (
109
+ <Select onChange={handleChange} value={currentValue?.rrule}>
110
+ <option value="">Doesn't repeat</option>
111
+ {availableRecurrences.map((recurrence) => {
112
+ if (!recurrence) {
113
+ return null
114
+ }
115
+ const rule = rrulestr(recurrence)
116
+
117
+ return (
118
+ <option key={recurrence} value={recurrence}>
119
+ {upperFirst(rule.toText())}
120
+ </option>
121
+ )
122
+ })}
123
+ {!hideCustom && <option value="custom">Custom...</option>}
124
+ </Select>
125
+ )}
126
+ {rruleMember && <ObjectInputMember member={rruleMember} {...renderProps} />}
127
+ <CustomRule
128
+ open={open}
129
+ onClose={onClose}
130
+ onChange={onChange}
131
+ initialValue={currentValue?.rrule}
132
+ startDate={
133
+ startDateMember?.kind == 'field' ? (startDateMember?.field?.value as string) : undefined
134
+ }
135
+ dateTimeOptions={dateTimeOptions}
136
+ />
137
+ </Stack>
138
+ )
139
+ }
@@ -0,0 +1,36 @@
1
+ import {PluginConfig, WithRequiredProperty} from './types'
2
+
3
+ const DEFAULT_RECURRENCES = [
4
+ 'RRULE:FREQ=DAILY;INTERVAL=1',
5
+ 'RRULE:FREQ=WEEKLY;INTERVAL=1',
6
+ 'RRULE:FREQ=MONTHLY;INTERVAL=1',
7
+ 'RRULE:FREQ=YEARLY;INTERVAL=1',
8
+ ]
9
+
10
+ export const DEFAULT_CONFIG: WithRequiredProperty<PluginConfig, 'defaultRecurrences'> = {
11
+ defaultRecurrences: DEFAULT_RECURRENCES,
12
+ }
13
+
14
+ export const MONTHS = [
15
+ 'Jan',
16
+ 'Feb',
17
+ 'Mar',
18
+ 'Apr',
19
+ 'May',
20
+ 'Jun',
21
+ 'Jul',
22
+ 'Aug',
23
+ 'Sep',
24
+ 'Oct',
25
+ 'Nov',
26
+ 'Dec',
27
+ ]
28
+
29
+ export const DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
30
+
31
+ export const DEFAULT_COUNTS = [
32
+ 5, // yearly
33
+ 12, // monthly
34
+ 12, // weekly
35
+ 30, // daily
36
+ ]
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export {recurringDates} from './plugin'
2
+ export * from './types'
package/src/plugin.tsx ADDED
@@ -0,0 +1,19 @@
1
+ import {definePlugin} from 'sanity'
2
+
3
+ import {DEFAULT_CONFIG} from './constants'
4
+ import recurringDateSchema from './schema/recurringDates'
5
+ import {PluginConfig, WithRequiredProperty} from './types'
6
+
7
+ export const recurringDates = definePlugin<PluginConfig>((config) => {
8
+ const pluginConfig: WithRequiredProperty<PluginConfig, 'defaultRecurrences'> = {
9
+ ...DEFAULT_CONFIG,
10
+ ...config,
11
+ }
12
+
13
+ return {
14
+ name: 'sanity-plugin-recurring-dates',
15
+ schema: {
16
+ types: [recurringDateSchema(pluginConfig)],
17
+ },
18
+ }
19
+ })
@@ -0,0 +1,44 @@
1
+ import {defineField} from 'sanity'
2
+
3
+ import {RecurringDates} from '../components/RecurringDate'
4
+ import {PluginConfig, WithRequiredProperty} from '../types'
5
+
6
+ export default (config: WithRequiredProperty<PluginConfig, 'defaultRecurrences'>) => {
7
+ const {hideEndDate, dateTimeOptions} = config
8
+
9
+ return defineField({
10
+ name: 'recurringDates',
11
+ title: 'Dates',
12
+ type: 'object',
13
+ fields: [
14
+ defineField({
15
+ title: 'Start Date',
16
+ name: 'startDate',
17
+ type: 'datetime',
18
+ options: dateTimeOptions,
19
+ validation: (Rule) => Rule.required(),
20
+ }),
21
+ defineField({
22
+ title: 'End Date',
23
+ name: 'endDate',
24
+ type: 'datetime',
25
+ options: dateTimeOptions,
26
+ validation: (Rule) => Rule.min(Rule.valueOfField('startDate')),
27
+ }),
28
+ defineField({
29
+ title: 'Recurring event',
30
+ name: 'recurs',
31
+ type: 'boolean',
32
+ }),
33
+ defineField({
34
+ title: 'RRULE',
35
+ name: 'rrule',
36
+ type: 'string',
37
+ hidden: true,
38
+ }),
39
+ ].filter((field) => !(field.name === 'endDate' && hideEndDate)),
40
+ components: {
41
+ input: (props) => RecurringDates({...props, pluginConfig: config}),
42
+ },
43
+ })
44
+ }
package/src/types.ts ADDED
@@ -0,0 +1,27 @@
1
+ import type {DatetimeOptions, ObjectDefinition} from 'sanity'
2
+
3
+ export interface PluginConfig {
4
+ defaultRecurrences?: string[]
5
+ hideEndDate?: boolean
6
+ hideCustom?: boolean
7
+ dateTimeOptions?: DatetimeOptions
8
+ }
9
+
10
+ export type WithRequiredProperty<Type, Key extends keyof Type> = Type & {
11
+ [Property in Key]-?: Type[Property]
12
+ }
13
+
14
+ // create a new schema definition based on object (we remove the ability to assign field, change the type add some options)
15
+ export type RecurringDateFieldOptions = Omit<ObjectDefinition, 'type' | 'fields'> & {
16
+ type: 'recurringDates'
17
+ options?: PluginConfig
18
+ }
19
+
20
+ // redeclares sanity module so we can add interfaces props to it
21
+ declare module 'sanity' {
22
+ // redeclares IntrinsicDefinitions and adds a named definition to it
23
+ // it is important that the key is the same as the type in the definition ('magically-added-type')
24
+ export interface IntrinsicDefinitions {
25
+ recurringDates: RecurringDateFieldOptions
26
+ }
27
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,16 @@
1
+ import {rrulestr} from 'rrule'
2
+
3
+ export const validateRRuleString = (recurrence: string): boolean => {
4
+ try {
5
+ rrulestr(recurrence)
6
+ return false
7
+ } catch (e) {
8
+ return true
9
+ }
10
+ }
11
+
12
+ export const validateRRuleStrings = (recurrences: string[]): boolean => {
13
+ return recurrences.some((recurrence) => {
14
+ return validateRRuleString(recurrence)
15
+ })
16
+ }
@@ -0,0 +1,11 @@
1
+ const {showIncompatiblePluginDialog} = require('@sanity/incompatible-plugin')
2
+ const {name, version, sanityExchangeUrl} = require('./package.json')
3
+
4
+ export default showIncompatiblePluginDialog({
5
+ name: name,
6
+ versions: {
7
+ v3: version,
8
+ v2: undefined,
9
+ },
10
+ sanityExchangeUrl,
11
+ })