当我单击某个按钮时,反应流利的UI打开组件

发布于 2025-01-27 13:01:44 字数 3660 浏览 2 评论 0原文

即时消息,当我从命令栏中单击某个按钮时,我想打开带有流利的UI的React页面,我想打开某个组件(documentpane.tsx)。

这是我的命令栏代码:

const theme = getTheme();
// Styles for both command bar and overflow/menu items
const itemStyles: Partial<IContextualMenuItemStyles> = {
  label: { fontSize: 18 },
  icon: { color: uPrinceTheme.palette.themePrimary },
  iconHovered: { color: uPrinceTheme.palette.themeSecondary },
};
// For passing the styles through to the context menus
const menuStyles: Partial<IContextualMenuStyles> = {
  subComponentStyles: { menuItem: itemStyles, callout: {} },
};

const getCommandBarButtonStyles = memoizeFunction(
  (originalStyles: IButtonStyles | undefined): Partial<IContextualMenuItemStyles> => {
    if (!originalStyles) {
      return itemStyles;
    }

    return concatStyleSets(originalStyles, itemStyles);
  },
);

// Custom renderer for main command bar items
const CustomButton: React.FunctionComponent<IButtonProps> = props => {
  const buttonOnMouseClick = () => alert(`${props.text} clicked`);
  // eslint-disable-next-line react/jsx-no-bind
  return <CommandBarButton {...props} onClick={buttonOnMouseClick} styles={getCommandBarButtonStyles(props.styles)} />;
};

// Custom renderer for menu items (these must have a separate custom renderer because it's unlikely
// that the same component could be rendered properly as both a command bar item and menu item).
// It's also okay to custom render only the command bar items without changing the menu items.
const CustomMenuItem: React.FunctionComponent<IContextualMenuItemProps> = props => {
  // Due to ContextualMenu implementation quirks, passing styles or onClick here doesn't work.
  // The onClick handler must be on the ICommandBarItemProps item instead (_overflowItems in this example).
  return <ContextualMenuItem {...props} />;
};

const overflowProps: IButtonProps = {
  ariaLabel: 'More commands',
  menuProps: {
    contextualMenuItemAs: CustomMenuItem,
    // Styles are passed through to menu items here
    styles: menuStyles,
    items: [], // CommandBar will determine items rendered in overflow
    isBeakVisible: true,
    beakWidth: 20,
    gapSpace: 10,
    directionalHint: DirectionalHint.topCenter,
  },
};

export const CommandBarButtonAsExample: React.FunctionComponent = () => {
  return (
    <CommandBar
      
      overflowButtonProps={overflowProps}
      // Custom render all buttons
      buttonAs={CustomButton}
      items={_items}
      ariaLabel="Use left and right arrow keys to navigate between commands"
    />
  );
};

const _items: ICommandBarItemProps[] = [
  {
    key: 'newItem',
    text: 'Create',
    iconProps: { iconName: 'Add' },
  },
  {
    key: 'upload',
    text: 'Read',
    iconProps: { iconName: 'Read' },
    href: 'https://developer.microsoft.com/en-us/fluentui',
  },
  { key: 'share', text: 'Update', iconProps: { iconName: 'Share' } },
  { key: 'download', text: 'Delete', iconProps: { iconName: 'Delete' } },

];


export default CommandBarButtonAsExample;

这是我的index.tsx:

import { StrictMode } from "react";
import ReactDOM from "react-dom";
import { initializeIcons } from "@fluentui/react/lib/Icons";
import App from "./App";

const rootElement = document.getElementById("root");
initializeIcons();
ReactDOM.render(
  <StrictMode>
    <App />
  </StrictMode>,
  rootElement
);

我还将在其中添加一个带有我的代码的git repo,以便你们可以更好地看到它。

https://github.com/robbe-delsoir/app2

非常感谢您的帮助呢

罗伯

Im making a react page with fluent ui and i wanted to open a certain component (documentpane.tsx) when i click a certain button from my command bar.

this is my commandbar code:

const theme = getTheme();
// Styles for both command bar and overflow/menu items
const itemStyles: Partial<IContextualMenuItemStyles> = {
  label: { fontSize: 18 },
  icon: { color: uPrinceTheme.palette.themePrimary },
  iconHovered: { color: uPrinceTheme.palette.themeSecondary },
};
// For passing the styles through to the context menus
const menuStyles: Partial<IContextualMenuStyles> = {
  subComponentStyles: { menuItem: itemStyles, callout: {} },
};

const getCommandBarButtonStyles = memoizeFunction(
  (originalStyles: IButtonStyles | undefined): Partial<IContextualMenuItemStyles> => {
    if (!originalStyles) {
      return itemStyles;
    }

    return concatStyleSets(originalStyles, itemStyles);
  },
);

// Custom renderer for main command bar items
const CustomButton: React.FunctionComponent<IButtonProps> = props => {
  const buttonOnMouseClick = () => alert(`${props.text} clicked`);
  // eslint-disable-next-line react/jsx-no-bind
  return <CommandBarButton {...props} onClick={buttonOnMouseClick} styles={getCommandBarButtonStyles(props.styles)} />;
};

// Custom renderer for menu items (these must have a separate custom renderer because it's unlikely
// that the same component could be rendered properly as both a command bar item and menu item).
// It's also okay to custom render only the command bar items without changing the menu items.
const CustomMenuItem: React.FunctionComponent<IContextualMenuItemProps> = props => {
  // Due to ContextualMenu implementation quirks, passing styles or onClick here doesn't work.
  // The onClick handler must be on the ICommandBarItemProps item instead (_overflowItems in this example).
  return <ContextualMenuItem {...props} />;
};

const overflowProps: IButtonProps = {
  ariaLabel: 'More commands',
  menuProps: {
    contextualMenuItemAs: CustomMenuItem,
    // Styles are passed through to menu items here
    styles: menuStyles,
    items: [], // CommandBar will determine items rendered in overflow
    isBeakVisible: true,
    beakWidth: 20,
    gapSpace: 10,
    directionalHint: DirectionalHint.topCenter,
  },
};

export const CommandBarButtonAsExample: React.FunctionComponent = () => {
  return (
    <CommandBar
      
      overflowButtonProps={overflowProps}
      // Custom render all buttons
      buttonAs={CustomButton}
      items={_items}
      ariaLabel="Use left and right arrow keys to navigate between commands"
    />
  );
};

const _items: ICommandBarItemProps[] = [
  {
    key: 'newItem',
    text: 'Create',
    iconProps: { iconName: 'Add' },
  },
  {
    key: 'upload',
    text: 'Read',
    iconProps: { iconName: 'Read' },
    href: 'https://developer.microsoft.com/en-us/fluentui',
  },
  { key: 'share', text: 'Update', iconProps: { iconName: 'Share' } },
  { key: 'download', text: 'Delete', iconProps: { iconName: 'Delete' } },

];


export default CommandBarButtonAsExample;

and this is my index.tsx now:

import { StrictMode } from "react";
import ReactDOM from "react-dom";
import { initializeIcons } from "@fluentui/react/lib/Icons";
import App from "./App";

const rootElement = document.getElementById("root");
initializeIcons();
ReactDOM.render(
  <StrictMode>
    <App />
  </StrictMode>,
  rootElement
);

i will also add a git repo with my code in it so you guys can see it better.

https://github.com/robbe-delsoir/app2

thank you very much for the help!

Robbe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2025-02-03 13:01:44

在Commandbar项目中添加一个ONCLICK方法,该方法为DocumentPane.tsx设置了Show组件状态。

const _items: ICommandBarItemProps[] = [
   {
      key: 'newItem',
      text: 'Create',
      iconProps: { iconName: 'Add' },
    },
    {
       key: 'upload',
       text: 'Read',
       iconProps: { iconName: 'Read' },
       onClick:{ () => { setShowDocumentPane(true) } }
       href: 'https://developer.microsoft.com/en-us/fluentui',
    },
    { key: 'share', text: 'Update', iconProps: { iconName: 'Share' } },
    { key: 'download', text: 'Delete', iconProps: { iconName: 'Delete' } },
];

Add an onClick method in the commandbar item which sets the show component state for documentpane.tsx.

const _items: ICommandBarItemProps[] = [
   {
      key: 'newItem',
      text: 'Create',
      iconProps: { iconName: 'Add' },
    },
    {
       key: 'upload',
       text: 'Read',
       iconProps: { iconName: 'Read' },
       onClick:{ () => { setShowDocumentPane(true) } }
       href: 'https://developer.microsoft.com/en-us/fluentui',
    },
    { key: 'share', text: 'Update', iconProps: { iconName: 'Share' } },
    { key: 'download', text: 'Delete', iconProps: { iconName: 'Delete' } },
];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文