Skip to content

Commit

Permalink
Merge pull request #1138 from interval/null-default-value
Browse files Browse the repository at this point in the history
Accept `null` for `defaultValue` in addition to `undefined`
  • Loading branch information
jacobmischka authored Mar 15, 2023
2 parents ddbe0b9 + be5cd30 commit a10d02f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/classes/IOComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface ComponentInstance<MN extends keyof IoSchema> {
isMultiple?: boolean
validationErrorMessage?: string | undefined
multipleProps?: {
defaultValue?: T_IO_RETURNS<MN>[]
defaultValue?: T_IO_RETURNS<MN>[] | null
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ export default class IOComponent<MethodName extends T_IO_METHOD_NAMES> {
MaybeMultipleComponentReturnValue<MethodName> | undefined
>
multipleProps?: {
defaultValue?: T_IO_RETURNS<MethodName>[]
defaultValue?: T_IO_RETURNS<MethodName>[] | null
}
displayResolvesImmediately?: boolean
}) {
Expand Down
16 changes: 8 additions & 8 deletions src/classes/IOPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export class MultipleableIOPromise<
Props extends T_IO_PROPS<MethodName> = T_IO_PROPS<MethodName>,
Output = ComponentReturnValue<MethodName>,
DefaultValue = T_IO_PROPS<MethodName> extends { defaultValue?: any }
? Output
? Output | null
: never
> extends InputIOPromise<MethodName, Props, Output> {
defaultValueGetter:
Expand Down Expand Up @@ -318,9 +318,9 @@ export class MultipleableIOPromise<
multiple({
defaultValue,
}: {
defaultValue?: DefaultValue[]
defaultValue?: DefaultValue[] | null
} = {}): MultipleIOPromise<MethodName, Props, Output> {
let transformedDefaultValue: T_IO_RETURNS<MethodName>[] | undefined
let transformedDefaultValue: T_IO_RETURNS<MethodName>[] | undefined | null
const propsSchema = ioSchema[this.methodName].props
if (defaultValue && 'defaultValue' in propsSchema.shape) {
const { defaultValueGetter } = this
Expand All @@ -331,7 +331,7 @@ export class MultipleableIOPromise<
try {
const defaultValueSchema = propsSchema.shape.defaultValue
transformedDefaultValue = z
.array(defaultValueSchema.unwrap())
.array(defaultValueSchema.unwrap().unwrap())
.parse(potentialDefaultValue)
} catch (err) {
console.error(
Expand Down Expand Up @@ -362,14 +362,14 @@ export class MultipleIOPromise<
getSingleValue:
| ((response: ComponentReturnValue<MethodName>) => Output)
| undefined
defaultValue: T_IO_RETURNS<MethodName>[] | undefined
defaultValue: T_IO_RETURNS<MethodName>[] | undefined | null

constructor({
defaultValue,
valueGetter,
...rest
}: {
defaultValue?: T_IO_RETURNS<MethodName>[]
defaultValue?: T_IO_RETURNS<MethodName>[] | null
renderer: ComponentRenderer<MethodName>
methodName: MethodName
label: string
Expand Down Expand Up @@ -485,14 +485,14 @@ export class OptionalMultipleIOPromise<
getSingleValue:
| ((response: ComponentReturnValue<MethodName>) => Output)
| undefined
defaultValue: T_IO_RETURNS<MethodName>[] | undefined
defaultValue: T_IO_RETURNS<MethodName>[] | undefined | null

constructor({
defaultValue,
valueGetter,
...rest
}: {
defaultValue?: T_IO_RETURNS<MethodName>[]
defaultValue?: T_IO_RETURNS<MethodName>[] | null
renderer: ComponentRenderer<MethodName>
methodName: MethodName
label: string
Expand Down
12 changes: 8 additions & 4 deletions src/examples/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ const interval = new Interval({
let i = 0

while (i < 2) {
await io.group([
const responses = await io.group([
io.input
.text('Empty text input', {
placeholder: 'Text goes here',
Expand All @@ -742,7 +742,7 @@ const interval = new Interval({
defaultValue: new Date(),
}),
io.input.boolean('Boolean input').optional(),
io.input.boolean('Boolean input', { defaultValue: true }),
io.input.boolean('Boolean input', { defaultValue: null }),
io.select
.single('Select something', {
options: [1, 2, 3],
Expand All @@ -752,11 +752,13 @@ const interval = new Interval({
options: [1, 2, 3],
defaultValue: 1,
}),
io.input.number('Number input').optional(),
io.input.number('Number input', { defaultValue: null }).optional(),
io.input.number('Number input', { defaultValue: 100 }),
io.input.email('Email input').optional(),
io.input.email('Email input', { defaultValue: '[email protected]' }),
io.input.richText('Rich text input').optional(),
io.input
.richText('Rich text input', { defaultValue: null })
.optional(),
io.input.richText('Rich text input', {
defaultValue: 'Hello world',
}),
Expand Down Expand Up @@ -807,6 +809,8 @@ const interval = new Interval({
io.input.file('File input').optional(),
])

console.debug(responses)

i++
}

Expand Down
44 changes: 26 additions & 18 deletions src/ioSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ export const INPUT_COMPONENT_TO_RENDER = DISPLAY_COMPONENT_TO_RENDER.merge(
isMultiple: z.boolean().optional().default(false),
isOptional: z.boolean().optional().default(false),
validationErrorMessage: z.string().nullish(),
multipleProps: z.optional(
z.object({
defaultValue: z.optional(z.array(z.any())),
})
),
multipleProps: z
.optional(
z.object({
defaultValue: z.optional(z.array(z.any())).nullable(),
})
)
.nullable(),
})
)

Expand Down Expand Up @@ -558,7 +560,7 @@ const INPUT_SCHEMA = {
props: z.object({
helpText: z.optional(z.string()),
placeholder: z.optional(z.string()),
defaultValue: z.optional(z.string()),
defaultValue: z.optional(z.string()).nullable(),
multiline: z.optional(z.boolean()),
lines: z.optional(z.number()),
minLength: z.optional(z.number().int().positive()),
Expand All @@ -572,7 +574,7 @@ const INPUT_SCHEMA = {
props: z.object({
helpText: z.optional(z.string()),
placeholder: z.optional(z.string()),
defaultValue: z.optional(z.string()),
defaultValue: z.optional(z.string()).nullable(),
disabled: z.optional(z.boolean().default(false)),
}),
state: z.null(),
Expand All @@ -585,7 +587,7 @@ const INPUT_SCHEMA = {
prepend: z.optional(z.string()),
helpText: z.optional(z.string()),
placeholder: z.optional(z.string()),
defaultValue: z.optional(z.number()),
defaultValue: z.optional(z.number()).nullable(),
decimals: z.optional(z.number().positive().int()),
currency: z.optional(currencyCode),
disabled: z.optional(z.boolean().default(false)),
Expand All @@ -597,7 +599,7 @@ const INPUT_SCHEMA = {
props: z.object({
helpText: z.optional(z.string()),
placeholder: z.optional(z.string()),
defaultValue: z.optional(z.string()),
defaultValue: z.optional(z.string()).nullable(),
allowedProtocols: z.array(z.string()).default(['http', 'https']),
disabled: z.optional(z.boolean().default(false)),
}),
Expand All @@ -607,7 +609,11 @@ const INPUT_SCHEMA = {
INPUT_BOOLEAN: {
props: z.object({
helpText: z.optional(z.string()),
defaultValue: z.boolean().default(false),
defaultValue: z
.boolean()
.nullable()
.default(false)
.transform(val => !!val),
disabled: z.optional(z.boolean().default(false)),
}),
state: z.null(),
Expand All @@ -617,7 +623,7 @@ const INPUT_SCHEMA = {
props: z.object({
helpText: z.optional(z.string()),
placeholder: z.optional(z.string()),
defaultValue: z.optional(z.string()),
defaultValue: z.optional(z.string()).nullable(),
disabled: z.optional(z.boolean().default(false)),
}),
state: z.null(),
Expand All @@ -626,7 +632,7 @@ const INPUT_SCHEMA = {
INPUT_DATE: {
props: z.object({
helpText: z.optional(z.string()),
defaultValue: z.optional(dateObject),
defaultValue: z.optional(dateObject).nullable(),
min: z.optional(dateObject),
max: z.optional(dateObject),
disabled: z.optional(z.boolean().default(false)),
Expand All @@ -637,7 +643,7 @@ const INPUT_SCHEMA = {
INPUT_TIME: {
props: z.object({
helpText: z.optional(z.string()),
defaultValue: z.optional(timeObject),
defaultValue: z.optional(timeObject).nullable(),
min: z.optional(timeObject),
max: z.optional(timeObject),
disabled: z.optional(z.boolean().default(false)),
Expand All @@ -648,7 +654,7 @@ const INPUT_SCHEMA = {
INPUT_DATETIME: {
props: z.object({
helpText: z.optional(z.string()),
defaultValue: z.optional(dateTimeObject),
defaultValue: z.optional(dateTimeObject).nullable(),
min: z.optional(dateTimeObject),
max: z.optional(dateTimeObject),
disabled: z.optional(z.boolean().default(false)),
Expand All @@ -659,7 +665,7 @@ const INPUT_SCHEMA = {
INPUT_SPREADSHEET: {
props: z.object({
helpText: z.string().optional(),
defaultValue: z.optional(z.array(deserializableRecord)),
defaultValue: z.optional(z.array(deserializableRecord)).nullable(),
columns: z.record(typeValue),
}),
state: z.null(),
Expand Down Expand Up @@ -709,7 +715,7 @@ const INPUT_SCHEMA = {
SEARCH: {
props: z.object({
results: z.array(searchResult),
defaultValue: z.optional(z.string()),
defaultValue: z.optional(z.string()).nullable(),
placeholder: z.optional(z.string()),
helpText: z.optional(z.string()),
disabled: z.optional(z.boolean().default(false)),
Expand Down Expand Up @@ -769,7 +775,7 @@ const INPUT_SCHEMA = {
props: z.object({
options: z.array(richSelectOption),
helpText: z.optional(z.string()),
defaultValue: z.optional(richSelectOption),
defaultValue: z.optional(richSelectOption).nullable(),
searchable: z.optional(z.boolean()),
disabled: z.optional(z.boolean().default(false)),
}),
Expand All @@ -782,7 +788,9 @@ const INPUT_SCHEMA = {
helpText: z.optional(z.string()),
defaultValue: z
.array(labelValue)
.default([] as z.infer<typeof labelValue>[]),
.nullable()
.default([] as z.infer<typeof labelValue>[])
.transform(val => val ?? []),
minSelections: z.optional(z.number().int().min(0)),
maxSelections: z.optional(z.number().positive().int()),
disabled: z.optional(z.boolean().default(false)),
Expand Down

0 comments on commit a10d02f

Please sign in to comment.