gpt4 book ai didi

reactjs - 在 Typescript 中实现 react-bootstrap 自定义下拉示例

转载 作者:行者123 更新时间:2023-12-04 14:35:22 29 4
gpt4 key购买 nike

我正在尝试实现来自 react-bootstrap page 的自定义切换下拉示例, 在 Typescript 中,使用 react 功能组件。
这是我的组件代码:


import React from 'react';
import Dropdown from 'react-bootstrap/Dropdown';
import FormControl from 'react-bootstrap/FormControl';


export const DropdownSelector =() => (
<Dropdown>
<Dropdown.Toggle as={CustomToggle} id="dropdown-custom-components">
Custom toggle
</Dropdown.Toggle>

<Dropdown.Menu as={CustomMenu}>
<Dropdown.Item eventKey="1">Red</Dropdown.Item>
<Dropdown.Item eventKey="2">Blue</Dropdown.Item>
<Dropdown.Item eventKey="3" active>
Orange
</Dropdown.Item>
<Dropdown.Item eventKey="1">Red-Orange</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
)

// The forwardRef is important!!
// Dropdown needs access to the DOM node in order to position the Menu
const CustomToggle = React.forwardRef(({ children, onClick }, ref) => (
<a
href=""
ref={ref}
onClick={(e) => {
e.preventDefault();
onClick(e);
}}
>
{children}
&#x25bc;
</a>
));

// forwardRef again here!
// Dropdown needs access to the DOM of the Menu to measure it
const CustomMenu = React.forwardRef(
({ children, style, className, 'aria-labelledby': labeledBy }, ref) => {
const [value, setValue] = useState('');

return (
<div
ref={ref}
style={style}
className={className}
aria-labelledby={labeledBy}
>
<FormControl
autoFocus
className="mx-3 my-2 w-auto"
placeholder="Type to filter..."
onChange={(e) => setValue(e.target.value)}
value={value}
/>
<ul className="list-unstyled">
{React.Children.toArray(children).filter(
(child) =>
!value || child.props.children.toLowerCase().startsWith(value),
)}
</ul>
</div>
);
},
);
这无法编译:

./src/components/helpers/dropdown-selector.tsx
TypeScript error in ./src/components/helpers/dropdown-selector.tsx(25,52):
Property 'onClick' does not exist on type '{ children?: ReactNode; }'. TS2339



我究竟做错了什么?
Stackblitz sandbox version here .使用那个编辑器,我看到了一堆类型错误(虽然它确实运行了);但是我用来开发应用程序的 IDE 不允许我在出现这些错误的情况下运行它......

最佳答案

我想我确实解决了你的问题


import React, { useState } from "react";
import Dropdown from "react-bootstrap/Dropdown";
import FormControl from "react-bootstrap/FormControl";

interface IFruity {
id: number;
fruit: string;
prefix: string;
suffix?: string;
}

const fruits: IFruity[] = [
{ id: 1, fruit: "Apples", prefix: "How's about them " },
{ id: 2, fruit: "Pear", prefix: "A cracking ", suffix: "!" },
{ id: 3, fruit: "Oranges", prefix: "What rhymes with ", suffix: "?" },
{ id: 4, fruit: "Banana", prefix: "Fruit flies like a " },
{ id: 5, fruit: "Coconuts", prefix: "Oh what a lovely bunch of " },
{ id: 6, fruit: "Avocado", prefix: "Is an ", suffix: " even a fruit?" }
];

type CustomToggleProps = {
children?: React.ReactNode;
onClick?: (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {};
};

// The forwardRef is important!!
// Dropdown needs access to the DOM node in order to position the Menu
const CustomToggle = React.forwardRef(
(props: CustomToggleProps, ref: React.Ref<HTMLAnchorElement>) => (
<a
href=""
ref={ref}
onClick={e => {
e.preventDefault();
props.onClick(e);
}}
>
{props.children}
<span style={{ paddingLeft: "5px" }}>&#x25bc;</span>
</a>
)
);

type CustomMenuProps = {
children?: React.ReactNode;
style?: React.CSSProperties;
className?: string;
labeledBy?: string;
};

// forwardRef again here!
// Dropdown needs access to the DOM of the Menu to measure it
const CustomMenu = React.forwardRef(
(props: CustomMenuProps, ref: React.Ref<HTMLDivElement>) => {
const [value, setValue] = useState("");

return (
<div
ref={ref}
style={props.style}
className={props.className}
aria-labelledby={props.labeledBy}
>
<FormControl
autoFocus
className="mx-3 my-2 w-auto"
placeholder="Type to filter..."
onChange={e => setValue(e.target.value)}
value={value}
/>
<ul className="list-unstyled">
{React.Children.toArray(props.children).filter(
(child: any) =>
!value || child.props.children.toLowerCase().startsWith(value)
)}
</ul>
</div>
);
}
);

export const DropdownSelector = () => {
const [selectedFruit, setSelectedFruit] = useState(0);

const theChosenFruit = () => {
const chosenFruit: IFruity = fruits.find(f => f.id === selectedFruit);
return chosenFruit
? chosenFruit.prefix + chosenFruit.fruit + (chosenFruit.suffix || "")
: "Select a fruit";
};

return (
<Dropdown onSelect={(e: string) => setSelectedFruit(Number(e))}>
<Dropdown.Toggle as={CustomToggle} id="dropdown-custom-components">
{theChosenFruit()}
</Dropdown.Toggle>

<Dropdown.Menu as={CustomMenu}>
{fruits.map(fruit => {
return (
<Dropdown.Item key={fruit.id} eventKey={fruit.id.toString()}>
{fruit.fruit}
</Dropdown.Item>
);
})}
</Dropdown.Menu>
</Dropdown>
);
};


关于reactjs - 在 Typescript 中实现 react-bootstrap 自定义下拉示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63900810/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com