Skip to content

Commit ed2aa84

Browse files
committed
use prop types
1 parent ad9331e commit ed2aa84

File tree

8 files changed

+122
-8
lines changed

8 files changed

+122
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"deploy": "gh-pages -d example/build"
3333
},
3434
"dependencies": {
35+
"prop-types": "^15.8.1",
3536
"xml2js": "^0.4.23"
3637
},
3738
"peerDependencies": {

src/components/Alignment/Alignment.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React from 'react';
2+
import { node, string } from 'prop-types';
3+
24
import { xmlToJson } from '../../utils/parsing';
35

46
import AlignmentContext from './alignment-context';
@@ -13,4 +15,13 @@ const Alignment = ({ alignment, children }) => {
1315
);
1416
};
1517

18+
Alignment.propTypes = {
19+
alignment: string.isRequired,
20+
children: node,
21+
};
22+
23+
Alignment.defaultProps = {
24+
children: null,
25+
};
26+
1627
export default Alignment;

src/components/Alignment/Segment.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React from 'react';
2+
import { string } from 'prop-types';
3+
24
import SentenceContext from './sentence-context';
35

46
import Segment from '../Segment';
@@ -16,4 +18,8 @@ const SegmentWithContext = ({ lnum }) => (
1618
</SentenceContext.Consumer>
1719
);
1820

21+
SegmentWithContext.propTypes = {
22+
lnum: string.isRequired,
23+
};
24+
1925
export default SegmentWithContext;

src/components/Alignment/Sentence/Sentence.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import React, { useState, useEffect } from 'react';
2+
import { node, string } from 'prop-types';
3+
4+
import { alignmentType } from '../../../types';
25

36
import AlignmentContext from '../alignment-context';
47
import SentenceContext from '../sentence-context';
@@ -63,7 +66,7 @@ const buildIdMap = (alignedText, sentence) => {
6366
idMap[lnum2][n2][lnum2] = idMap[lnum1][n1][lnum2];
6467
};
6568

66-
(sentence.wds || []).forEach((wd) => {
69+
sentence.wds.forEach((wd) => {
6770
const { lnum } = wd.$;
6871

6972
wd.w.forEach((word) => {
@@ -90,16 +93,16 @@ const buildIdMap = (alignedText, sentence) => {
9093
return idMap;
9194
};
9295

96+
// eslint-disable-next-line react/prop-types
9397
const WrappedSentence = ({ id, json, children }) => {
94-
const [active, setActive] = useState({});
98+
const [active, setActive] = useState(null);
9599
const [idMap, setIdMap] = useState({});
96100
const [sentence, setSentence] = useState({});
97101

98102
useEffect(() => {
99103
const alignedText = json['aligned-text'];
100-
const newSentence = alignedText
101-
? alignedText.sentence.find(({ $: { id: sentenceId } }) => sentenceId === id)
102-
: {};
104+
const newSentence = (alignedText.sentence || [])
105+
.find(({ $: { id: sentenceId } }) => sentenceId === id);
103106

104107
setSentence(newSentence);
105108
setIdMap(buildIdMap(alignedText, newSentence));
@@ -133,4 +136,23 @@ const Sentence = ({ id, children }) => (
133136
</AlignmentContext.Consumer>
134137
);
135138

139+
WrappedSentence.propTypes = {
140+
id: string.isRequired,
141+
json: alignmentType.isRequired,
142+
children: node,
143+
};
144+
145+
WrappedSentence.defaultProps = {
146+
children: null,
147+
};
148+
149+
Sentence.propTypes = {
150+
id: string.isRequired,
151+
children: node,
152+
};
153+
154+
Sentence.defaultProps = {
155+
children: null,
156+
};
157+
136158
export default Sentence;

src/components/Segment/Segment.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import React from 'react';
2+
import { func, string } from 'prop-types';
3+
4+
import { activeType, sentenceType } from '../../types';
5+
26
import styles from './Segment.module.scss';
37

48
const wordSpans = (words, lnum, active, setActive) => {
@@ -8,7 +12,7 @@ const wordSpans = (words, lnum, active, setActive) => {
812
const { text, $: { n } } = word;
913
const classes = [styles.word];
1014

11-
if (active[lnum] && active[lnum].has(n)) {
15+
if (active && active[lnum] && active[lnum].has(n)) {
1216
classes.push(styles.active);
1317
}
1418
const onClick = () => {
@@ -31,7 +35,7 @@ const wordSpans = (words, lnum, active, setActive) => {
3135
onClick={onClick}
3236
onKeyDown={onKeyDown}
3337
>
34-
{text[0]}
38+
{(text || []).join('')}
3539
</span>,
3640
);
3741
spans.push(' ');
@@ -58,4 +62,15 @@ const Segment = ({
5862
);
5963
};
6064

65+
Segment.propTypes = {
66+
lnum: string.isRequired,
67+
sentence: sentenceType.isRequired,
68+
active: activeType,
69+
setActive: func.isRequired,
70+
};
71+
72+
Segment.defaultProps = {
73+
active: null,
74+
};
75+
6176
export default Segment;

src/types/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {
2+
wordType,
3+
wordListType,
4+
sentenceType,
5+
alignmentType,
6+
activeType,
7+
} from './types';
8+
9+
export {
10+
wordType,
11+
wordListType,
12+
sentenceType,
13+
alignmentType,
14+
activeType,
15+
};

src/types/types.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
arrayOf,
3+
instanceOf,
4+
objectOf,
5+
shape,
6+
string,
7+
} from 'prop-types';
8+
9+
const wordType = shape({
10+
$: shape({
11+
n: string.isRequired,
12+
}),
13+
text: arrayOf(string),
14+
});
15+
16+
const wordListType = shape({
17+
$: shape({
18+
lnum: string.isRequired,
19+
}),
20+
w: arrayOf(wordType),
21+
});
22+
23+
const sentenceType = shape({
24+
$: shape({
25+
id: string.isRequired,
26+
}),
27+
wds: arrayOf(wordListType),
28+
});
29+
30+
const alignmentType = shape({
31+
'aligned-text': shape({
32+
sentence: arrayOf(sentenceType),
33+
}),
34+
});
35+
36+
const activeType = objectOf(instanceOf(Set));
37+
38+
export {
39+
wordType,
40+
wordListType,
41+
sentenceType,
42+
alignmentType,
43+
activeType,
44+
};

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9484,7 +9484,7 @@ prompts@^2.0.1:
94849484
kleur "^3.0.3"
94859485
sisteransi "^1.0.5"
94869486

9487-
prop-types@^15.6.2, prop-types@^15.7.2:
9487+
prop-types@^15.6.2, prop-types@^15.7.2, prop-types@^15.8.1:
94889488
version "15.8.1"
94899489
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
94909490
integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==

0 commit comments

Comments
 (0)