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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanity-plugin-recurring-dates",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Add a custom input component to your Sanity Studio to manage recurring dates (e.g. for events)",
5
5
  "keywords": [
6
6
  "sanity",
@@ -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
@@ -32,7 +32,7 @@ declare module 'sanity' {
32
32
  }
33
33
 
34
34
  export interface RecurringDate {
35
- rrule: string
36
- startDate: string
37
- endDate: string
35
+ rrule?: string
36
+ startDate?: string
37
+ endDate?: string
38
38
  }