Allow hidden config to be a callback

This commit is contained in:
Frank Noirot
2025-03-11 15:19:35 -04:00
parent b5028f7aa8
commit e952148cd2
3 changed files with 27 additions and 6 deletions

View File

@ -17,7 +17,12 @@ function CommandBarHeader({ children }: React.PropsWithChildren<{}>) {
if (!selectedCommand?.args) return undefined
const s = { ...selectedCommand.args }
for (const [name, arg] of Object.entries(s)) {
if (arg.hidden) delete s[name]
if (
typeof arg.hidden === 'function'
? arg.hidden(commandBarState.context)
: arg.hidden
)
delete s[name]
}
return s
}, [selectedCommand])

View File

@ -120,7 +120,12 @@ export type CommandArgumentConfig<
) => boolean)
warningMessage?: string
/** If `true`, arg is used as passed-through data, never for user input */
hidden?: boolean
hidden?:
| boolean
| ((
commandBarContext: { argumentsToSubmit: Record<string, unknown> }, // Should be the commandbarMachine's context, but it creates a circular dependency
machineContext?: C
) => boolean)
skip?: boolean
/** For showing a summary display of the current value, such as in
* the command bar's header
@ -236,7 +241,12 @@ export type CommandArgument<
machineContext?: ContextFrom<T>
) => boolean)
/** If `true`, arg is used as passed-through data, never for user input */
hidden?: boolean
hidden?:
| boolean
| ((
commandBarContext: { argumentsToSubmit: Record<string, unknown> }, // Should be the commandbarMachine's context, but it creates a circular dependency
machineContext?: ContextFrom<T>
) => boolean)
skip?: boolean
machineActor?: Actor<T>
warningMessage?: string

View File

@ -134,8 +134,10 @@ export const commandBarMachine = setup({
// that is, the first argument that is not already in the argumentsToSubmit
// or hidden, or that is not undefined, or that is not marked as "skippable".
// TODO validate the type of the existing arguments
const nonHiddenArgs = Object.entries(selectedCommand.args).filter(
(a) => !a[1].hidden
const nonHiddenArgs = Object.entries(selectedCommand.args).filter((a) =>
a[1].hidden && typeof a[1].hidden === 'function'
? !a[1].hidden(context)
: !a[1].hidden
)
let argIndex = 0
@ -260,7 +262,11 @@ export const commandBarMachine = setup({
},
'All arguments are skippable': ({ context }) => {
return Object.values(context.selectedCommand!.args!).every(
(argConfig) => argConfig.skip || argConfig.hidden
(argConfig) =>
argConfig.skip ||
(typeof argConfig.hidden === 'function'
? argConfig.hidden(context)
: argConfig.hidden)
)
},
'Has selected command': ({ context }) => !!context.selectedCommand,