sanity-plugin-recurring-dates 1.1.0 → 1.2.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/README.md +6 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.esm.js +302 -4864
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +306 -4868
- package/dist/index.js.map +1 -1
- package/package.json +27 -26
- package/src/components/CustomRule/CustomRule.tsx +100 -107
- package/src/components/CustomRule/Monthly.tsx +1 -1
- package/src/components/CustomRule/Weekly.tsx +1 -1
- package/src/components/DateInputs/CommonDateTimeInput.tsx +5 -4
- package/src/components/DateInputs/DateInput.tsx +1 -1
- package/src/components/DateInputs/DateTimeInput.tsx +1 -1
- package/src/components/DateInputs/base/DatePicker.tsx +4 -2
- package/src/components/DateInputs/base/DateTimeInput.tsx +8 -4
- package/src/components/DateInputs/base/LazyTextInput.tsx +13 -14
- package/src/components/DateInputs/base/calendar/Calendar.tsx +18 -16
- package/src/components/DateInputs/base/calendar/CalendarDay.tsx +1 -1
- package/src/components/DateInputs/base/calendar/CalendarMonth.tsx +3 -2
- package/src/components/DateInputs/base/calendar/YearInput.tsx +4 -3
- package/src/components/RecurringDate.tsx +21 -4
- package/src/components/RemoveEndDate.tsx +3 -2
- package/src/schema/recurringDates.tsx +9 -5
- package/src/types.ts +6 -2
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {Box, Grid, Text} from '@sanity/ui'
|
|
2
2
|
import {isSameDay, isSameMonth} from 'date-fns'
|
|
3
3
|
import React from 'react'
|
|
4
|
+
|
|
4
5
|
import {CalendarDay} from './CalendarDay'
|
|
5
6
|
import {WEEK_DAY_NAMES} from './constants'
|
|
6
7
|
import {getWeeksOfMonth} from './utils'
|
|
@@ -13,7 +14,7 @@ interface CalendarMonthProps {
|
|
|
13
14
|
hidden?: boolean
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
export function CalendarMonth(props: CalendarMonthProps) {
|
|
17
|
+
export function CalendarMonth(props: CalendarMonthProps): React.JSX.Element {
|
|
17
18
|
return (
|
|
18
19
|
<Box aria-hidden={props.hidden || false} data-ui="CalendarMonth">
|
|
19
20
|
<Grid gap={1} style={{gridTemplateColumns: 'repeat(7, minmax(44px, 46px))'}}>
|
|
@@ -44,7 +45,7 @@ export function CalendarMonth(props: CalendarMonthProps) {
|
|
|
44
45
|
selected={selected}
|
|
45
46
|
/>
|
|
46
47
|
)
|
|
47
|
-
})
|
|
48
|
+
}),
|
|
48
49
|
)}
|
|
49
50
|
</Grid>
|
|
50
51
|
</Box>
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import React from 'react'
|
|
2
1
|
import {TextInput} from '@sanity/ui'
|
|
2
|
+
import React from 'react'
|
|
3
|
+
|
|
3
4
|
import {LazyTextInput} from '../LazyTextInput'
|
|
4
5
|
|
|
5
6
|
type Props = Omit<React.ComponentProps<typeof TextInput>, 'onChange' | 'value'> & {
|
|
@@ -7,7 +8,7 @@ type Props = Omit<React.ComponentProps<typeof TextInput>, 'onChange' | 'value'>
|
|
|
7
8
|
onChange: (year: number) => void
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
export const YearInput = ({onChange, ...props}: Props) => {
|
|
11
|
+
export const YearInput = ({onChange, ...props}: Props): React.JSX.Element => {
|
|
11
12
|
const handleChange = React.useCallback(
|
|
12
13
|
(event: React.FocusEvent<HTMLInputElement> | React.ChangeEvent<HTMLInputElement>) => {
|
|
13
14
|
const numericValue = parseInt(event.currentTarget.value, 10)
|
|
@@ -15,7 +16,7 @@ export const YearInput = ({onChange, ...props}: Props) => {
|
|
|
15
16
|
onChange(numericValue)
|
|
16
17
|
}
|
|
17
18
|
},
|
|
18
|
-
[onChange]
|
|
19
|
+
[onChange],
|
|
19
20
|
)
|
|
20
21
|
|
|
21
22
|
return <LazyTextInput {...props} onChange={handleChange} inputMode="numeric" />
|
|
@@ -2,7 +2,13 @@ import {Box, Flex, Grid, Select, Stack, Text} from '@sanity/ui'
|
|
|
2
2
|
import {upperFirst} from 'lodash'
|
|
3
3
|
import React, {useCallback, useState} from 'react'
|
|
4
4
|
import {rrulestr} from 'rrule'
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
ObjectInputMember,
|
|
7
|
+
type ObjectInputProps,
|
|
8
|
+
type ObjectSchemaType,
|
|
9
|
+
type Rule,
|
|
10
|
+
set,
|
|
11
|
+
} from 'sanity'
|
|
6
12
|
import {Feedback} from 'sanity-plugin-utils'
|
|
7
13
|
|
|
8
14
|
import type {PluginConfig, WithRequiredProperty} from '../types'
|
|
@@ -18,10 +24,10 @@ type RecurringDateObjectSchemaType = Omit<ObjectSchemaType, 'options'> & {
|
|
|
18
24
|
options?: PluginConfig
|
|
19
25
|
}
|
|
20
26
|
|
|
21
|
-
export function RecurringDates(props: RecurringDatesProps) {
|
|
27
|
+
export function RecurringDates(props: RecurringDatesProps): React.JSX.Element {
|
|
22
28
|
const {onChange, members, value: currentValue, schemaType, pluginConfig} = props
|
|
23
29
|
const {options, title}: RecurringDateObjectSchemaType = schemaType
|
|
24
|
-
const {defaultRecurrences, hideEndDate, hideCustom, dateTimeOptions, dateOnly} = {
|
|
30
|
+
const {defaultRecurrences, hideEndDate, hideCustom, dateTimeOptions, dateOnly, validation} = {
|
|
25
31
|
...pluginConfig,
|
|
26
32
|
...options,
|
|
27
33
|
}
|
|
@@ -104,8 +110,19 @@ export function RecurringDates(props: RecurringDatesProps) {
|
|
|
104
110
|
}
|
|
105
111
|
}
|
|
106
112
|
|
|
113
|
+
// Add custom validation to the start and end date fields
|
|
114
|
+
if (validation?.startDate && startDateMember?.kind == 'field') {
|
|
115
|
+
startDateMember.field.schemaType.validation = (CustomValidation) =>
|
|
116
|
+
validation?.startDate?.(CustomValidation) as Rule
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (validation?.endDate && endDateMember?.kind == 'field') {
|
|
120
|
+
endDateMember.field.schemaType.validation = (CustomValidation) =>
|
|
121
|
+
validation?.endDate?.(CustomValidation) as Rule
|
|
122
|
+
}
|
|
123
|
+
|
|
107
124
|
// Do we have an end date set for this field?
|
|
108
|
-
const hasEndDate = currentValue
|
|
125
|
+
const hasEndDate = currentValue?.endDate
|
|
109
126
|
|
|
110
127
|
return (
|
|
111
128
|
<Stack space={3}>
|
|
@@ -2,6 +2,7 @@ import {TrashIcon, WarningOutlineIcon} from '@sanity/icons'
|
|
|
2
2
|
import {Box, Button, Card, Flex, Stack, Text} from '@sanity/ui'
|
|
3
3
|
import {upperFirst} from 'lodash'
|
|
4
4
|
import {useCallback} from 'react'
|
|
5
|
+
import React from 'react'
|
|
5
6
|
import {type ObjectInputProps, unset} from 'sanity'
|
|
6
7
|
|
|
7
8
|
export function RemoveEndDate({
|
|
@@ -10,7 +11,7 @@ export function RemoveEndDate({
|
|
|
10
11
|
}: {
|
|
11
12
|
title?: string
|
|
12
13
|
onChange: ObjectInputProps['onChange']
|
|
13
|
-
}) {
|
|
14
|
+
}): React.JSX.Element {
|
|
14
15
|
// Update the RRULE field when the select changes
|
|
15
16
|
const handleUnsetClick = useCallback(() => {
|
|
16
17
|
onChange(unset(['endDate']))
|
|
@@ -39,7 +40,7 @@ export function RemoveEndDate({
|
|
|
39
40
|
tone="critical"
|
|
40
41
|
text={<>Remove end date</>}
|
|
41
42
|
onClick={handleUnsetClick}
|
|
42
|
-
width="
|
|
43
|
+
width="fill"
|
|
43
44
|
/>
|
|
44
45
|
</Stack>
|
|
45
46
|
</Flex>
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {defineField} from 'sanity'
|
|
1
|
+
import {defineField, SchemaTypeDefinition} from 'sanity'
|
|
2
2
|
|
|
3
3
|
import {RecurringDates} from '../components/RecurringDate'
|
|
4
4
|
import {PluginConfig, WithRequiredProperty} from '../types'
|
|
5
5
|
|
|
6
|
-
export default (
|
|
7
|
-
|
|
6
|
+
export default (
|
|
7
|
+
config: WithRequiredProperty<PluginConfig, 'defaultRecurrences'>,
|
|
8
|
+
): SchemaTypeDefinition => {
|
|
9
|
+
const {dateTimeOptions, dateOnly, validation} = config
|
|
8
10
|
|
|
9
11
|
return defineField({
|
|
10
12
|
name: 'recurringDates',
|
|
@@ -16,14 +18,16 @@ export default (config: WithRequiredProperty<PluginConfig, 'defaultRecurrences'>
|
|
|
16
18
|
name: 'startDate',
|
|
17
19
|
type: dateOnly ? 'date' : 'datetime',
|
|
18
20
|
options: dateTimeOptions,
|
|
19
|
-
validation: (Rule) =>
|
|
21
|
+
validation: (Rule) =>
|
|
22
|
+
validation?.startDate ? validation.startDate(Rule) : Rule.required(),
|
|
20
23
|
}),
|
|
21
24
|
defineField({
|
|
22
25
|
title: 'End Date',
|
|
23
26
|
name: 'endDate',
|
|
24
27
|
type: dateOnly ? 'date' : 'datetime',
|
|
25
28
|
options: dateTimeOptions,
|
|
26
|
-
validation: (Rule) =>
|
|
29
|
+
validation: (Rule) =>
|
|
30
|
+
validation?.endDate ? validation.endDate(Rule) : Rule.min(Rule.valueOfField('startDate')),
|
|
27
31
|
}),
|
|
28
32
|
defineField({
|
|
29
33
|
title: 'Recurring event',
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {DatetimeOptions, ObjectDefinition} from 'sanity'
|
|
1
|
+
import type {DateRule, DatetimeOptions, ObjectDefinition} from 'sanity'
|
|
2
2
|
|
|
3
3
|
export interface PluginConfig {
|
|
4
4
|
defaultRecurrences?: string[]
|
|
@@ -6,6 +6,10 @@ export interface PluginConfig {
|
|
|
6
6
|
hideCustom?: boolean
|
|
7
7
|
dateTimeOptions?: DatetimeOptions
|
|
8
8
|
dateOnly?: boolean
|
|
9
|
+
validation?: {
|
|
10
|
+
startDate?: (Rule: DateRule) => DateRule
|
|
11
|
+
endDate?: (Rule: DateRule) => DateRule
|
|
12
|
+
}
|
|
9
13
|
}
|
|
10
14
|
|
|
11
15
|
export type WithRequiredProperty<Type, Key extends keyof Type> = Type & {
|
|
@@ -30,5 +34,5 @@ declare module 'sanity' {
|
|
|
30
34
|
export interface RecurringDate {
|
|
31
35
|
rrule: string
|
|
32
36
|
startDate: string
|
|
33
|
-
endDate:
|
|
37
|
+
endDate: string
|
|
34
38
|
}
|