123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- import React, { useEffect, useState } from 'react';
- import './App.scss';
- const Input = () => {
- const [text, setText] = useState('text');
- return <div>
- <h1>{text}</h1>
- <input value={text} onChange={e => setText(e.target.value)} />
- </div>
-
- }
- const LoginForm = ({onLogin}) => {
- const [login, setLogin] = useState('');
- const [pswd, setPswd] = useState('');
- return <div>
- <input value={login} onChange={e => setLogin(e.target.value)}/>
- <input value={pswd} onChange={e => setPswd(e.target.value)}/>
- <button disabled={login.length<3 || pswd.length<8 ? true:""} onClick={() => onLogin(login,pswd)}>Login</button>
- </div>
- }
- const Counter = ({ms=1000}) => {
- const [counter, setCounter] = useState(0)
- useEffect(() => {
- const interval = setInterval (() => setCounter(counter => counter+1), ms)
- return () => {
- console.log('DID UNMOUNT')
- clearInterval(interval)
- }
- }, [ms])
- /* setInterval (() => setCounter(counter+1), 1000) */
- return (
- <div>{counter}</div>
- )
- }
- const Spoiler = ({header="+",children, opened=true}) => {
- const [open, setOpen] = useState(opened)
- return (
- <div>
- <div onClick={()=>setOpen(!open)}>{header}</div>
- {open && children}
- </div>
- )
- }
- const Counters = () => {
- const [cntrs, setCntrs] = useState([])
- const [ms, setMs] = useState(1000)
- return (
- <div>
- <button onClick={() => setMs(ms+100)}>+</button>
- <button onClick={() => setMs(ms-100)}>-</button>
- {cntrs.map(() => <Counter/>)}
- <button onClick={() => setCntrs([...cntrs, cntrs.length])}>Add</button>
- </div>
- )
- }
- const RangeInput = ({min,max}) => {
- const [value, SetValue] = useState(0);
- let color = "white";
-
- /* useEffect(() => {
- if(value < min || value > max){
- color = "red";
- console.log(color);
- } else {
- color = '';
- }
- }, [value]) */
- if(value < min || value > max) {
- color = "red"
-
- } else {
- color = "";
- }
- return (
- <input onChange={e => SetValue(e.target.value.length)} style={{backgroundColor:color}}/>
- )
- }
- const PasswordConfirm = ({min}) => {
- const[value1,SetValue1] = useState('');
- const[value2,SetValue2] = useState('');
- let color = "";
- let pattern = /^[0-9a-zA-Z]+$/;
- if(value1.length > min && value2.length > min && value1.match(pattern) && value2.match(pattern) && value1 === value2) {
- color="green"
- } else {
- color="red";
- }
- return(
- <div>
- <input type="password" onChange={e => SetValue1(e.target.value)} style={{borderColor:color}}/>
- <input type="password" onChange={e => SetValue2(e.target.value)} style={{borderColor:color}}/>
- </div>
- )
- }
- class Timer extends React.Component {
-
- constructor(props) {
- super(props)
- this.state = {seconds: 0,
- minutes:0,
- hour:0,
- propsTime: props.time
- }
- this.Pause = this.Pause.bind(this);
- this.tick = this.tick.bind(this);
- }
-
- static getDerivedStateFromProps(props, state){
- console.log('get derived state');
- return /* ({propsTime: props.time}) */ null
- }
-
- componentDidMount() {
- this.timerID = setInterval(this.tick,1000)
- }
- Pause() {
- clearInterval(this.timerID);
- }
-
- tick() {
- let t = this.state.propsTime -1
- let hours = Math.floor(t/3600);
- let minutes = Math.floor((t - hours*3600)/60);
- let seconds = Math.floor(t - hours*3600-minutes*60)
- this.setState({
- seconds,minutes,hours, propsTime: t
- })
- /* this.setState({
- seconds: this.state.seconds - 1
- }) */
- /* console.log(t); */
- if (t <= 0){
- clearInterval(this.timerID)
- }
- }
-
- render() {
- const {seconds, minutes, hours} = this.state
- return(
- <div>
- <h1>TIMER</h1>
- <span>hours :{hours} </span>
- <span>minutes: {minutes} </span>
- <span>seconds: {seconds}</span>
- <button onClick={this.Pause}>Pause</button>
- </div>
- )
- }
- }
- class TimerControl extends React.Component {
- constructor(props){
- super(props)
- this.state = {hours:0,
- minutes:0,
- seconds:0,
- toSeconds:0,
- addTimer: false}
- this.handlerHours = this.handlerHours.bind(this);
- this.handlerMinutes = this.handlerMinutes.bind(this);
- this.handlerSeconds = this.handlerSeconds.bind(this);
- this.handleAddTimer = this.handleAddTimer.bind(this);
- }
- handlerHours(e){
- let hourToSeconds = +e*3600
- console.log(hourToSeconds);
- this.setState({hours: hourToSeconds})
- }
- handlerMinutes(e){
- let minuteToSeconds = +e*60
- console.log(minuteToSeconds);
- this.setState({minutes: minuteToSeconds})
- }
- handlerSeconds(e){
- console.log(+e);
- this.setState({seconds: +e})
- }
- handleAddTimer() {
- let allTime = this.state.hours + this.state.minutes + this.state.seconds
- this.setState({addTimer:!this.state.addTimer, toSeconds: allTime})
- }
- render() {
- return(
- <div>
- <label>hours <input type="number" max="24" min="0" onChange={e => this.handlerHours(e.target.value)}/></label>
- <label>minutes <input type="number" max="60" min="0" onChange={e => this.handlerMinutes(e.target.value)}/></label>
- <label>seconds <input type="number" max="60" min="0" onChange={e => this.handlerSeconds(e.target.value)}/></label>
- <button onClick={this.handleAddTimer}>Start</button>
- {this.state.addTimer && <Timer time={this.state.toSeconds}/>}
- </div>
- )
- }
- }
- const SecondsTimer = ({seconds}) => <h2>{seconds}</h2>
- class TimerContainer extends React.Component {
- constructor(props){
- super(props)
- this.state = {
- secToEnd:0,
- seconds: this.props.seconds,
- refresh: this.props.refresh
- }
- this.tick = this.tick.bind(this);
- }
-
- componentDidMount() {
- this.timerID = setInterval(this.tick, this.state.refresh)
- }
- tick() {
- let startTime = performance.now();
- this.setState({seconds: this.state.seconds - ((performance.now() - startTime)/1000)})
-
- /* let setSec = this.state.seconds - Math.floor((performance.now() - startTime)/1000);
- console.log(setSec); */
-
- }
- render(){
- const Render = this.props.render
- return(
- <div>
- <Render seconds={this.state.seconds}/>
- </div>
- )
- }
- }
- function App () {
- return(
- <div className='App'>
- {/* <Input/> */}
- {/* <LoginForm onLogin={(login, password) => console.log(login,password)}/> */}
- <Spoiler header={<h1>Hello</h1>} opened={false}>
- <h1>Hello</h1>
- <p>blabla</p>
- </Spoiler>
- <RangeInput min={2} max={10}/>
- <PasswordConfirm min={2}/>
- <TimerControl/>
- <Timer time={10000}/>
- <TimerContainer seconds={1800} refresh={100} render={SecondsTimer}/>
- </div>
- );
- }
- export default App;
|