row action add props top and left
This commit is contained in:
@@ -14,11 +14,23 @@ interface RowActionsDropdownProps {
|
|||||||
actions: RowActionItem[];
|
actions: RowActionItem[];
|
||||||
className?: string;
|
className?: string;
|
||||||
trigger?: React.ReactNode;
|
trigger?: React.ReactNode;
|
||||||
|
topOffset?: number;
|
||||||
|
leftOffset?: number;
|
||||||
|
topOffsetMobile?: number;
|
||||||
|
leftOffsetMobile?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const RowActionsDropdown: React.FC<RowActionsDropdownProps> = ({ actions, className = '', trigger }) => {
|
const RowActionsDropdown: React.FC<RowActionsDropdownProps> = ({
|
||||||
|
actions,
|
||||||
|
className = '',
|
||||||
|
trigger,
|
||||||
|
topOffset = 0,
|
||||||
|
leftOffset = 0,
|
||||||
|
topOffsetMobile = 0,
|
||||||
|
leftOffsetMobile = 0
|
||||||
|
}) => {
|
||||||
const [isOpen, setIsOpen] = useState(false)
|
const [isOpen, setIsOpen] = useState(false)
|
||||||
const [position, setPosition] = useState({ top: 0, left: 0 })
|
const [dropdownPosition, setDropdownPosition] = useState({ top: 0, left: 0 })
|
||||||
const dropdownRef = useRef<HTMLDivElement>(null)
|
const dropdownRef = useRef<HTMLDivElement>(null)
|
||||||
const buttonRef = useRef<HTMLButtonElement>(null)
|
const buttonRef = useRef<HTMLButtonElement>(null)
|
||||||
|
|
||||||
@@ -35,31 +47,46 @@ const RowActionsDropdown: React.FC<RowActionsDropdownProps> = ({ actions, classN
|
|||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
const calculatePosition = (rect: DOMRect) => {
|
||||||
|
const isMobile = window.innerWidth < 768
|
||||||
|
const dropdownWidth = isMobile ? 140 : 150
|
||||||
|
const dropdownHeight = actions.length * (isMobile ? 36 : 40)
|
||||||
|
|
||||||
|
// انتخاب offset بر اساس سایز صفحه
|
||||||
|
const currentTopOffset = isMobile ? topOffsetMobile : topOffset
|
||||||
|
const currentLeftOffset = isMobile ? leftOffsetMobile : leftOffset
|
||||||
|
|
||||||
|
let left = rect.left + window.scrollX - dropdownWidth + rect.width + currentLeftOffset
|
||||||
|
let top = rect.bottom + window.scrollY + currentTopOffset
|
||||||
|
|
||||||
|
if (left + dropdownWidth > window.innerWidth) {
|
||||||
|
left = rect.left + window.scrollX - dropdownWidth + currentLeftOffset
|
||||||
|
}
|
||||||
|
|
||||||
|
if (left < 0) {
|
||||||
|
left = isMobile ? 20 : 40
|
||||||
|
}
|
||||||
|
|
||||||
|
if (top + dropdownHeight > window.innerHeight + window.scrollY) {
|
||||||
|
top = rect.top + window.scrollY - dropdownHeight + currentTopOffset
|
||||||
|
}
|
||||||
|
|
||||||
|
// اطمینان از اینکه dropdown از صفحه خارج نشود
|
||||||
|
if (left < 0) left = 20
|
||||||
|
if (left + dropdownWidth > window.innerWidth) left = window.innerWidth - dropdownWidth - 20
|
||||||
|
if (top < 0) top = 20
|
||||||
|
if (top + dropdownHeight > window.innerHeight) top = window.innerHeight - dropdownHeight - 20
|
||||||
|
|
||||||
|
return { top, left }
|
||||||
|
}
|
||||||
|
|
||||||
const handleToggle = (e: React.MouseEvent) => {
|
const handleToggle = (e: React.MouseEvent) => {
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
|
|
||||||
if (!isOpen && buttonRef.current) {
|
if (!isOpen && buttonRef.current) {
|
||||||
const rect = buttonRef.current.getBoundingClientRect()
|
const rect = buttonRef.current.getBoundingClientRect()
|
||||||
const isMobile = window.innerWidth < 768
|
const calculatedPosition = calculatePosition(rect)
|
||||||
const dropdownWidth = isMobile ? 140 : 150
|
setDropdownPosition(calculatedPosition)
|
||||||
const dropdownHeight = actions.length * (isMobile ? 36 : 40)
|
|
||||||
|
|
||||||
let left = rect.left + window.scrollX - dropdownWidth + rect.width
|
|
||||||
let top = rect.bottom + window.scrollY
|
|
||||||
|
|
||||||
if (left + dropdownWidth > window.innerWidth) {
|
|
||||||
left = rect.left + window.scrollX - dropdownWidth
|
|
||||||
}
|
|
||||||
|
|
||||||
if (left < 0) {
|
|
||||||
left = isMobile ? 20 : 40
|
|
||||||
}
|
|
||||||
|
|
||||||
if (top + dropdownHeight > window.innerHeight + window.scrollY) {
|
|
||||||
top = rect.top + window.scrollY - dropdownHeight
|
|
||||||
}
|
|
||||||
|
|
||||||
setPosition({ top, left })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsOpen(!isOpen)
|
setIsOpen(!isOpen)
|
||||||
@@ -78,8 +105,8 @@ const RowActionsDropdown: React.FC<RowActionsDropdownProps> = ({ actions, classN
|
|||||||
ref={dropdownRef}
|
ref={dropdownRef}
|
||||||
className="fixed py-3 md:py-3 bg-white dark:bg-[#252526] rounded-xl md:rounded-2xl shadow-lg z-[5] min-w-[140px] md:min-w-[150px] border border-gray-200 dark:border-[#3e3e42]"
|
className="fixed py-3 md:py-3 bg-white dark:bg-[#252526] rounded-xl md:rounded-2xl shadow-lg z-[5] min-w-[140px] md:min-w-[150px] border border-gray-200 dark:border-[#3e3e42]"
|
||||||
style={{
|
style={{
|
||||||
top: `${position.top}px`,
|
top: `${dropdownPosition.top}px`,
|
||||||
left: `${position.left}px`,
|
left: `${dropdownPosition.left}px`,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{actions.map((action, index) => (
|
{actions.map((action, index) => (
|
||||||
|
|||||||
@@ -285,6 +285,11 @@ const Header: FC = () => {
|
|||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
trigger={<MessageQuestion size={20} color={getIconColor('primary')} />}
|
trigger={<MessageQuestion size={20} color={getIconColor('primary')} />}
|
||||||
|
topOffset={20}
|
||||||
|
leftOffset={70}
|
||||||
|
topOffsetMobile={10}
|
||||||
|
leftOffsetMobile={60}
|
||||||
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user