This repository was archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathWordOccurrencesWrapper.js
276 lines (262 loc) · 7.11 KB
/
WordOccurrencesWrapper.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import React from "react";
import PropTypes from "prop-types";
import ExamplesContainer from "./ExamplesContainer";
import { WordOccurrences } from "@yoast/components";
import ProminentWord from "yoastseo/src/values/ProminentWord";
const initialWords = [
new ProminentWord( "davids", "david", 2 ),
new ProminentWord( "goliaths", "goliath", 6 ),
new ProminentWord( "word", "word", 3 ),
new ProminentWord( "yoast", "yoast", 8 ),
new ProminentWord( "test", "test", 10 ),
new ProminentWord( "apps", "app", 6 ),
new ProminentWord( "teletubbies", "teletubby", 11 ),
new ProminentWord( "strange", "stange", 4 ),
new ProminentWord( "improvisation", "improvisation", 4 ),
new ProminentWord( "ranking", "rank", 5 ),
new ProminentWord( "google", "google", 5 ),
new ProminentWord( "terms", "term", 8 ),
new ProminentWord( "wordpress", "wordpress", 9 ),
new ProminentWord( "inspiration", "inspiration", 2 ),
new ProminentWord( "internal", "internal", 2 ),
new ProminentWord( "linking", "link", 2 ),
new ProminentWord( "suggestions", "suggestion", 3 ),
new ProminentWord( "keyword", "keyword", 3 ),
new ProminentWord( "analysis", "analysis", 4 ),
new ProminentWord( "linguïns", "linguïn", 5 ),
];
/**
*
* @param {Object} props The component's properties.
*
* @param {ProminentWord[]} props.wordsForInsights The words to populate the list with.
* @param {function} props.onChange The handler handling change events.
* @param {function} props.onDelete The handler handling delete events.
*
* @returns {InputRow[]} A list of input rows.
*
* @constructor
*/
const InputList = ( { wordsForInsights, onChange, onDelete } ) => {
return wordsForInsights.map(
( wordForInsights, index ) => {
return <InputRow
key={ `RelevantWordInputRow-${ index }` }
index={ index }
wordForInsights={ wordForInsights }
onChange={ onChange }
onDelete={ onDelete }
/>;
}
);
};
/**
* A row to change or remove a word to the input list.
*
* @param {Object} props The properties.
*
* @param {ProminentWord} props.wordForInsights The word to show in the row.
* @param {number} props.index The row's index.
* @param {function} props.onChange The handler to call when one of the properties of the word (word, stem, occurrences) changes.
* @param {function} props.onDelete The handler to call when the row is removed.
*
* @returns {React.Component} The row.
*
* @constructor
*/
const InputRow = ( { wordForInsights, index, onChange, onDelete } ) => {
/**
* Called when the word changes.
*
* @param {Event} event The change event.
*
* @returns {void}
*/
function onWordChange( event ) {
onChange( event.target.value, "_word", index );
}
/**
* Called when the stem changes.
*
* @param {Event} event The change event.
*
* @returns {void}
*/
function onStemChange( event ) {
onChange( event.target.value, "_stem", index );
}
/**
* Called when the occurrences change.
*
* @param {Event} event The change event.
*
* @returns {void}
*/
function onOccurrencesChange( event ) {
onChange( event.target.value, "_occurrences", index );
}
/**
* Called when the row is deleted.
*
* @returns {void}
*/
function onDeleteRow() {
onDelete( index );
}
return (
<div key={ `InputDiv-${ index }` }>
<input
key={ `RelevantWordInput-${ index }` }
onChange={ onWordChange }
value={ wordForInsights._word }
/>
<input
key={ `RelevantStemInput-${ index }` }
onChange={ onStemChange }
value={ wordForInsights._stem }
/>
<input
key={ `RelevantOccurrencesInput-${ index }` }
onChange={ onOccurrencesChange }
value={ wordForInsights._occurrences }
type="number"
/>
<button onClick={ onDeleteRow }>REMOVE</button>
</div>
);
};
InputRow.propTypes = {
wordForInsights: PropTypes.instanceOf( ProminentWord ).isRequired,
index: PropTypes.number.isRequired,
onChange: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
};
/**
* Clones the array of words.
*
* @param {ProminentWord[]} words The list of prominent words to clone.
*
* @returns {ProminentWord[]} A clone of the given words.
*/
const cloneWords = function( words ) {
return words.map( word => {
return new ProminentWord( word.getWord(), word.getStem(), word.getOccurrences() );
} );
};
/**
* Wraps the word occurrences component.
* Adds a component to change, add and delete words manually.
*/
class WordOccurrencesWrapper extends React.Component {
/**
* Creates a new wrapper.
*
* @param {Object} props The properties
*/
constructor( props ) {
super( props );
this.state = {
words: cloneWords( initialWords ),
};
this.changeWord = this.changeWord.bind( this );
this.removeWord = this.removeWord.bind( this );
this.addWord = this.addWord.bind( this );
this.reset = this.reset.bind( this );
}
/**
* Changes a property of a word.
*
* @param {string} value The new value of the property.
* @param {"_word"|"_stem"|"_occurrences"} type The name of the property.
* @param {number} index The index of the word that needs to be changed.
*
* @returns {void}
*/
changeWord( value, type, index ) {
this.setState( ( prevState ) => {
const nextState = Object.assign( {}, prevState );
nextState.words = [ ...prevState.words ];
switch ( type ) {
case "_word":
nextState.words[ index ].setWord( value );
break;
case "_stem":
nextState.words[ index ]._stem = value;
break;
case "_occurrences":
nextState.words[ index ].setOccurrences( value );
break;
}
return nextState;
} );
}
/**
* Removes a word from the table.
*
* @param {number} index The index of the word that needs to be removed.
*
* @returns {void}
*/
removeWord( index ) {
this.setState( ( prevState ) => {
const nextState = Object.assign( {}, prevState );
nextState.words.splice( index, 1 );
return {
words: nextState.words,
};
} );
}
/**
* Adds a new word.
*
* @returns {void}
*/
addWord() {
this.setState( prevState => {
prevState.words.push(
new ProminentWord( "" ),
);
return (
{
words: prevState.words,
}
);
} );
}
/**
* Resets the words to their initial state.
*
* @returns {void}
*/
reset() {
this.setState( {
words: cloneWords( initialWords ),
} );
}
/**
* Renders the component.
*
* @returns {ExamplesContainer} The rendered component.
*/
render() {
return (
<ExamplesContainer>
<InputList
onChange={ this.changeWord }
wordsForInsights={ this.state.words }
onDelete={ this.removeWord }
/>
<button onClick={ this.addWord }>Add word</button>
<button onClick={ this.reset }>Reset</button>
<div style={ { marginTop: "150px", width: "100%", height: "600px" } }>
<WordOccurrences
words={ this.state.words }
header={ <p>This is an example text that will be displayed before the list is rendered.</p> }
footer={ <p>This is an example text that will be displayed after the list is rendered.</p> }
/>
</div>
</ExamplesContainer>
);
}
}
export default WordOccurrencesWrapper;