sanity-plugin-recurring-dates 1.2.0 → 1.3.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.
- package/dist/index.d.ts +3 -3
- package/dist/index.esm.js +57 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +56 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/RecurringDatesPreview.tsx +55 -0
- package/src/schema/recurringDates.tsx +11 -0
- package/src/types.ts +3 -3
package/package.json
CHANGED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT, format} from '@sanity/util/legacyDateFormat'
|
|
2
|
+
import {upperFirst} from 'lodash'
|
|
3
|
+
import React from 'react'
|
|
4
|
+
import {rrulestr} from 'rrule'
|
|
5
|
+
import type {ObjectSchemaType, PreviewProps} from 'sanity'
|
|
6
|
+
|
|
7
|
+
import type {PluginConfig, RecurringDate, WithRequiredProperty} from '../types'
|
|
8
|
+
|
|
9
|
+
type CastPreviewProps = PreviewProps &
|
|
10
|
+
RecurringDate & {
|
|
11
|
+
pluginConfig: WithRequiredProperty<PluginConfig, 'defaultRecurrences'>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type RecurringDateObjectSchemaType = Omit<ObjectSchemaType, 'options'> & {
|
|
15
|
+
options?: PluginConfig
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function RecurringDatesPreview(props: CastPreviewProps): React.JSX.Element {
|
|
19
|
+
const {startDate, endDate, rrule, schemaType, pluginConfig} = props
|
|
20
|
+
const options: RecurringDateObjectSchemaType = schemaType?.options
|
|
21
|
+
|
|
22
|
+
const {dateTimeOptions, dateOnly} = {
|
|
23
|
+
...pluginConfig,
|
|
24
|
+
...options,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const rule = rrule && rrulestr(rrule)
|
|
28
|
+
|
|
29
|
+
const dateFormat = dateTimeOptions?.dateFormat || DEFAULT_DATE_FORMAT
|
|
30
|
+
const timeFormat = dateTimeOptions?.timeFormat || DEFAULT_TIME_FORMAT
|
|
31
|
+
|
|
32
|
+
const start = startDate ? new Date(startDate) : undefined
|
|
33
|
+
const end = endDate ? new Date(endDate) : undefined
|
|
34
|
+
const sameDay = start && end && start.toDateString() === end.toDateString()
|
|
35
|
+
|
|
36
|
+
let title = 'No start date'
|
|
37
|
+
if (dateOnly) {
|
|
38
|
+
title = start ? format(start, dateFormat) : 'No start date'
|
|
39
|
+
if (end && !sameDay) {
|
|
40
|
+
title += ` - ${format(end, dateFormat)}`
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
title = start ? format(start, `${dateFormat} ${timeFormat}`) : 'No start date'
|
|
44
|
+
if (end) {
|
|
45
|
+
title += ` - ${format(end, sameDay ? timeFormat : `${dateFormat} ${timeFormat}`)}`
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const previewProps = {
|
|
50
|
+
title,
|
|
51
|
+
subtitle: rule && upperFirst(rule.toText()),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return props.renderDefault({...previewProps, ...props})
|
|
55
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import {CalendarIcon} from '@sanity/icons'
|
|
1
2
|
import {defineField, SchemaTypeDefinition} from 'sanity'
|
|
2
3
|
|
|
3
4
|
import {RecurringDates} from '../components/RecurringDate'
|
|
5
|
+
import {RecurringDatesPreview} from '../components/RecurringDatesPreview'
|
|
4
6
|
import {PluginConfig, WithRequiredProperty} from '../types'
|
|
5
7
|
|
|
6
8
|
export default (
|
|
@@ -12,6 +14,7 @@ export default (
|
|
|
12
14
|
name: 'recurringDates',
|
|
13
15
|
title: 'Dates',
|
|
14
16
|
type: 'object',
|
|
17
|
+
icon: CalendarIcon,
|
|
15
18
|
fields: [
|
|
16
19
|
defineField({
|
|
17
20
|
title: 'Start Date',
|
|
@@ -43,6 +46,14 @@ export default (
|
|
|
43
46
|
],
|
|
44
47
|
components: {
|
|
45
48
|
input: (props) => RecurringDates({...props, pluginConfig: config}),
|
|
49
|
+
preview: (props) => RecurringDatesPreview({...props, pluginConfig: config}),
|
|
50
|
+
},
|
|
51
|
+
preview: {
|
|
52
|
+
select: {
|
|
53
|
+
startDate: 'startDate',
|
|
54
|
+
endDate: 'endDate',
|
|
55
|
+
rrule: 'rrule',
|
|
56
|
+
},
|
|
46
57
|
},
|
|
47
58
|
})
|
|
48
59
|
}
|
package/src/types.ts
CHANGED