-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaq-page.js
155 lines (143 loc) · 4.2 KB
/
faq-page.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { LitElement, html, css } from 'https://unpkg.com/lit-element?module';
class FaqPage extends LitElement {
static styles = css`
:host {
display: block;
color: black;
}
.faq-container {
padding: 20px;
}
.faq-item {
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
.faq-question {
font-weight: bold;
}
`;
static properties = {
searchTerm: { type: String },
searchResults: { type: Array },
originalData: { type: Array },
};
constructor() {
super();
this.searchTerm = '';
this.searchResults = [
{
id: 'faq1',
question: 'What is React?',
answer: 'React is a JavaScript library for building user interfaces.',
},
{
id: 'faq2',
question: 'How do I install React?',
answer: 'You can install React by using npm or yarn.',
},
{
id: 'faq3',
question: 'What is the virtual DOM?',
answer: 'The virtual DOM is a lightweight copy of the actual DOM used for efficient rendering in React.',
},
{
id: 'faq4',
question: 'What is JSX?',
answer: 'JSX is a syntax extension for JavaScript used in React to write HTML-like code in JavaScript files.',
},
{
id: 'faq5',
question: 'What are React components?',
answer: 'React components are reusable UI elements that encapsulate their own logic and rendering.',
},
{
id: 'faq6',
question: 'What is the purpose of state in React?',
answer: 'State is used to manage the data that can change in a React component and trigger re-rendering.',
},
{
id: 'faq7',
question: 'What is the role of props in React?',
answer: 'Props are used to pass data from a parent component to a child component in React.',
},
{
id: 'faq8',
question: 'How to handle events in React?',
answer: 'Event handling in React involves attaching event listeners to JSX elements using camel-cased names.',
},
{
id: 'faq9',
question: 'What is the React component lifecycle?',
answer: 'The component lifecycle represents the different phases a component goes through, such as initialization, rendering, and unmounting.',
},
{
id: 'faq10',
question: 'How to fetch data from an API in React?',
answer: 'Data fetching in React can be done using the built-in fetch API or using third-party libraries such as Axios.',
},
];
this.originalData = this.searchResults;
}
handleSearch() {
if (this.searchTerm === '') {
this.searchResults = this.originalData;
} else {
const filteredResults = this.originalData.filter(
(faq) =>
faq.question.toLowerCase().includes(this.searchTerm.toLowerCase())
);
this.searchResults = filteredResults;
}
}
handleKeyPress(event) {
if (event.key === 'Enter') {
this.handleSearch();
}
}
render() {
return html`
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
/>
<div class="faq-container">
<h2>FAQs</h2>
<div class="input-group mb-3">
<input
type="text"
class="form-control"
placeholder="Search FAQs"
.value=${this.searchTerm}
@input=${(e) => (this.searchTerm = e.target.value)}
@keypress=${this.handleKeyPress}
/>
<button
class="btn btn-primary"
type="button"
@click=${this.handleSearch}
>
Search
</button>
</div>
${this.searchTerm
? html`
<p>
Showing results for: <strong>${this.searchTerm}</strong>
</p>
`
: ''}
${this.searchResults.map(
(faq) => html`
<div class="faq-item">
<p class="faq-question">${faq.question}</p>
<p>${faq.answer}</p>
</div>
`
)}
</div>
`;
}
}
customElements.define('faq-page', FaqPage);