screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState } = React; const App = () => { const [file, setFile] = useState(null); const handleFileChange = (event) => { const selectedFile = event.target.files[0]; if (selectedFile) { const reader = new FileReader(); reader.onload = () => { const fileContent = reader.result; if (fileContent.length > 0) { // File is not empty setFile(selectedFile); } else { // File is empty setFile(null); } }; reader.readAsText(selectedFile); } else { // No file selected setFile(null); } }; const handleSubmit = (event) => { event.preventDefault(); // Perform the form submission or further processing here }; return ( <div className="container"> <h3>React js check if input file is empty </h3> <input type="file" onChange={handleFileChange} /> <button type="submit" onClick={handleSubmit} disabled={!file}> Submit </button> </div> ); }; ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { display: flex; flex-direction: column; align-items: center; padding: 20px; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); width: 400px; margin: 0 auto; } input[type="file"] { margin-bottom: 10px; } button[type="submit"] { padding: 10px 20px; background-color: #3498db; color: #fff; border: none; border-radius: 4px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease; } button[type="submit"]:disabled { background-color: #ccc; cursor: not-allowed; } button[type="submit"]:hover { background-color: #2980b9; } </style> </body> </html>