提问人:Dawood Trumboo 提问时间:11/11/2023 更新时间:11/11/2023 访问量:9
我无法弄清楚如何将必需属性添加到 Headless.ui 提供的菜单组件
Im not able to figure out how do I add required attribute to the Menu Component provided by Headless.ui
问:
我有一个表单,其中我渲染了一个单独的 Dropdown 组件,其中我从 Headless Ui 导入了一个下拉列表。因此,当我按下表单的提交按钮时,我希望下拉组件在提交之前具有一些值,就像具有必需属性的 select 元素工作时一样。但是我无法弄清楚在不手动编写逻辑的情况下使 Menu 组件像这样工作的道具是什么。
import { Fragment, useState } from 'react'
import { Menu, Transition } from '@headlessui/react'
import { BiChevronDown } from 'react-icons/bi'
import {LuAsterisk} from 'react-icons/lu'
function classNames(...classes) {
return classes.filter(Boolean).join(' ')
}
export default function DropDown({label}) {
const [optionValue, setOptionValue] = useState("Select an option");
const handleSelect = (e) =>{
const selectedOption = e.target.value;
setOptionValue(selectedOption);
};
return (
<Menu as="div" className="w-[49%] relative inline-block text-left">
<div className='flex gap-[2px] items-center'>
<label>{label}</label> <LuAsterisk color='red'/>
</div>
<div className='flex h-[40px] mt-1'>
<Menu.Button className="flex justify-between items-center w-full gap-x-1.5 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 ring-1 ring-inset ring-[#E0E0E0] hover:bg-gray-50">
<span className='text-sm font-normal text-[#637381]'>{optionValue}</span>
<BiChevronDown className="-mr-1 h-5 w-5 text-gray-400" aria-hidden="true" />
</Menu.Button>
</div>
<Transition
as={Fragment}
enter="transition ease-out duration-100"
enterFrom="transform opacity-0 scale-95"
enterTo="transform opacity-100 scale-100"
leave="transition ease-in duration-75"
leaveFrom="transform opacity-100 scale-100"
leaveTo="transform opacity-0 scale-95"
>
<Menu.Items className="absolute right-0 z-10 mt-2 w-full origin-top-right rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none">
<div className="py-1">
<Menu.Item>
{({ active }) => (
<option className={classNames(
active ? 'bg-gray-100 text-gray-900' : 'text-gray-700',
'block px-4 py-2 text-sm'
)}
onClick={handleSelect}>Srinagar</option>
)}
</Menu.Item>
<Menu.Item>
{({ active }) => (
<option className={classNames(
active ? 'bg-gray-100 text-gray-900' : 'text-gray-700',
'block px-4 py-2 text-sm'
)}
onClick={handleSelect}>Jammu</option>
)}
</Menu.Item>
<Menu.Item>
{({ active }) => (
<option className={classNames(
active ? 'bg-gray-100 text-gray-900' : 'text-gray-700',
'block px-4 py-2 text-sm'
)}
onClick={handleSelect}>Account Settings</option>
)}
</Menu.Item>
<Menu.Item>
{({ active }) => (
<option className={classNames(
active ? 'bg-gray-100 text-gray-900' : 'text-gray-700',
'block px-4 py-2 text-sm'
)}
onClick={handleSelect}>Canada</option>
)}
</Menu.Item>
</div>
</Menu.Items>
</Transition>
</Menu>
)
}
我尝试使用具有必需属性的 select 元素,但这似乎不起作用。
答: 暂无答案
评论