在以下项目中,我正在Microsoft Excel中使用TypeScript和React创建任务窗格。加载任务窗格时,它将仅具有几个选项的基本下拉列表。
import * as React from "react";
import Progress from "./Progress";
import { Dropdown, DropdownMenuItemType, IDropdownStyles, IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown';
//import TopLevel from "./Operations/TopLevel";
//import MakeItem from "./Operations/MakeItem";
//import RawMaterial from "./Operations/RawMaterial";
//import BuyItem from "./Operations/BuyItem";
export interface AppProps {
title: string;
isOfficeInitialized: boolean;
}
export interface AppState {
}
export default class App extends React.Component<AppProps, AppState> {
constructor(props, context) {
super(props, context);
this.state = {
};
}
componentDidMount() {
this.setState({
});
}
render() {
const { title, isOfficeInitialized } = this.props;
const dropdownStyles: Partial<IDropdownStyles> = {
dropdown: { width: 300 }
};
const options: IDropdownOption[] = [
{ key: 'blank', text: '' },
{ key: 'topLevelMake', text: 'Parents', itemType: DropdownMenuItemType.Header },
{ key: 'topLevel', text: 'TOP LEVEL' },
{ key: 'make', text: 'MAKE ITEM' },
{ key: 'divider_1', text: '-', itemType: DropdownMenuItemType.Divider },
{ key: 'Purchased', text: 'Purchases', itemType: DropdownMenuItemType.Header },
{ key: 'rawMaterial', text: 'RAW MATERIAL' },
{ key: 'buyItem', text: 'BUY ITEM' },
];
;
if (!isOfficeInitialized) {
return (
<Progress title={title} logo="assets/logo-filled.png" message="Please sideload your addin to see app body." />
);
}
return (
<div className="ms-welcome">
<Dropdown
label="Operation"
key="dropDownKey"
options={options}
styles={dropdownStyles}
/>
</div>
);
}
}
在收到Klas的回复后,我对代码进行了一些更改,但是由于没有错误抛出而遇到了麻烦。我有App.tsx,TopLevel.tsx和由于Klas创建的名为Dropdown.tsx的新文件。现在我会忘记“制作”,我只是在试图理解逻辑。我在这里做错了什么?
App.tsx
import * as React from "react";
import Dropdown from "./Operations/Dropdown";
export interface AppProps {
title: string;
isOfficeInitialized: boolean;
}
export interface AppState {
}
function App() {
return <Dropdown />;
}
export default App;
Dropdown.tsx
import * as React from "react";
import TopLevel from './TopLevel';
import { useState } from 'react';
const Dropdown = (): React.ReactElement => {
const [value, setValue] = useState("none");
let Form: any;
switch (value) {
case "TopLevel":
Form = <TopLevel title="title" logo="logo" message="message" />;
break;
case "Make":
Form = <div />;
break;
default:
Form = <div />;
break;
}
return (
<div>
<select
id="lang"
onChange={event => event?.target?.value && setValue(event.target.value)}
value={value}
>
<option value="select">Select</option>
<option value="TopLevel">TopLevel</option>
<option value="Make">Make</option>
</select>
{Form}
</div>
);
};
export default Dropdown;
TopLevel.tsx
import * as React from "react";
export interface TopLevelProps {
title: string;
logo: string;
message: string;
}
export default class TopLevel extends React.Component<TopLevelProps> {
render() {
return (
<div>
<div>TopLevel</div>
<input placeholder="Part Number" />
</div>
);
}
}
这是一个具有下拉菜单的基本程序,根据下拉菜单的值将显示不同的形式。
const Dropdown = (): React.ReactElement => {
const [value, setValue] = useState("none");
let Form: any;
switch (value) {
case "TopLevel":
Form = <TopLevel title="title" logo="logo" message="message" />;
break;
case "Make":
Form = <Make />;
break;
default:
Form = <div />;
break;
}
return (
<div>
<select
id="lang"
onChange={event => event?.target?.value && setValue(event.target.value)}
value={value}
>
<option value="select">Select</option>
<option value="TopLevel">TopLevel</option>
<option value="Make">Make</option>
</select>
{Form}
</div>
);
};
export interface TopLevelProps {
title: string;
logo: string;
message: string;
}
class TopLevel extends React.Component<TopLevelProps> {
render() {
return (
<div>
<div>TopLevel</div>
<input placeholder="Part Number" />
</div>
);
}
}
class Make extends React.Component<{}> {
render() {
return <div>Make</div>;
}
}
function App() {
return <Dropdown />;
}
注意switch
如何让您使用不同的形式表示不同的值。
您正在使用的DropDown
组件也具有onChange
属性,因此应该可以通过类似的方式进行连接,并以不同的形式呈现不同的值。