fixed selectfield ui
This commit is contained in:
parent
0e75a3e1c9
commit
29cebedfad
@ -12,6 +12,8 @@ const SelectField = ({
|
|||||||
labelKey = "name",
|
labelKey = "name",
|
||||||
isLoading = false,
|
isLoading = false,
|
||||||
}) => {
|
}) => {
|
||||||
|
const [position, setPosition] = useState("bottom");
|
||||||
|
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const dropdownRef = useRef(null);
|
const dropdownRef = useRef(null);
|
||||||
|
|
||||||
@ -34,10 +36,26 @@ const SelectField = ({
|
|||||||
setOpen(false);
|
setOpen(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const toggleDropdown = () => setOpen((prev) => !prev);
|
const toggleDropdown = () => {
|
||||||
|
if (!open) {
|
||||||
|
const rect = dropdownRef.current?.getBoundingClientRect();
|
||||||
|
const viewportHeight = window.innerHeight;
|
||||||
|
|
||||||
|
const spaceBelow = viewportHeight - rect.bottom;
|
||||||
|
const dropdownHeight = 200;
|
||||||
|
|
||||||
|
if (spaceBelow < dropdownHeight) {
|
||||||
|
setPosition("top"); // open upward
|
||||||
|
} else {
|
||||||
|
setPosition("bottom"); // open downward
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setOpen((prev) => !prev);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mb-3 position-relative" ref={dropdownRef}>
|
<div className="position-relative" ref={dropdownRef}>
|
||||||
{label && (
|
{label && (
|
||||||
<Label className="form-label" required={required}>
|
<Label className="form-label" required={required}>
|
||||||
{label}
|
{label}
|
||||||
@ -60,32 +78,45 @@ const SelectField = ({
|
|||||||
</button>
|
</button>
|
||||||
|
|
||||||
{open && !isLoading && (
|
{open && !isLoading && (
|
||||||
<ul
|
<div
|
||||||
className="dropdown-menu w-100 shadow-sm show animate__fadeIn"
|
className="w-full px-1 shadow-md rounded p-1"
|
||||||
style={{
|
style={{
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
top: "100%",
|
top: position === "bottom" ? "100%" : "auto",
|
||||||
|
bottom: position === "top" ? "100%" : "auto",
|
||||||
left: 0,
|
left: 0,
|
||||||
zIndex: 1050,
|
zIndex: 1050,
|
||||||
marginTop: "4px",
|
|
||||||
borderRadius: "0.375rem",
|
marginTop: position === "bottom" ? "2px" : "0",
|
||||||
overflow: "hidden",
|
marginBottom: position === "top" ? "2px" : "0",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{options.map((option, i) => (
|
<ul
|
||||||
<li key={i}>
|
className="dropdown-menu w-100 show border-0 rounded px-1 py-1"
|
||||||
<button
|
style={{
|
||||||
type="button"
|
position: "static",
|
||||||
className={`dropdown-item ${
|
|
||||||
option[valueKey] === value ? "active" : ""
|
maxHeight: "220px",
|
||||||
}`}
|
overflowY: "auto",
|
||||||
onClick={() => handleSelect(option)}
|
overflowX: "hidden",
|
||||||
>
|
}}
|
||||||
{option[labelKey]}
|
id="vertical-example"
|
||||||
</button>
|
>
|
||||||
</li>
|
{options.map((option, i) => (
|
||||||
))}
|
<li key={i}>
|
||||||
</ul>
|
<button
|
||||||
|
type="button"
|
||||||
|
className={`dropdown-item rounded text-truncate ${
|
||||||
|
option[valueKey] === value ? "active" : ""
|
||||||
|
}`}
|
||||||
|
onClick={() => handleSelect(option)}
|
||||||
|
>
|
||||||
|
{option[labelKey]}
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user