-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExam.js
131 lines (114 loc) · 3.28 KB
/
Exam.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React, { Component } from 'react'
import ExamList from './ExamList'
class Exam extends Component {
constructor (props, context) {
super(props, context)
this.clickChange = this.clickChange.bind(this)
this.inputChange = this.inputChange.bind(this)
this.state = {
keyWord: '',
examOption: '1'
}
}
inputChange (keyWord) {
this.setState({ keyWord: keyWord })
}
clickChange (examOption) {
this.setState({ examOption: examOption })
}
render () {
return (
<div>
<ExamSelector onChange={this.clickChange} />
<SearchBox testlist={ExamList[this.state.examOption]} defaultKeyword={this.state.keyWord} keywordChange={this.inputChange} />
<QList testlist={ExamList[this.state.examOption]} defaultKeyword={this.state.keyWord} keywordChange={this.inputChange} />
</div>
)
}
}
class ExamSelector extends Component {
constructor (props, context) {
super(props, context)
this.examChange = this.examChange.bind(this)
}
examChange (e) {
this.props.onChange(e.target.value)
}
render () {
return (
<div class='py-5' >
<div class='container'>
<div class='row'>
<div class='col-md-12' >
<p class='lead'>Choosing examination paper </p>
<select onChange={this.examChange}>
<option value='1'>Exam 1</option>
<option value='2'>Exam 2</option>
<option value='3'>Exam 3</option>
</select>
</div>
</div>
</div>
</div>
)
}
}
class SearchBox extends Component {
constructor (props) {
super(props)
this.inputChange = this.inputChange.bind(this)
}
inputChange (e) {
this.props.keywordChange(e.target.value)
}
render () {
const { defaultKeyword } = this.props
return (
<div class='py-5' >
<div class='container'>
<div class='row'>
<div class='col-md-12' >
<p class='lead'>Keyword search</p>
<input type='text' placeholder='Input keyword here!' value={defaultKeyword} onChange={this.inputChange} />
</div>
</div>
</div>
</div>
)
}
}
function getHighlightedText (text, higlight) {
// Split on higlight term and include term into parts, ignore case
let parts = text.split(new RegExp(`(${higlight})`, 'gi'))
return <span> { parts.map((part, i) =>
<span key={i} style={part.toLowerCase() === higlight.toLowerCase() ? { fontWeight: 'bold', backgroundColor: 'orange' } : {}}>
{ part }
</span>)
} </span>
}
class QList extends Component {
render () {
const { testlist } = this.props
const { defaultKeyword } = this.props
return (
<div class='py-5' >
<div class='container'>
<div class='row'>
<div class='col-md-12' >
<p class='lead'>Question List</p>
<ul>
{testlist.map(exam => (
<li>
<p class='lead'>{getHighlightedText(exam.name, defaultKeyword)}</p>
<p class='lead'>{getHighlightedText(exam.q, defaultKeyword)}</p>
</li>
))}
</ul>
</div>
</div>
</div>
</div>
)
}
}
export default Exam