Pass them as an Object like Niyas Nazar commented.
Example:
CommandBarContext.tsx
import React, { createContext, useContext, useState } from 'react'
interface ComandBarState {
newEnabled: boolean
setNewEnabled: (state: boolean) => void
copyEnabled: boolean
setCopyEnabled: (state: boolean) => void
deleteEnabled: boolean
setDeleteEnabled: (state: boolean) => void
}
const commandBarContext = createContext<ComandBarState>({
newEnabled: true,
setNewEnabled: (state: boolean) => { },
copyEnabled: false,
setCopyEnabled: (state: boolean) => { },
deleteEnabled: false,
setDeleteEnabled: (state: boolean) => { },
})
export const CommandBarProvider = (props: any) => {
const { children } = props
const [newEnabled, setNewEnabled] = useState<boolean>(true)
const [copyEnabled, setCopyEnabled] = useState<boolean>(false)
const [deleteEnabled, setDeleteEnabled] = useState<boolean>(false)
return (
<commandBarContext.Provider value={{
newEnabled, setNewEnabled,
copyEnabled, setCopyEnabled,
deleteEnabled, setDeleteEnabled
}}>
{children}
</commandBarContext.Provider>)
}
export const useCommandNew = () => {
const { newEnabled, setNewEnabled } = useContext(commandBarContext)
return { newEnabled, setNewEnabled }
}
export const useCommandCopy = () => {
const { copyEnabled, setCopyEnabled } = useContext(commandBarContext)
return { copyEnabled, setCopyEnabled }
}
export const useCommandDelete = () => {
const { deleteEnabled, setDeleteEnabled } = useContext(commandBarContext)
return { deleteEnabled, setDeleteEnabled }
}
CommandBar.tsx
import React, { FunctionComponent } from 'react'
import {
CommandBar as FluentCommandBar,
ICommandBarItemProps,
ICommandBarStyles
} from '@fluentui/react/lib/CommandBar'
import { Text } from '@fluentui/react/lib/Text'
import { Stack, StackItem } from '@fluentui/react/lib/Stack'
import {
useCommandNew,
useCommandCopy,
useCommandDelete,
} from './CommandBarContext'
import { useTranslation } from 'react-i18next'
export interface CommandBarProps {
title: string
onCommandNew?: () => void,
onCommandCopy?: () => void,
onCommandDelete?: () => void,
}
export const CommandBar: FunctionComponent<CommandBarProps> = props => {
const { title } = props
const {
onCommandNew = () => { },
onCommandCopy = () => { },
onCommandDelete = () => { },
} = props
const { newEnabled } = useCommandNew()
const { copyEnabled } = useCommandCopy()
const { deleteEnabled } = useCommandDelete()
const { t } = useTranslation()
const items: ICommandBarItemProps[] = [
{
key: 'commandNew',
text: t('New'),
disabled: !newEnabled,
iconProps: { iconName: 'Add' },
onClick: onCommandNew,
},
{
key: 'commandCopy',
text: t('Copy'),
iconProps: { iconName: 'Copy' },
disabled: !copyEnabled,
onClick: onCommandCopy,
},
{
key: 'commandDelete',
text: t('Delete'),
iconProps: { iconName: 'Delete' },
disabled: !deleteEnabled,
onClick: onCommandDelete,
},
]
return (
<Stack horizontal tokens={{ childrenGap: 30 }}>
<Text variant="xLarge" styles={{ root: { marginTop: 7 } }}>{title}</Text>
<StackItem grow>
<FluentCommandBar
items={items}
styles={commandBarStyles}
/>
</StackItem>
</Stack>
)
}
const commandBarStyles: ICommandBarStyles = {
root: {
marginTop: 0,
paddingLeft: 0,
paddingRight: 0
}
}
Use in parent component:
...
import {
useCommandNew,
useCommandCopy,
useCommandDelete,
} from './CommandBarContext'
...
const { setNewEnabled } = useCommandNew()
const { setCopyEnabled } = useCommandCopy()
const { setDeleteEnabled } = useCommandDelete()
...
return (
<CommandBarProvider>
<Stack tokens={{ childrenGap: 5 }}>
<CommandBar title="Locations" />
<Table
columns={columns}
items={items}
onSelection={onSelection}
onItemInvoked={onItemInvoked}
/>
</Stack>
<Panel
header="Location"
columns={columns}
items={selection}
isPanelOpen={isPanelOpen}
onCancel={onCancel}
onSave={onSave}
/>
</CommandBarProvider>
)
}
...