|
1 | 1 | import React, { Component } from 'react' |
2 | 2 | import { InputHandler } from '../InputHandler/InputHandler.js' |
| 3 | +import SelectionSort from '../Strategy/Algorithms/SelectionSort.js'; |
| 4 | +import { Strategy } from '../Strategy/Strategy.js'; |
3 | 5 | import { Options } from './options/Options.js'; |
4 | | - |
| 6 | +import { ReactComponent as Play } from '../../img/play.svg' |
| 7 | +import { ReactComponent as Replay } from '../../img/replay.svg' |
5 | 8 | class InputField extends Component { |
6 | 9 |
|
7 | 10 | constructor(props) { |
8 | 11 | super(props); |
9 | 12 | this.InputHandler = new InputHandler(); |
10 | 13 | this.name = props.name; |
| 14 | + this.selectionSort = new SelectionSort(); |
| 15 | + this.Strategy = new Strategy(); |
| 16 | + this.state = { |
| 17 | + algorithm: '' |
| 18 | + } |
11 | 19 | } |
12 | 20 |
|
13 | 21 |
|
14 | 22 | render() { |
15 | | - const {inputArrayLength} = this.props.InputHandler.state; |
| 23 | + const {inputArrayLength, options} = this.props.InputHandler.state; |
| 24 | + const { algorithm } = this.state; |
| 25 | + |
| 26 | + |
16 | 27 |
|
17 | 28 | return ( |
18 | | - <div className={`flex items-center justify-around border-b p-3 w-sreen flex-wrap ${this.name[0] + ' '}`}> |
19 | | - <div className="flex flex-col justify-center items-center h-full mb-3 sm:mb-0"> |
20 | | - <div className="flex"> |
21 | | - <input placeholder={`Array size (MAX ${InputHandler.getAllowedMaxInputSize()})`} value={inputArrayLength === 0 ? '' : inputArrayLength} onChange={(e) => { |
22 | | - // SG 07/07/2022 21:03 Input validation, only accepts a number that is less than the allowed array size |
23 | | - (Number(e.target.value) || !e.target.value) |
24 | | - && (e.target.value <= InputHandler.getAllowedMaxInputSize()) |
25 | | - ? this.props.InputHandler.setState({inputArrayLength: e.target.value.replace(/\D/g, '')}) |
26 | | - : this.props.InputHandler.setState({inputArrayLength: InputHandler.getAllowedMaxInputSize()}) |
| 29 | + <div className="flex flex-col"> |
| 30 | + |
| 31 | + <div className={`flex items-center justify-around p-3 w-sreen gap-4 flex-wrap ${this.name[0] + ' '}`}> |
| 32 | + <div className="flex flex-col justify-center items-center h-full sm:mb-0"> |
| 33 | + <div className="flex"> |
| 34 | + <input placeholder={`Array size (MAX ${InputHandler.getAllowedMaxInputSize()})`} value={inputArrayLength === 0 ? '' : inputArrayLength} onChange={(e) => { |
| 35 | + // SG 07/07/2022 21:03 Input validation, only accepts a number that is less than the allowed array size |
| 36 | + (Number(e.target.value) || !e.target.value) |
| 37 | + && (e.target.value <= InputHandler.getAllowedMaxInputSize()) |
| 38 | + ? this.props.InputHandler.setState({inputArrayLength: e.target.value.replace(/\D/g, '')}) |
| 39 | + : this.props.InputHandler.setState({inputArrayLength: InputHandler.getAllowedMaxInputSize()}) |
| 40 | + }} |
| 41 | + className=" bg-gray-200 rounded-md text-center p-[.5em] font-medium w-[57%] sm:w-100" |
| 42 | + maximum={InputHandler.getAllowedMaxInputSize()} |
| 43 | + /> |
| 44 | + <button onClick={() => { |
| 45 | + this.props.InputHandler.setState({generatedArray: InputHandler.handleInputRequest(inputArrayLength)}); |
| 46 | + }} className="bg-blue-300 p-2 ml-3 rounded-md text-white font-medium hover:scale-[1.02] hover:bg-blue-400 transition duration-200 ease-in-out">Generate Array</button> |
| 47 | + </div> |
| 48 | + {/* <p className={`text-gray-700 mt-1 ${this.name[1] + ' '}`}>Array size should not exceed {InputHandler.getAllowedMaxInputSize()}.</p> */} |
| 49 | + </div> |
| 50 | + |
| 51 | + <div className="w-[41em] overflow-auto flex gap-3 p-1"> |
| 52 | + <div onClick={() => { |
| 53 | + this.Strategy.setStrategy = this.selectionSort; |
| 54 | + this.selectionSort.setOptions = options; |
| 55 | + this.setState({algorithm: 'selection'}); |
27 | 56 | }} |
28 | | - className=" bg-gray-200 rounded-md text-center p-[.5em] font-medium w-[57%] sm:w-100" |
29 | | - maximum={InputHandler.getAllowedMaxInputSize()} |
30 | | - /> |
31 | | - <button onClick={() => { |
| 57 | + className={`hover:scale-[1.02] transition-all duration-200 ease-in-out border-2 ${algorithm === 'selection' ? "bg-blue-100 border-blue-300" : "border-gray-300"} min-w-[150px] rounded-xl text-gray-600 flex flex-col p-3 items-center`}> |
| 58 | + |
| 59 | + <p className="font-semibold">Selection Sort</p> |
| 60 | + </div> |
| 61 | + |
| 62 | + <div onClick={() => { |
| 63 | + this.Strategy.setStrategy = this.selectionSort; |
| 64 | + this.selectionSort.setOptions = options; |
| 65 | + this.setState({algorithm: 'merge'}); |
| 66 | + }} |
| 67 | + className={`hover:scale-[1.02] transition-all duration-200 ease-in-out border-2 ${algorithm === 'merge' ? "bg-blue-100 border-blue-300" : "border-gray-300"} min-w-[150px] rounded-xl text-gray-600 flex flex-col p-3 items-center`}> |
| 68 | + |
| 69 | + <p className="font-semibold">Merge Sort</p> |
| 70 | + </div> |
| 71 | + |
| 72 | + <div onClick={() => { |
| 73 | + this.Strategy.setStrategy = this.selectionSort; |
| 74 | + this.selectionSort.setOptions = options; |
| 75 | + this.setState({algorithm: 'quick'}); |
| 76 | + }} |
| 77 | + className={`hover:scale-[1.02] transition-all duration-200 ease-in-out border-2 ${algorithm === 'quick' ? "bg-blue-100 border-blue-300" : "border-gray-300"} min-w-[150px] rounded-xl text-gray-600 flex flex-col p-3 items-center`}> |
| 78 | + |
| 79 | + <p className="font-semibold">Quick Sort</p> |
| 80 | + </div> |
| 81 | + |
| 82 | + <div onClick={() => { |
| 83 | + this.Strategy.setStrategy = this.selectionSort; |
| 84 | + this.selectionSort.setOptions = options; |
| 85 | + this.setState({algorithm: 'bubble'}); |
| 86 | + }} |
| 87 | + className={`hover:scale-[1.02] transition-all duration-200 ease-in-out border-2 ${algorithm === 'bubble' ? "bg-blue-100 border-blue-300" : "border-gray-300"} min-w-[150px] rounded-xl text-gray-600 flex flex-col p-3 items-center`}> |
| 88 | + <p className="font-semibold">Bubble Sort</p> |
| 89 | + </div> |
| 90 | + |
| 91 | + </div> |
| 92 | + |
| 93 | + <Options options={this.props.InputHandler}/> |
| 94 | + </div> |
| 95 | + |
| 96 | + <div className="flex self-center group gap-3"> |
| 97 | + <div onClick={() => this.Strategy.perform(options)} className={`p-3 rounded-lg flex ${this.state.algorithm ? "bg-green-600 cursor-pointer hover:bg-green-700" : "bg-gray-600"}`}> |
| 98 | + <p className="text-white font-semibold mr-2">Sort</p><Play className={`${this.state.algorithm && "group-hover:scale-[1.15] transition-all duration-200 ease-in-out"}`}/> |
| 99 | + </div> |
| 100 | + |
| 101 | + <div className="self-center group"> |
| 102 | + <div onClick={() => { //TODO need to make sure to kill setTimeout |
32 | 103 | this.props.InputHandler.setState({generatedArray: InputHandler.handleInputRequest(inputArrayLength)}); |
33 | | - }} className="bg-blue-300 p-2 ml-3 rounded-md text-white font-medium hover:scale-[1.02] hover:bg-blue-400 transition duration-200 ease-in-out">Generate Array</button> |
| 104 | + }} className={`p-3 rounded-lg flex ${this.state.algorithm ? "bg-green-600 cursor-pointer hover:bg-green-700" : "bg-gray-600"}`}><Replay/></div> |
34 | 105 | </div> |
35 | | - {/* <p className={`text-gray-700 mt-1 ${this.name[1] + ' '}`}>Array size should not exceed {InputHandler.getAllowedMaxInputSize()}.</p> */} |
36 | 106 | </div> |
37 | | - <Options options={this.props.InputHandler}/> |
38 | 107 | </div> |
39 | 108 | ) |
40 | 109 | } |
|
0 commit comments