-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
112 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,63 @@ | ||
import { z } from 'zod' | ||
import { T_IO_PROPS, T_IO_RETURNS, labelValue } from '../ioSchema' | ||
import Logger from '../classes/Logger' | ||
|
||
type SelectMultipleProps<Option extends z.infer<typeof labelValue> | string> = | ||
Omit<T_IO_PROPS<'SELECT_MULTIPLE'>, 'options' | 'defaultValue'> & { | ||
options: Option[] | ||
defaultValue?: Option[] | ||
} | ||
|
||
export default function selectMultiple< | ||
Option extends z.infer<typeof labelValue> | string | ||
>(props: SelectMultipleProps<Option>) { | ||
const normalizedOptions: z.infer<typeof labelValue>[] = props.options.map( | ||
option => { | ||
if (typeof option === 'string') { | ||
export default function selectMultiple(logger: Logger) { | ||
return <Option extends z.infer<typeof labelValue> | string>( | ||
props: SelectMultipleProps<Option> | ||
) => { | ||
const normalizedOptions: z.infer<typeof labelValue>[] = props.options.map( | ||
option => { | ||
if (typeof option === 'string') { | ||
return { | ||
label: option, | ||
value: option, | ||
} | ||
} else { | ||
return option as Exclude<Option, string> | ||
} | ||
} | ||
) | ||
type Options = typeof props.options | ||
const optionMap = new Map( | ||
normalizedOptions.map((o, i) => [o.value, props.options[i]]) | ||
) | ||
|
||
let defaultValue = props.defaultValue?.map(d => { | ||
if (typeof d === 'string') { | ||
return { | ||
label: option, | ||
value: option, | ||
label: d, | ||
value: d, | ||
} | ||
} else { | ||
return option as Exclude<Option, string> | ||
return d as Exclude<Option, string> | ||
} | ||
} | ||
) | ||
type Options = typeof props.options | ||
const optionMap = new Map( | ||
normalizedOptions.map((o, i) => [o.value, props.options[i]]) | ||
) | ||
}) | ||
|
||
const defaultValue = props.defaultValue?.map(d => { | ||
if (typeof d === 'string') { | ||
return { | ||
label: d, | ||
value: d, | ||
} | ||
} else { | ||
return d | ||
if (defaultValue && defaultValue.every(val => !optionMap.has(val.value))) { | ||
logger.warn( | ||
'The defaultValue property must be a subset of the provided options, the provided defaultValue will be discarded.' | ||
) | ||
defaultValue = [] | ||
} | ||
}) | ||
|
||
const stripper = labelValue.strip() | ||
const stripper = labelValue.strip() | ||
|
||
return { | ||
props: { | ||
...props, | ||
defaultValue: defaultValue?.map(o => stripper.parse(o)), | ||
options: normalizedOptions.map(o => stripper.parse(o)), | ||
}, | ||
getValue(response: T_IO_RETURNS<'SELECT_MULTIPLE'>): Options { | ||
return response.map(r => optionMap.get(r.value)) as Options | ||
}, | ||
return { | ||
props: { | ||
...props, | ||
defaultValue: defaultValue?.map(o => stripper.parse(o)), | ||
options: normalizedOptions.map(o => stripper.parse(o)), | ||
}, | ||
getValue(response: T_IO_RETURNS<'SELECT_MULTIPLE'>): Options { | ||
return response.map(r => optionMap.get(r.value)) as Options | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters