@pie-lib/text-select 1.12.7 → 1.12.8-next.1639

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/CHANGELOG.json +1 -632
  2. package/CHANGELOG.md +178 -28
  3. package/NEXT.CHANGELOG.json +1 -0
  4. package/lib/legend.js +63 -36
  5. package/lib/legend.js.map +1 -1
  6. package/lib/token-select/index.js +3 -2
  7. package/lib/token-select/index.js.map +1 -1
  8. package/lib/token-select/token.js +113 -74
  9. package/lib/token-select/token.js.map +1 -1
  10. package/lib/tokenizer/controls.js +14 -0
  11. package/lib/tokenizer/controls.js.map +1 -1
  12. package/lib/tokenizer/token-text.js +11 -2
  13. package/lib/tokenizer/token-text.js.map +1 -1
  14. package/package.json +8 -6
  15. package/src/__tests__/__snapshots__/text-select.test.jsx.snap +21 -0
  16. package/src/__tests__/text-select.test.jsx +34 -0
  17. package/src/__tests__/utils.test.jsx +27 -0
  18. package/src/legend.js +54 -36
  19. package/src/token-select/__tests__/__snapshots__/index.test.jsx.snap +49 -0
  20. package/src/token-select/__tests__/__snapshots__/token.test.jsx.snap +27 -0
  21. package/src/token-select/__tests__/index.test.jsx +257 -0
  22. package/src/token-select/__tests__/token.test.jsx +33 -0
  23. package/src/token-select/index.jsx +3 -1
  24. package/src/token-select/token.jsx +98 -72
  25. package/src/tokenizer/__tests__/__snapshots__/controls.test.jsx.snap +59 -0
  26. package/src/tokenizer/__tests__/__snapshots__/index.test.jsx.snap +31 -0
  27. package/src/tokenizer/__tests__/__snapshots__/token-text.test.jsx.snap +17 -0
  28. package/src/tokenizer/__tests__/builder.test.js +256 -0
  29. package/src/tokenizer/__tests__/controls.test.jsx +25 -0
  30. package/src/tokenizer/__tests__/index.test.jsx +140 -0
  31. package/src/tokenizer/__tests__/selection-utils.test.js +26 -0
  32. package/src/tokenizer/__tests__/token-text.test.jsx +136 -0
  33. package/src/tokenizer/controls.jsx +20 -1
  34. package/src/tokenizer/token-text.jsx +9 -0
  35. package/README.md +0 -3
package/src/legend.js CHANGED
@@ -2,9 +2,9 @@ import React from 'react';
2
2
  import { withStyles } from '@material-ui/core/styles';
3
3
  import Check from '@material-ui/icons/Check';
4
4
  import Close from '@material-ui/icons/Close';
5
- import Remove from '@material-ui/icons/Remove';
6
5
  import { color } from '@pie-lib/render-ui';
7
6
  import Translator from '@pie-lib/translator';
7
+ import classNames from 'classnames';
8
8
 
9
9
  const { translator } = Translator;
10
10
 
@@ -12,6 +12,7 @@ export const Legend = withStyles((theme) => ({
12
12
  flexContainer: {
13
13
  display: 'flex',
14
14
  flexDirection: 'row',
15
+ alignItems: 'center',
15
16
  gap: `${2 * theme.spacing.unit}px`,
16
17
  borderBottom: '1px solid lightgrey',
17
18
  borderTop: '1px solid lightgrey',
@@ -19,60 +20,77 @@ export const Legend = withStyles((theme) => ({
19
20
  paddingTop: theme.spacing.unit,
20
21
  marginBottom: theme.spacing.unit,
21
22
  },
23
+ key: {
24
+ fontSize: '14px',
25
+ fontWeight: 'bold',
26
+ color: color.black(),
27
+ marginLeft: theme.spacing.unit,
28
+ },
22
29
  container: {
23
- display: 'flex',
24
- flexDirection: 'row',
25
- gap: `${theme.spacing.unit / 2}px`,
26
- alignItems: 'center',
30
+ position: 'relative',
31
+ padding: '4px',
32
+ fontSize: '14px',
33
+ borderRadius: '4px',
27
34
  },
28
35
  correct: {
29
- fontSize: 'larger',
30
- color: color.correct(),
31
- backgroundColor: color.correctSecondary(),
32
- border: `2px solid ${color.correct()}`,
36
+ border: `${color.correctTertiary()} solid 2px`,
33
37
  },
34
38
  incorrect: {
35
- fontSize: 'larger',
36
- color: color.missing(),
37
- backgroundColor: color.incorrectSecondary(),
38
- border: `2px solid ${color.missing()}`,
39
+ border: `${color.incorrectWithIcon()} solid 2px`,
39
40
  },
40
41
  missing: {
41
- fontSize: 'larger',
42
- color: color.missing(),
43
- backgroundColor: color.incorrectSecondary(),
44
- border: `2px dashed ${color.missing()}`,
42
+ border: `${color.incorrectWithIcon()} dashed 2px`,
43
+ },
44
+ incorrectIcon: {
45
+ backgroundColor: color.incorrectWithIcon(),
46
+ },
47
+ correctIcon: {
48
+ backgroundColor: color.correctTertiary(),
45
49
  },
46
- }))(({ classes, language }) => {
47
- const icons = [
50
+ icon: {
51
+ color: color.white(),
52
+ position: 'absolute',
53
+ top: '-8px',
54
+ left: '-8px',
55
+ borderRadius: '50%',
56
+ fontSize: '12px',
57
+ padding: '2px',
58
+ },
59
+ }))(({ classes, language, showOnlyCorrect }) => {
60
+ const legendItems = [
48
61
  {
49
- iconName: Check,
62
+ Icon: Check,
50
63
  label: translator.t('selectText.correctAnswerSelected', { lng: language }),
51
- className: classes.correct,
64
+ containerClass: classNames(classes.correct, classes.container),
65
+ iconClass: classNames(classes.correctIcon, classes.icon),
52
66
  },
53
67
  {
54
- iconName: Remove,
55
- label: translator.t('selectText.correctAnswerNotSelected', { lng: language }),
56
- className: classes.missing,
68
+ Icon: Close,
69
+ label: translator.t('selectText.incorrectSelection', { lng: language }),
70
+ containerClass: classNames(classes.incorrect, classes.container),
71
+ iconClass: classNames(classes.incorrectIcon, classes.icon),
57
72
  },
58
73
  {
59
- iconName: Close,
60
- label: translator.t('selectText.incorrectSelection', { lng: language }),
61
- className: classes.incorrect,
74
+ Icon: Close,
75
+ label: translator.t('selectText.correctAnswerNotSelected', { lng: language }),
76
+ containerClass: classNames(classes.missing, classes.container),
77
+ iconClass: classNames(classes.incorrectIcon, classes.icon),
62
78
  },
63
79
  ];
64
80
 
81
+ if (showOnlyCorrect) {
82
+ legendItems.splice(1, 2);
83
+ }
84
+
65
85
  return (
66
86
  <div className={classes.flexContainer}>
67
- {icons.map((icon, index) => {
68
- const Icon = icon.iconName;
69
- return (
70
- <div key={index} className={classes.container}>
71
- <Icon className={icon.className} width={'19px'} height={'19px'}></Icon>
72
- <span>{icon.label}</span>
73
- </div>
74
- );
75
- })}
87
+ <span className={classes.key}>{translator.t('selectText.key', { lng: language })}</span>
88
+ {legendItems.map(({ Icon, label, containerClass, iconClass }, idx) => (
89
+ <div key={idx} className={containerClass}>
90
+ <Icon className={iconClass} />
91
+ <span>{label}</span>
92
+ </div>
93
+ ))}
76
94
  </div>
77
95
  );
78
96
  });
@@ -0,0 +1,49 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`token-select snapshot renders 1`] = `
4
+ <div
5
+ className=""
6
+ dangerouslySetInnerHTML={
7
+ Object {
8
+ "__html": "<p><span class=\\"tokenRootClass Token-token-1 Token-selectable-5\\" data-indexkey=\\"0\\" data-reactroot=\\"\\">foo bar</span></p>",
9
+ }
10
+ }
11
+ onClick={[Function]}
12
+ />
13
+ `;
14
+
15
+ exports[`token-select snapshot renders in view mode with selected tokens 1`] = `
16
+ <div
17
+ className=""
18
+ dangerouslySetInnerHTML={
19
+ Object {
20
+ "__html": "<p><span class=\\"Token-token-1 Token-selected-6 Token-disabledBlack-3\\" data-indexkey=\\"0\\" data-reactroot=\\"\\">foo,</span><br><span class=\\"Token-token-1 Token-selected-6 Token-disabledBlack-3\\" data-indexkey=\\"2\\" data-reactroot=\\"\\">bar</span></p>",
21
+ }
22
+ }
23
+ onClick={[Function]}
24
+ />
25
+ `;
26
+
27
+ exports[`token-select snapshot renders paragraphs properly 1`] = `
28
+ <div
29
+ className=""
30
+ dangerouslySetInnerHTML={
31
+ Object {
32
+ "__html": "<p><span class=\\"tokenRootClass Token-token-1 Token-selectable-5\\" data-indexkey=\\"0\\" data-reactroot=\\"\\">foo,</span></p><p><span class=\\"tokenRootClass Token-token-1 Token-selectable-5\\" data-indexkey=\\"2\\" data-reactroot=\\"\\">bar</span></p>",
33
+ }
34
+ }
35
+ onClick={[Function]}
36
+ />
37
+ `;
38
+
39
+ exports[`token-select snapshot renders sentences properly 1`] = `
40
+ <div
41
+ className=""
42
+ dangerouslySetInnerHTML={
43
+ Object {
44
+ "__html": "<p><span class=\\"tokenRootClass Token-token-1 Token-selectable-5\\" data-indexkey=\\"0\\" data-reactroot=\\"\\">foo,</span><br><span class=\\"tokenRootClass Token-token-1 Token-selectable-5\\" data-indexkey=\\"2\\" data-reactroot=\\"\\">bar</span></p>",
45
+ }
46
+ }
47
+ onClick={[Function]}
48
+ />
49
+ `;
@@ -0,0 +1,27 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`token snapshot renders 1`] = `
4
+ <Wrapper>
5
+ <span
6
+ className="tokenRootClass token"
7
+ dangerouslySetInnerHTML={
8
+ Object {
9
+ "__html": "foo bar",
10
+ }
11
+ }
12
+ />
13
+ </Wrapper>
14
+ `;
15
+
16
+ exports[`token snapshot renders with brs 1`] = `
17
+ <Wrapper>
18
+ <span
19
+ className="tokenRootClass token"
20
+ dangerouslySetInnerHTML={
21
+ Object {
22
+ "__html": "foo <br>bar",
23
+ }
24
+ }
25
+ />
26
+ </Wrapper>
27
+ `;
@@ -0,0 +1,257 @@
1
+ import { TokenSelect } from '../index';
2
+ import React from 'react';
3
+ import { shallow } from 'enzyme';
4
+
5
+ describe('token-select', () => {
6
+ describe('snapshot', () => {
7
+ it('renders', () => {
8
+ const w = shallow(
9
+ <TokenSelect
10
+ tokens={[
11
+ {
12
+ text: 'foo bar',
13
+ start: 0,
14
+ end: 7,
15
+ predefined: true,
16
+ selectable: true,
17
+ selected: false,
18
+ },
19
+ ]}
20
+ classes={{}}
21
+ onChange={jest.fn()}
22
+ />,
23
+ );
24
+ expect(w).toMatchSnapshot();
25
+ });
26
+
27
+ it('renders sentences properly', () => {
28
+ const w = shallow(
29
+ <TokenSelect
30
+ tokens={[
31
+ {
32
+ text: 'foo,',
33
+ start: 0,
34
+ end: 4,
35
+ predefined: true,
36
+ selectable: true,
37
+ selected: false,
38
+ },
39
+ {
40
+ text: '\n',
41
+ start: 4,
42
+ end: 5,
43
+ selected: false,
44
+ },
45
+ {
46
+ text: 'bar',
47
+ start: 5,
48
+ end: 8,
49
+ predefined: true,
50
+ selectable: true,
51
+ selected: false,
52
+ },
53
+ ]}
54
+ classes={{}}
55
+ onChange={jest.fn()}
56
+ />,
57
+ );
58
+ expect(w).toMatchSnapshot();
59
+ });
60
+
61
+ it('renders paragraphs properly', () => {
62
+ const w = shallow(
63
+ <TokenSelect
64
+ tokens={[
65
+ {
66
+ text: 'foo,',
67
+ start: 0,
68
+ end: 4,
69
+ predefined: true,
70
+ selectable: true,
71
+ selected: false,
72
+ },
73
+ {
74
+ text: '\n\n',
75
+ start: 4,
76
+ end: 5,
77
+ selected: false,
78
+ },
79
+ {
80
+ text: 'bar',
81
+ start: 5,
82
+ end: 8,
83
+ predefined: true,
84
+ selectable: true,
85
+ selected: false,
86
+ },
87
+ ]}
88
+ classes={{}}
89
+ onChange={jest.fn()}
90
+ />,
91
+ );
92
+ expect(w).toMatchSnapshot();
93
+ });
94
+
95
+ it('renders in view mode with selected tokens', () => {
96
+ const w = shallow(
97
+ <TokenSelect
98
+ disabled
99
+ tokens={[
100
+ {
101
+ text: 'foo,',
102
+ start: 0,
103
+ end: 4,
104
+ predefined: true,
105
+ selectable: true,
106
+ selected: true,
107
+ },
108
+ {
109
+ text: '\n',
110
+ start: 4,
111
+ end: 5,
112
+ selected: false,
113
+ },
114
+ {
115
+ text: 'bar',
116
+ start: 5,
117
+ end: 8,
118
+ predefined: true,
119
+ selectable: true,
120
+ selected: true,
121
+ },
122
+ ]}
123
+ classes={{}}
124
+ onChange={jest.fn()}
125
+ />,
126
+ );
127
+ expect(w).toMatchSnapshot();
128
+ });
129
+ });
130
+
131
+ describe('logic', () => {
132
+ let w, onChange;
133
+ beforeEach(() => {
134
+ onChange = jest.fn();
135
+ w = shallow(
136
+ <TokenSelect
137
+ tokens={[
138
+ {
139
+ text: 'foo',
140
+ start: 0,
141
+ end: 3,
142
+ },
143
+ {
144
+ text: 'bar',
145
+ start: 4,
146
+ end: 7,
147
+ },
148
+ ]}
149
+ classes={{}}
150
+ onChange={onChange}
151
+ />,
152
+ );
153
+ });
154
+ describe('selectedCount', () => {
155
+ it('returns the correct count', () => {
156
+ expect(w.instance().selectedCount()).toEqual(0);
157
+ });
158
+
159
+ it('returns the correct count for 1 selected', () => {
160
+ w.setProps({ tokens: [{ selected: true }] });
161
+ expect(w.instance().selectedCount()).toEqual(1);
162
+ });
163
+ });
164
+
165
+ describe('canSelectMore', () => {
166
+ it('returns true for undefined max selections', () => {
167
+ w.setProps({ maxNoOfSelections: undefined });
168
+ expect(w.instance().canSelectMore(10)).toEqual(true);
169
+ });
170
+ it('returns true for 0 max selections', () => {
171
+ w.setProps({ maxNoOfSelections: 0 });
172
+ expect(w.instance().canSelectMore(10)).toEqual(true);
173
+ });
174
+ it('returns true for -1 max selections', () => {
175
+ w.setProps({ maxNoOfSelections: -1 });
176
+ expect(w.instance().canSelectMore(10)).toEqual(true);
177
+ });
178
+ it('returns true for 5 max selections and count 4', () => {
179
+ w.setProps({ maxNoOfSelections: 5 });
180
+ expect(w.instance().canSelectMore(4)).toEqual(true);
181
+ });
182
+
183
+ it('returns true for 5 max selections and count 5', () => {
184
+ w.setProps({ maxNoOfSelections: 5 });
185
+ expect(w.instance().canSelectMore(5)).toEqual(false);
186
+ });
187
+
188
+ it('returns false for 5 max selections and count 6', () => {
189
+ w.setProps({ maxNoOfSelections: 5 });
190
+ expect(w.instance().canSelectMore(6)).toEqual(false);
191
+ });
192
+
193
+ it('returns true for 1 max selections and count 1', () => {
194
+ w.setProps({ maxNoOfSelections: 1 });
195
+ expect(w.instance().canSelectMore(1)).toEqual(true);
196
+ });
197
+ });
198
+
199
+ describe('toggleToken', () => {
200
+ it('return if clicked target is not selectable', () => {
201
+ w.setProps({ maxNoOfSelections: 1, tokens: [{ selected: true }] });
202
+
203
+ const closest = jest.fn().mockReturnValue(null);
204
+ const mockedEvent = { target: { closest } };
205
+
206
+ w.instance().toggleToken(mockedEvent);
207
+
208
+ expect(onChange).not.toBeCalled();
209
+ });
210
+
211
+ it('calls onChange', () => {
212
+ w.setProps({ maxNoOfSelections: 0, tokens: [{ selected: true }] });
213
+
214
+ const closest = jest.fn().mockReturnValue({
215
+ dataset: {
216
+ indexkey: '0',
217
+ },
218
+ });
219
+ const mockedEvent = { target: { closest } };
220
+
221
+ w.instance().toggleToken(mockedEvent);
222
+
223
+ expect(onChange).toBeCalled();
224
+ });
225
+
226
+ it('returns if max tokens have been selected', () => {
227
+ w.setProps({ maxNoOfSelections: 0, tokens: [{ selected: true }] });
228
+
229
+ const closest = jest.fn().mockReturnValue({
230
+ dataset: {
231
+ indexkey: '0',
232
+ },
233
+ });
234
+ const mockedEvent = { target: { closest } };
235
+
236
+ w.instance().toggleToken(mockedEvent);
237
+
238
+ expect(onChange).not.toBeCalledWith([{ selected: true }]);
239
+ });
240
+
241
+ it('calls onChange if maxNoOfSelections is 1 and selectedCount is 1', () => {
242
+ w.setProps({ maxNoOfSelections: 1, tokens: [{ selected: true }] });
243
+
244
+ const closest = jest.fn().mockReturnValue({
245
+ dataset: {
246
+ indexkey: '0',
247
+ },
248
+ });
249
+ const mockedEvent = { target: { closest } };
250
+
251
+ w.instance().toggleToken(mockedEvent);
252
+
253
+ expect(onChange).toBeCalled();
254
+ });
255
+ });
256
+ });
257
+ });
@@ -0,0 +1,33 @@
1
+ import { Token } from '../token';
2
+ import { shallow } from 'enzyme';
3
+ import React from 'react';
4
+
5
+ describe('token', () => {
6
+ describe('snapshot', () => {
7
+ it('renders', () => {
8
+ const w = shallow(
9
+ <Token
10
+ classes={{
11
+ token: 'token',
12
+ selectable: 'selectable',
13
+ }}
14
+ text={'foo bar'}
15
+ />,
16
+ );
17
+ expect(w).toMatchSnapshot();
18
+ });
19
+
20
+ it('renders with brs', () => {
21
+ const w = shallow(
22
+ <Token
23
+ classes={{
24
+ token: 'token',
25
+ selectable: 'selectable',
26
+ }}
27
+ text={'foo \nbar'}
28
+ />,
29
+ );
30
+ expect(w).toMatchSnapshot();
31
+ });
32
+ });
33
+ });
@@ -58,7 +58,9 @@ export class TokenSelect extends React.Component {
58
58
  const targetedTokenIndex = targetSpanWrapper && targetSpanWrapper.dataset && targetSpanWrapper.dataset.indexkey;
59
59
  const t = targetedTokenIndex && tokensCloned[targetedTokenIndex];
60
60
 
61
- if (t && t.correct === undefined && !animationsDisabled) {
61
+ // don't toggle if we are in print mode, token correctness is defined or if it's missing
62
+ // (missing means that it was evaluated as correct and not selected)
63
+ if (t && t.correct === undefined && !animationsDisabled && !t.isMissing) {
62
64
  const { onChange, maxNoOfSelections } = this.props;
63
65
  const selected = !t.selected;
64
66