App.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import React from 'react';
  2. class Spoiler extends React.Component {
  3. constructor(props) {
  4. super(props)
  5. this.state = { open: "true" };
  6. this.press = this.press.bind(this);
  7. }
  8. spoilerHide() {
  9. if (this.state.open === "true") {
  10. return <div>{this.props.children}</div>
  11. } else {
  12. return <div></div>
  13. }
  14. }
  15. press() {
  16. let stateChange = (this.state.open === "true") ? "false" : "true"
  17. this.setState({
  18. open: stateChange
  19. });
  20. }
  21. render() {
  22. return (
  23. <>
  24. <div onClick={this.press}>{this.props.header}</div>
  25. <div>{this.spoilerHide()}</div>
  26. </>
  27. );
  28. }
  29. }
  30. //
  31. class RangeInput extends React.Component {
  32. constructor(props) {
  33. super(props)
  34. this.state = { text: "" }
  35. }
  36. render() {
  37. return (
  38. <input value={this.state.text} onChange={(e) => {
  39. (e.target.value.length < this.props.min + 1 || e.target.value.length > this.props.max - 1) ? (e.target.style.backgroundColor = "red")
  40. : (e.target.style.backgroundColor = "white")
  41. this.setState({ text: e.target.value })
  42. }} />
  43. )
  44. }
  45. }
  46. //
  47. class PasswordConfirm extends React.Component {
  48. constructor(props) {
  49. super(props)
  50. this.state = {
  51. password: "",
  52. passwordConfirmed: ""
  53. }
  54. }
  55. render() {
  56. return (
  57. <>
  58. <form>
  59. <input placeholder="enter password..." type='password' value={this.state.password} onChange={(e) => {
  60. this.setState({ password: e.target.value })
  61. }} />
  62. <input placeholder="enter password again..." type='password' value={this.state.passwordConfirmed} onChange={(e) => {
  63. this.setState({ passwordConfirmed: e.target.value })
  64. }} />
  65. <button disabled={!(this.state.password.length > this.props.min && this.state.password === this.state.passwordConfirmed)}>submit</button>
  66. </form>
  67. </>
  68. )
  69. }
  70. }
  71. //
  72. class Timer extends React.Component {
  73. constructor(props) {
  74. super(props)
  75. this.state = {
  76. time: {},
  77. sec: this.props.sec,
  78. pause: "false"
  79. }
  80. this.pause = this.pause.bind(this);
  81. }
  82. secondsToTime(secs) {
  83. let hours = Math.floor(secs / (60 * 60))
  84. let divisor_for_minutes = secs % (60 * 60);
  85. let minutes = Math.floor(divisor_for_minutes / 60);
  86. let divisor_for_seconds = divisor_for_minutes % 60;
  87. let seconds = Math.ceil(divisor_for_seconds);
  88. let obj = {
  89. "h": hours,
  90. "m": minutes,
  91. "s": seconds
  92. };
  93. return obj;
  94. }
  95. pause() {
  96. let statePause = (this.state.pause === "true") ? "false" : "true"
  97. this.setState({
  98. pause: statePause
  99. });
  100. }
  101. tick() {
  102. if (this.state.pause === "false") {
  103. this.setState({
  104. sec: this.state.sec - 1,
  105. time: this.secondsToTime(this.state.sec - 1)
  106. });
  107. } else {
  108. this.setState({
  109. sec: this.state.sec,
  110. time: this.secondsToTime(this.state.sec)
  111. });
  112. }
  113. }
  114. componentDidMount() {
  115. let timeLeftVar = this.secondsToTime(this.state.seconds);
  116. this.setState({ time: timeLeftVar });
  117. this.timerID = setInterval(
  118. () => this.tick(),
  119. 1000
  120. );
  121. }
  122. componentWillUnmount() {
  123. clearInterval(this.timerID);
  124. }
  125. render() {
  126. return (
  127. <>
  128. <div>Hrs: {this.state.time.h} min: {this.state.time.m} sec: {this.state.time.s}</div>
  129. <button onClick={this.pause}>pause/start</button>
  130. </>
  131. )
  132. }
  133. }
  134. //
  135. // class TimerControl extends React.Component {
  136. // constructor(props) {
  137. // super(props)
  138. // this.state = {
  139. // hours: 0,
  140. // minutes: 0,
  141. // sec: 100,
  142. // pause: "false"
  143. // }
  144. // debugger;
  145. // this.pause = this.pause.bind(this);
  146. // }
  147. // secondsToTime(secs) {
  148. // debugger
  149. // if (this.state.sec != 0) {
  150. // let hoursCalc = Math.floor(secs / (60 * 60))
  151. // let divisor_for_minutes = secs % (60 * 60);
  152. // let minutesCalc = Math.floor(divisor_for_minutes / 60);
  153. // let divisor_for_seconds = divisor_for_minutes % 60;
  154. // let secondsCalc = Math.ceil(divisor_for_seconds);
  155. // this.setState({
  156. // hours: hoursCalc,
  157. // minutes: minutesCalc,
  158. // sec: secondsCalc,
  159. // });
  160. // } else {
  161. // }
  162. // }
  163. // //
  164. // // hoursToTime(hours) {
  165. // // let hoursCalc = hours
  166. // // let secondsCalc = Math.floor(hours * 3600)
  167. // // let minutesCalc = Math.floor(hours * 60)
  168. // // //let divisor_for_seconds = divisor_for_minutes % 60;
  169. // // // let divisor_for_minutes = hours % (60 * 60);
  170. // // // let minutesCalc = Math.floor(divisor_for_minutes / 60);
  171. // // // let secondsCalc = Math.ceil(divisor_for_seconds);
  172. // // this.setState({
  173. // // hours: hoursCalc,
  174. // // minutes: minutesCalc,
  175. // // sec: secondsCalc,
  176. // // });
  177. // // }
  178. // pause() {
  179. // let statePause = (this.state.pause === "true") ? "false" : "true"
  180. // this.setState({
  181. // pause: statePause
  182. // });
  183. // }
  184. // tick() {
  185. // if (this.state.pause === "false") {
  186. // this.setState({
  187. // // hours: this.state.hours,
  188. // // minutes: this.state.minutes,
  189. // sec: this.secondsToTime(this.state.sec - 1)
  190. // });
  191. // } else {
  192. // this.setState({
  193. // // hours: this.state.hours,
  194. // // minutes: this.state.minutes,
  195. // sec: this.state.sec
  196. // });
  197. // }
  198. // }
  199. // componentDidMount() {
  200. // debugger;
  201. // let timeLeftVar = this.secondsToTime(this.state.sec);
  202. // this.setState({ sec: timeLeftVar });
  203. // this.timerID = setInterval(
  204. // () => this.tick(),
  205. // 1000
  206. // );
  207. // }
  208. // componentWillUnmount() {
  209. // clearInterval(this.timerID);
  210. // }
  211. // render() {
  212. // return (
  213. // <>
  214. // <div>
  215. // <form>
  216. // <input type="number" placeholder="h" onChange={(e) => {
  217. // this.setState({ hours: e.target.value })
  218. // }} />
  219. // <input type="number" placeholder="m" onChange={(e) => {
  220. // this.setState({ minutes: e.target.value })
  221. // }} />
  222. // <input type="number" placeholder="s" onChange={(e) => {
  223. // this.setState({ sec: e.target.value })
  224. // }} />
  225. // </form>
  226. // </div>
  227. // <div>
  228. // <div>Hrs: {this.state.hours} min: {this.state.minutes} sec: {this.state.sec}</div>
  229. // <button onClick={this.pause}>pause/start</button>
  230. // </div>
  231. // </>
  232. // )
  233. // }
  234. // }
  235. const App = () => {
  236. return (
  237. <>
  238. <Spoiler header={<h1>Заголовок</h1>} open>
  239. Контент 1
  240. <p>
  241. лорем ипсум траливали и тп.
  242. </p>
  243. </Spoiler>
  244. <RangeInput min={2} max={10} />
  245. <PasswordConfirm min={2} />
  246. <Timer sec={3606} />
  247. </>
  248. )
  249. }
  250. export default App;