123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- import userEvent from '@testing-library/user-event'
- import React, { Component, useState, useRef, useEffect } from 'react'
- import './App.css'
- // import Header from './components/Header';
- // import Navbar from './components/Navbar';
- // import Profile from './components/Profile';
- const Input = () => {
- const [value, setValue] = useState('asdf')
- return (
- <>
- <input value={value} onChange={e => setValue(e.target.value)}/>
- <p>{value.toUpperCase()}</p>
- </>
- )
- }
- const Spoiler = ({children, header:Header}) => {
- const [show, setShow] = useState(false)
- return (
- <>
- <Header onClick={() => setShow(!show)}/>
- {show && children}
- </>
- )
- }
- const RangeInput = ({min, max}) => {
-
- const [value, setValue] = useState("notRed")
- return (
- <>
- <input type="text" value={value} style = {{backgroundColor: (value.length < min) || (value.length > max) ? 'red' : 'white' }} onChange={e => {
- setValue(e.target.value);
-
- }}/>
- </>
- )
- }
- const PasswordConfirm = ({min}) => {
- const [firstPass, setValue] = useState("123");
- const [secondPass, setsecValue] = useState("123");
- const [color, setColorValue] = useState("red");
- const checkPassword = (newVal, confirmVal) => {
- return (newVal.length > min && confirmVal.length > min && newVal == confirmVal) ? "green" : "red";
- }
-
- return (
- <>
- <h3 style = {{color: color}} >{color}</h3>
- <input type="password" value = {firstPass} style = {{backgroundColor: color}} onChange = {(e) => {
- setValue(e.target.value);
- setColorValue(checkPassword(e.target.value, secondPass));
- }}
- />
-
- <input type="password"value = {secondPass} style = {{backgroundColor: color}} onChange = {(e) => {
- setsecValue(e.target.value);
- setColorValue(checkPassword(e.target.value, firstPass));
- }}/>
- </>
- )
- }
- const Timer = ({sec}) => {
- var h = Math.floor(sec / 3600);
- var m = Math.floor(sec % 3600 / 60);
- var s = Math.floor(sec % 3600 % 60);
- const [count, changeCount] = useState(`${h}: ${m}: ${s}`);
- const [lock, closer] = useState(true);
- const [onOff, turning] = useState(false);
- const countRef = useRef(onOff);
- return (
- <>
- <h3>Timer {count}</h3>
- <button onClick = {() => {
- turning(!onOff);
- countRef.current = onOff;
-
- lock && (() => {
- setInterval((() => {
- if(sec && countRef.current == false) {
- sec -=1;
- h = Math.floor(sec / 3600);
- m = Math.floor(sec % 3600 / 60);
- s = Math.floor(sec % 3600 % 60);
- changeCount(`${h}: ${m}: ${s}`);
- } else {
- return
- }
- }), 1000);
- })();
- closer(false);
-
- }}>Start/pause</button>
- </>
- )
- }
- const TimerControl = ({}) => {
- const [hours, setHours] = useState("1");
- const [minutes, setMinutes] = useState("1");
- const [sec, setSec] = useState("1");
- const [buttOn, turnOnOff] = useState(true);
- const [countDownSown, show] = useState(false);
- const checkNumbers = (newVal, confirmVal) => {
- return (confirmVal.includes(".") || newVal.includes(".") || newVal < 0 || +confirmVal < 0 || +newVal > 60 || +confirmVal > 60) ? turnOnOff(false) : turnOnOff(true);
- }
-
- return (
- <>
- <h3>Timer Control</h3>
- <div>
- <input type = "number" id = "hours" value = {hours} onChange={(e) => {
- setHours(e.target.value)}}/>
- <input type = "number" id = "minutes" value = {minutes} style = {{backgroundColor: (minutes < 0) || (minutes > 59) || minutes.includes(".") ? 'red' : 'white' }} onChange={(e) => {
- setMinutes(e.target.value);
- checkNumbers(e.target.value, sec)}}/>
- <input type = "number" id = "sec" value = {sec} style = {{backgroundColor: (sec < 0) || (sec > 59) || (sec.includes(".")) ? 'red' : 'white' }} onChange={(e) => {
- setSec(e.target.value);
- checkNumbers(e.target.value, minutes)}}/>
- <button disabled={!buttOn} onClick={ () => show(true) }>Start/pause</button>
- <div id = "timer">
- { countDownSown && <Timer sec={((+hours * 60 * 60) + (+minutes * 60) + +sec)} /> }
- </div>
- </div>
-
- </>
-
- )
- }
- const SecondsTimer = ({seconds}) => <h2>{seconds}</h2>;
- const TimerContainer = ({seconds, refresh, render}) => {
- const Render = render;
- const [count, setCount] = useState(300);
- useEffect(() => {
- var firstState = Date.now()
- var secCounter;
- var interval = setInterval(() => {
- secCounter = seconds - (Math.floor((Date.now()/1000) - firstState/1000));
-
- setCount(secCounter);
- if (secCounter < 0) {
- setCount(0)
- return clearInterval(interval)
- }
- }, refresh);
- }, []);
- return (
- <>
- <Render seconds = {count}/>
- </>
- )
- }
- const Timer2 = ({seconds}) => {
- var h = Math.floor(seconds / 3600);
- var m = Math.floor(seconds % 3600 / 60);
- var s = Math.floor(seconds % 3600 % 60);
- return (
- <>
- <h3>Timer {`${h}: ${m}: ${s}`}</h3>
- <button onClick = {() => {
-
-
- }}>Start/pause</button>
- </>
- )
- }
- const Watches = ({seconds}) => {
- var h = Math.floor(seconds / 3600) * 30;
- var m = Math.floor(seconds % 3600 / 60) * (360 / 60);
- var s = Math.floor(seconds % 3600 % 60) * (360 / 60);
- return (
- <>
- <div id = "container" style = {{position: "relative"}}>
- <img id = "clock" src = "http://draw.asmer.fe.a-level.com.ua/ClockFace/ClockFace.png" style = {{position: "absolute", zIndex :"0"}}/>
- <img id = "hours" src = "http://draw.asmer.fe.a-level.com.ua/ClockFace/ClockFace_H.png" style = {{position: "absolute", zIndex :"1", transform: `rotate(${h}deg)`}}/>
- <img id = "minutes" src = "http://draw.asmer.fe.a-level.com.ua/ClockFace/ClockFace_M.png" style = {{position: "absolute", zIndex :"2", transform: `rotate(${m}deg)`}}/>
- <img id = "secunds" src = "http://draw.asmer.fe.a-level.com.ua/ClockFace/ClockFace_S.png"style = {{position: "absolute", zIndex :"3", transform: `rotate(${s}deg)`}}/>
- </div>
- </>
- )
- }
- const TimerContainerControl = ({refresh, render}) => {
- const [hours, setHours] = useState("1");
- const [minutes, setMinutes] = useState("1");
- const [sec, setSec] = useState("1");
- const [buttOn, turnOnOff] = useState(true);
- const [countDownSown, show] = useState(false);
- const [msc, letsCount] = useState("0");
- const Render = render;
- const checkNumbers = (newVal, confirmVal) => {
- return (confirmVal.includes(".") || newVal.includes(".") || newVal < 0 || +confirmVal < 0 || +newVal > 60 || +confirmVal > 60) ? turnOnOff(false) : turnOnOff(true);
- }
-
- useEffect(() => {
- if (!countDownSown) {
- return clearInterval(interval)
- }
- var firstState = Date.now()
- var secCounter;
-
- var interval = setInterval(() => {
-
- secCounter = msc + (Math.floor((Date.now()/1000) - firstState/1000));
- letsCount(secCounter);
-
- }, refresh);
-
- }, [countDownSown]);
- return (
- <>
- <h3>Timer Control</h3>
- <div>
- <input type = "number" id = "hours" value = {hours} onChange={(e) => {
- setHours(e.target.value)}}/>
- <input type = "number" id = "minutes" value = {minutes} style = {{backgroundColor: (minutes < 0) || (minutes > 59) || minutes.includes(".") ? 'red' : 'white' }} onChange={(e) => {
- setMinutes(e.target.value);
- checkNumbers(e.target.value, sec)}}/>
- <input type = "number" id = "sec" value = {sec} style = {{backgroundColor: (sec < 0) || (sec > 59) || (sec.includes(".")) ? 'red' : 'white' }} onChange={(e) => {
- setSec(e.target.value);
- checkNumbers(e.target.value, minutes)}}/>
- <button disabled={!buttOn} onClick={ () => {
- show(true);
- letsCount(((+hours * 60 * 60) + (+minutes * 60) + +sec))
- }}>Start/pause</button>
- <div id = "timer">
- { countDownSown && <Render seconds={msc} /> }
- </div>
- </div>
-
- </>
-
- )
- }
- const App = () => {
-
- return (
- <>
- <div className = "spoiler">
- <Spoiler header={({ ...props }) => <h2 {...props}>Show</h2>}>
- <Input/>
- <Input />
- <Spoiler header={({ onClick }) => <button onClick={onClick}>Show</button>}>
- <Input />
- <Input />
- </Spoiler>
- </Spoiler>
- </div>
- <div id = "rangeInput">
- <h2>Range Input</h2>
- <RangeInput min={2} max={10} />
- </div>
- <div id = "passwordConfirm">
- <PasswordConfirm min = {3} />
- </div>
- <div id = "timerControl">
- <TimerControl/>
- </div>
- <div id = "timerContainer">
- <h3>Timer Container</h3>
- <TimerContainer seconds={1800} refresh={100} render={SecondsTimer}/>
- </div>
- <div id = "LSD">
- <h3>LSD</h3>
- <TimerContainer seconds={300} refresh={100} render={Timer2}/>
- </div>
- <div id = "Watches">
- <h3>Watches + containerControl</h3>
- <TimerContainerControl refresh={100} render={Watches}/>
- </div>
- </>
- );
- }
- export default App;
|