// and this is my reusable component
import React, { useState } from 'react';
import { Rnd } from 'react-rnd';
const ResizableAndDraggable = ({ initialSize, initialPosition, onLayoutChange, scale, children }) => {
// State to manage the size of the resizable component
const [rndSize, setRndSize] = useState(initialSize);
// State to manage the position of the draggable component
const [rndPosition, setRndPosition] = useState(initialPosition);
// Function to handle resizing of the component
const handleResize = (e, direction, ref, delta, position) => {
// Update the size state based on the new size
const newSize = {
width: ref.style.width,
height: ref.style.height,
};
setRndSize(newSize);
};
// Function to handle dragging of the component
const handleDrag = (e, d) => {
// Update the position state based on the new position
setRndPosition({ x: d.x, y: d.y });
};
return (
<Rnd
className='border-[1px] border-white-400 bg-slate-100/50 rounded-md h-full'
bounds="parent"
draggable
lockAspectRatio={false}
// onLayoutChange={onLayoutChange} // You can uncomment this if needed
onResize={handleResize}
onDrag={handleDrag}
resizeHandleStyles={{
bottomRight: {
bottom: '20px',
right: '10px',
cursor: 'bottom',
width: '16px',
height: '16px',
},
}}
size={{
// Adjust size based on the scale factor
width: rndSize.width scale,
height: rndSize.height scale,
}}
position={rndPosition}
I wanted to make this feature available on all devices, but I was unsuccessful. On Android devices, I am unable to add or delete items, and the on-change function only works during drag and resize operations.
import React, { useState, useEffect } from 'react'; import ResizableAndDraggable from './components/ResizableAndDraggable'; import { InputField } from './components/InputField'; import './App.css' import { ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import { useDispatch, useSelector } from 'react-redux'; import { GetAllUserSliceAction, UserEditSliceAction, UserSliceAction } from './reduxToolkit/slice/userSlice';
function debounce(fn, ms) { let timer return => { clearTimeout(timer) timer = setTimeout( => { timer = null fn.apply(this, arguments) }, ms) }; }
const App = () => { // Initialize scale for resizing const scale = 1;
// State for window size const [windowSize, setWindowSize] = useState({ width: window.innerWidth, height: window.innerHeight, });
// State for form inputs const [form, setForm] = useState({ name: '', email: '' });
// State for all users const [isEdit, setIsEdit] = useState(false);
// Redux dispatch const dispatch = useDispatch();
// Redux selector to get data from store const result = useSelector((state) => state.addUserReducer);
// Function to handle window resize
useEffect(() => { // Dispatch action to get all users dispatch(GetAllUserSliceAction());
},[]);
// Function to handle input change const onChange = (e) => { setForm({ ...form, [e.target.name]: e.target.value }); }
// Function to handle form submission const handleSubmit = (e) => { e.preventDefault();
}
// Function to handle edit button click const handleClick = (user) => { setIsEdit(true); setForm(user); }
// Return JSX return ( <>
); };
export default App;
// and this is my reusable component import React, { useState } from 'react'; import { Rnd } from 'react-rnd';
const ResizableAndDraggable = ({ initialSize, initialPosition, onLayoutChange, scale, children }) => { // State to manage the size of the resizable component const [rndSize, setRndSize] = useState(initialSize);
// State to manage the position of the draggable component const [rndPosition, setRndPosition] = useState(initialPosition);
// Function to handle resizing of the component const handleResize = (e, direction, ref, delta, position) => { // Update the size state based on the new size const newSize = { width: ref.style.width, height: ref.style.height, }; setRndSize(newSize); };
// Function to handle dragging of the component const handleDrag = (e, d) => { // Update the position state based on the new position setRndPosition({ x: d.x, y: d.y }); };
return ( <Rnd className='border-[1px] border-white-400 bg-slate-100/50 rounded-md h-full' bounds="parent" draggable lockAspectRatio={false} // onLayoutChange={onLayoutChange} // You can uncomment this if needed onResize={handleResize} onDrag={handleDrag} resizeHandleStyles={{ bottomRight: { bottom: '20px', right: '10px', cursor: 'bottom', width: '16px', height: '16px', }, }} size={{ // Adjust size based on the scale factor width: rndSize.width scale, height: rndSize.height scale, }} position={rndPosition}
export default ResizableAndDraggable; and this is live link https://662ad77ff6a679254276f9b4--polite-buttercream-92ca7d.netlify.app/# and this is my git repo https://github.com/st4rkjatt/dummyFronted