index.html

<!DOCTYPE html>
<html>
    <body>
        <div id = "root"></div>
    </body>
    <script src="<https://unpkg.com/[email protected]/umd/react.production.min.js>"></script>
    <script src="<https://unpkg.com/[email protected]/umd/react-dom.production.min.js>"></script>
    <script src="<https://unpkg.com/@babel/standalone/babel.min.js>"></script>
    <script type="text/babel">
        function MinutesToHours() {
            const [amount, setAmount] = React.useState(0);
            const [flipped, setFlipped] = React.useState(false);
            const onChange = (event) => {
                setAmount(event.target.value);
            };
            const reset = () => setAmount(0);
            const onFlip = () => {
                reset();
                setFlipped((current) => !current);
            };
            return (
                <div>
                    <div>
                        <label htmlFor="minutes">Minutes</label>
                        <input
                            id="minutes"
                            value={flipped ? amount * 60 : amount}
                            placeholder="Minutes"
                            type="number"
                            onChange={onChange}
                            disabled={flipped}
                        />
                    </div>
                    <div>
                        <label htmlFor="hours">Hours</label>
                        <input
                            id="hours"
                            value={flipped ? amount : Math.round(amount / 60)}
                            placeholder="Hours"
                            type="number"
                            onChange={onChange}
                            disabled={!flipped}
                        />
                    </div>
                    <button onClick={reset}>Reset</button>
                    <button onClick={onFlip}>{flipped ? "minute -> hour" : "hour -> minute"}</button>
                </div>
            );
        }
        function KmToMiles() {
            const [amount, setAmount] = React.useState(0);
            const [flipped, setFlipped] = React.useState(false);
            const onChange = (event) => {
                setAmount(event.target.value);
            };
            const reset = () => setAmount(0);
            const onFlip = () => {
                reset();
                setFlipped((current) => !current);
            };
            return (
                <div>
                    <div>
                        <label htmlFor="Km">Km</label>
                        <input
                            id="Km"
                            value={flipped ? amount / 0.62137 : amount}
                            placeholder="Km"
                            type="number"
                            onChange={onChange}
                            disabled={flipped}
                        />
                    </div>
                    <div>
                        <label htmlFor="miles">Miles</label>
                        <input
                            id="miles"
                            value={flipped ? amount : amount * 0.62137}
                            placeholder="Miles"
                            type="number"
                            onChange={onChange}
                            disabled={!flipped}
                        />
                    </div>
                    <button onClick={reset}>Reset</button>
                    <button onClick={onFlip}>{flipped ? "km -> miles" : "km -> minute"}</button>
                </div>
            );
        }
        function App() {
            const [index, setIndex] = React.useState("xx");
            const onSelect = (event) => {
                setIndex(event.target.value);
            };
            return (
                <div>
                    <h1>Super Converter</h1>
                    <select value={index} onChange={onSelect}>
                        <option value="xx">--Select your units--</option>
                        <option value="0">Minutes & Hours</option>
                        <option value="1">Km & Miles</option>
                    </select>
                    <hr />
                    {index === "0" ? <MinutesToHours /> : null}
                    {index === "1" ? <KmToMiles /> : null}
                </div>
            );
        }
        const root = document.getElementById("root");
        ReactDOM.render(<App />, root);
    </script>
</html>