App.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import React, { useEffect, useState } from 'react';
  2. import './App.scss';
  3. const Input = () => {
  4. const [text, setText] = useState('text');
  5. return <div>
  6. <h1>{text}</h1>
  7. <input value={text} onChange={e => setText(e.target.value)} />
  8. </div>
  9. }
  10. const LoginForm = ({onLogin}) => {
  11. const [login, setLogin] = useState('');
  12. const [pswd, setPswd] = useState('');
  13. return <div>
  14. <input value={login} onChange={e => setLogin(e.target.value)}/>
  15. <input value={pswd} onChange={e => setPswd(e.target.value)}/>
  16. <button disabled={login.length<3 || pswd.length<8 ? true:""} onClick={() => onLogin(login,pswd)}>Login</button>
  17. </div>
  18. }
  19. const Counter = ({ms=1000}) => {
  20. const [counter, setCounter] = useState(0)
  21. useEffect(() => {
  22. const interval = setInterval (() => setCounter(counter => counter+1), ms)
  23. return () => {
  24. console.log('DID UNMOUNT')
  25. clearInterval(interval)
  26. }
  27. }, [ms])
  28. /* setInterval (() => setCounter(counter+1), 1000) */
  29. return (
  30. <div>{counter}</div>
  31. )
  32. }
  33. const Spoiler = ({header="+",children, opened=true}) => {
  34. const [open, setOpen] = useState(opened)
  35. return (
  36. <div>
  37. <div onClick={()=>setOpen(!open)}>{header}</div>
  38. {open && children}
  39. </div>
  40. )
  41. }
  42. const Counters = () => {
  43. const [cntrs, setCntrs] = useState([])
  44. const [ms, setMs] = useState(1000)
  45. return (
  46. <div>
  47. <button onClick={() => setMs(ms+100)}>+</button>
  48. <button onClick={() => setMs(ms-100)}>-</button>
  49. {cntrs.map(() => <Counter/>)}
  50. <button onClick={() => setCntrs([...cntrs, cntrs.length])}>Add</button>
  51. </div>
  52. )
  53. }
  54. const RangeInput = ({min,max}) => {
  55. const [value, SetValue] = useState(0);
  56. let color = "white";
  57. /* useEffect(() => {
  58. if(value < min || value > max){
  59. color = "red";
  60. console.log(color);
  61. } else {
  62. color = '';
  63. }
  64. }, [value]) */
  65. if(value < min || value > max) {
  66. color = "red"
  67. } else {
  68. color = "";
  69. }
  70. return (
  71. <input onChange={e => SetValue(e.target.value.length)} style={{backgroundColor:color}}/>
  72. )
  73. }
  74. const PasswordConfirm = ({min}) => {
  75. const[value1,SetValue1] = useState('');
  76. const[value2,SetValue2] = useState('');
  77. let color = "";
  78. let pattern = /^[0-9a-zA-Z]+$/;
  79. if(value1.length > min && value2.length > min && value1.match(pattern) && value2.match(pattern) && value1 === value2) {
  80. color="green"
  81. } else {
  82. color="red";
  83. }
  84. return(
  85. <div>
  86. <input type="password" onChange={e => SetValue1(e.target.value)} style={{borderColor:color}}/>
  87. <input type="password" onChange={e => SetValue2(e.target.value)} style={{borderColor:color}}/>
  88. </div>
  89. )
  90. }
  91. class Timer extends React.Component {
  92. constructor(props) {
  93. super(props)
  94. this.state = {seconds: 0,
  95. minutes:0,
  96. hour:0,
  97. propsTime: props.time
  98. }
  99. this.Pause = this.Pause.bind(this);
  100. this.tick = this.tick.bind(this);
  101. }
  102. static getDerivedStateFromProps(props, state){
  103. console.log('get derived state');
  104. return /* ({propsTime: props.time}) */ null
  105. }
  106. componentDidMount() {
  107. this.timerID = setInterval(this.tick,1000)
  108. }
  109. Pause() {
  110. clearInterval(this.timerID);
  111. }
  112. tick() {
  113. let t = this.state.propsTime -1
  114. let hours = Math.floor(t/3600);
  115. let minutes = Math.floor((t - hours*3600)/60);
  116. let seconds = Math.floor(t - hours*3600-minutes*60)
  117. this.setState({
  118. seconds,minutes,hours, propsTime: t
  119. })
  120. /* this.setState({
  121. seconds: this.state.seconds - 1
  122. }) */
  123. /* console.log(t); */
  124. if (t <= 0){
  125. clearInterval(this.timerID)
  126. }
  127. }
  128. render() {
  129. const {seconds, minutes, hours} = this.state
  130. return(
  131. <div>
  132. <h1>TIMER</h1>
  133. <span>hours :{hours} </span>
  134. <span>minutes: {minutes} </span>
  135. <span>seconds: {seconds}</span>
  136. <button onClick={this.Pause}>Pause</button>
  137. </div>
  138. )
  139. }
  140. }
  141. class TimerControl extends React.Component {
  142. constructor(props){
  143. super(props)
  144. this.state = {hours:0,
  145. minutes:0,
  146. seconds:0,
  147. toSeconds:0,
  148. addTimer: false}
  149. this.handlerHours = this.handlerHours.bind(this);
  150. this.handlerMinutes = this.handlerMinutes.bind(this);
  151. this.handlerSeconds = this.handlerSeconds.bind(this);
  152. this.handleAddTimer = this.handleAddTimer.bind(this);
  153. }
  154. handlerHours(e){
  155. let hourToSeconds = +e*3600
  156. console.log(hourToSeconds);
  157. this.setState({hours: hourToSeconds})
  158. }
  159. handlerMinutes(e){
  160. let minuteToSeconds = +e*60
  161. console.log(minuteToSeconds);
  162. this.setState({minutes: minuteToSeconds})
  163. }
  164. handlerSeconds(e){
  165. console.log(+e);
  166. this.setState({seconds: +e})
  167. }
  168. handleAddTimer() {
  169. let allTime = this.state.hours + this.state.minutes + this.state.seconds
  170. this.setState({addTimer:!this.state.addTimer, toSeconds: allTime})
  171. }
  172. render() {
  173. return(
  174. <div>
  175. <label>hours <input type="number" max="24" min="0" onChange={e => this.handlerHours(e.target.value)}/></label>
  176. <label>minutes <input type="number" max="60" min="0" onChange={e => this.handlerMinutes(e.target.value)}/></label>
  177. <label>seconds <input type="number" max="60" min="0" onChange={e => this.handlerSeconds(e.target.value)}/></label>
  178. <button onClick={this.handleAddTimer}>Start</button>
  179. {this.state.addTimer && <Timer time={this.state.toSeconds}/>}
  180. </div>
  181. )
  182. }
  183. }
  184. const SecondsTimer = ({seconds}) => <h2>{seconds}</h2>
  185. class TimerContainer extends React.Component {
  186. constructor(props){
  187. super(props)
  188. this.state = {
  189. secToEnd:0,
  190. seconds: this.props.seconds,
  191. refresh: this.props.refresh
  192. }
  193. this.tick = this.tick.bind(this);
  194. }
  195. componentDidMount() {
  196. this.timerID = setInterval(this.tick, this.state.refresh)
  197. }
  198. tick() {
  199. let startTime = performance.now();
  200. this.setState({seconds: this.state.seconds - ((performance.now() - startTime)/1000)})
  201. /* let setSec = this.state.seconds - Math.floor((performance.now() - startTime)/1000);
  202. console.log(setSec); */
  203. }
  204. render(){
  205. const Render = this.props.render
  206. return(
  207. <div>
  208. <Render seconds={this.state.seconds}/>
  209. </div>
  210. )
  211. }
  212. }
  213. function App () {
  214. return(
  215. <div className='App'>
  216. {/* <Input/> */}
  217. {/* <LoginForm onLogin={(login, password) => console.log(login,password)}/> */}
  218. <Spoiler header={<h1>Hello</h1>} opened={false}>
  219. <h1>Hello</h1>
  220. <p>blabla</p>
  221. </Spoiler>
  222. <RangeInput min={2} max={10}/>
  223. <PasswordConfirm min={2}/>
  224. <TimerControl/>
  225. <Timer time={10000}/>
  226. <TimerContainer seconds={1800} refresh={100} render={SecondsTimer}/>
  227. </div>
  228. );
  229. }
  230. export default App;