Skip to content

Commit 3f4958a

Browse files
oklasjukben
authored andcommitted
support of unique key generator by trigger output option (#68)
* support of unique key generator by trigger `output` option * update snapshot * docs(readme): fixed readme, key property * docs(readme): added oklas to contributors;
1 parent dbd75f4 commit 3f4958a

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@
9595
"contributions": [
9696
"code"
9797
]
98+
},
99+
{
100+
"login": "oklas",
101+
"name": "Serguei Okladnikov",
102+
"avatar_url": "https://avatars3.githubusercontent.com/u/8123356?v=4",
103+
"profile": "https://github.com/oklas",
104+
"contributions": [
105+
"bug",
106+
"code"
107+
]
98108
}
99109
],
100110
"repoType": "github"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Enhanced textarea to achieve autocomplete functionality.
55

66
[![MIT License][license-badge]][License]
77
[![PRs Welcome][prs-badge]][prs]
8-
[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors)
8+
[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors)
99
<hr>
1010

1111
</div>
@@ -148,7 +148,7 @@ export default App;
148148

149149
You can also specify the behavior of caret if you return object `{text: "item", caretPosition: "start"}` the caret will be before the word once the user confirms his selection. Other possible value are "next", "end" and number, which is absolute number in contex of textarea (0 is equal position before the first char). Defaults to "next" which is space after the injected word.
150150

151-
Default behavior for string based item is string: `<TRIGGER><ITEM><TRIGGER>`). This method should **always** return a unique string, otherwise you have to use object notation and specify your own `key`.
151+
The default behavior for string based item is a string: `<TRIGGER><ITEM><TRIGGER>`). This method should **always** return a unique string, otherwise, you have to use object notation and specify your own `key` or return object from `dataProvider` with `key` property.
152152

153153
## [Example of usage](http://react-textarea-autocomplete.surge.sh/)
154154
`create-react-app example && cd example && yarn add @jukben/emoji-search @webscopeio/react-textarea-autocomplete`
@@ -242,7 +242,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
242242
<!-- prettier-ignore -->
243243
| [<img src="https://avatars3.githubusercontent.com/u/8135252?v=4" width="100px;"/><br /><sub><b>Jakub Beneš</b></sub>](https://jukben.cz)<br />[💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=jukben "Code") [📖](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=jukben "Documentation") [🎨](#design-jukben "Design") [🤔](#ideas-jukben "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/3114719?v=4" width="100px;"/><br /><sub><b>Andrey Taktaev</b></sub>](https://github.com/JokerNN)<br />[💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=JokerNN "Code") | [<img src="https://avatars0.githubusercontent.com/u/10706203?v=4" width="100px;"/><br /><sub><b>Marcin Lichwała</b></sub>](https://github.com/marcinlichwala)<br />[💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=marcinlichwala "Code") [📖](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=marcinlichwala "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/9276511?v=4" width="100px;"/><br /><sub><b>Davidson Nascimento</b></sub>](https://github.com/davidsonsns)<br />[💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=davidsonsns "Code") | [<img src="https://avatars1.githubusercontent.com/u/7477359?v=4" width="100px;"/><br /><sub><b>KajMagnus</b></sub>](http://www.effectivediscussions.org/)<br />[🐛](https://github.com/webscopeio/react-textarea-autocomplete/issues?q=author%3Akajmagnus "Bug reports") [💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=kajmagnus "Code") | [<img src="https://avatars2.githubusercontent.com/u/1083817?v=4" width="100px;"/><br /><sub><b>Ján Vorčák</b></sub>](https://twitter.com/janvorcak)<br />[🐛](https://github.com/webscopeio/react-textarea-autocomplete/issues?q=author%3Ajvorcak "Bug reports") [💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=jvorcak "Code") | [<img src="https://avatars2.githubusercontent.com/u/9800850?v=4" width="100px;"/><br /><sub><b>Mateusz Burzyński</b></sub>](https://github.com/Andarist)<br />[💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=Andarist "Code") [📦](#platform-Andarist "Packaging/porting to new platform") |
244244
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
245-
| [<img src="https://avatars0.githubusercontent.com/u/35139777?v=4" width="100px;"/><br /><sub><b>Deepak Pai</b></sub>](https://github.com/debugpai2)<br />[🐛](https://github.com/webscopeio/react-textarea-autocomplete/issues?q=author%3Adebugpai2 "Bug reports") [💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=debugpai2 "Code") | [<img src="https://avatars0.githubusercontent.com/u/2336595?v=4" width="100px;"/><br /><sub><b>Aleck Landgraf</b></sub>](http://aleck.me)<br />[💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=alecklandgraf "Code") |
245+
| [<img src="https://avatars0.githubusercontent.com/u/35139777?v=4" width="100px;"/><br /><sub><b>Deepak Pai</b></sub>](https://github.com/debugpai2)<br />[🐛](https://github.com/webscopeio/react-textarea-autocomplete/issues?q=author%3Adebugpai2 "Bug reports") [💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=debugpai2 "Code") | [<img src="https://avatars0.githubusercontent.com/u/2336595?v=4" width="100px;"/><br /><sub><b>Aleck Landgraf</b></sub>](http://aleck.me)<br />[💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=alecklandgraf "Code") | [<img src="https://avatars3.githubusercontent.com/u/8123356?v=4" width="100px;"/><br /><sub><b>Serguei Okladnikov</b></sub>](https://github.com/oklas)<br />[🐛](https://github.com/webscopeio/react-textarea-autocomplete/issues?q=author%3Aoklas "Bug reports") [💻](https://github.com/webscopeio/react-textarea-autocomplete/commits?author=oklas "Code") |
246246
<!-- ALL-CONTRIBUTORS-LIST:END -->
247247
248248
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!

__tests__/__snapshots__/index.spec.js.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,22 @@ exports[`object-based items with keys match the snapshot of dropdown, list, and
358358
</li>
359359
`;
360360

361+
exports[`object-based items without keys and custom unique generator match the snapshot 1`] = `
362+
<div
363+
className="rta "
364+
>
365+
<textarea
366+
className="rta__textarea "
367+
onBlur={[Function]}
368+
onChange={[Function]}
369+
onClick={[Function]}
370+
onSelect={[Function]}
371+
placeholder="Write a message."
372+
value="Controlled text"
373+
/>
374+
</div>
375+
`;
376+
361377
exports[`string-based items w/o output fn match match the snapshot 1`] = `
362378
<div
363379
className="rta "

__tests__/index.spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,60 @@ describe('object-based items with keys', () => {
443443
expect(rta.find('textarea').node.value).toBe('some test ___happy_face___ ');
444444
});
445445
});
446+
447+
describe('object-based items without keys and custom unique generator', () => {
448+
const mockedChangeFn = jest.fn();
449+
const mockedSelectFn = jest.fn();
450+
const mockedCaretPositionChangeFn = jest.fn();
451+
452+
const rtaComponent = (
453+
<ReactTextareaAutocomplete
454+
placeholder={'Write a message.'}
455+
value={'Controlled text'}
456+
onChange={mockedChangeFn}
457+
onSelect={mockedSelectFn}
458+
onCaretPositionChange={mockedCaretPositionChangeFn}
459+
loadingComponent={Loading}
460+
trigger={{
461+
':': {
462+
output: item => ({
463+
key: 10 + item.value,
464+
text: `___${item.text}___`,
465+
caretPosition: 'next',
466+
}),
467+
dataProvider: () => [
468+
{ value: 1, label: ':)', text: 'happy_face' },
469+
{ value: 2, label: ':(', text: 'sad_face' },
470+
{ value: 3, label: ':|', text: 'sad_face' },
471+
],
472+
component: SmileItemComponent,
473+
},
474+
}}
475+
/>
476+
);
477+
const rta = mount(rtaComponent);
478+
479+
it('match the snapshot', () => {
480+
expect(shallow(rtaComponent)).toMatchSnapshot();
481+
});
482+
483+
it('After the trigger was typed, it should appear list of options', () => {
484+
rta
485+
.find('textarea')
486+
.simulate('change', { target: { value: 'some test :a' } });
487+
expect(rta.find('.rta__autocomplete')).toHaveLength(1);
488+
});
489+
490+
it('should display all items', () => {
491+
expect(rta.find('.rta__autocomplete .rta__list .rta__item')).toHaveLength(
492+
3
493+
);
494+
});
495+
496+
it('should generate unique value by the output generator', () => {
497+
const items = rta.find(Item);
498+
expect(items.at(0).key()).toEqual("11");
499+
expect(items.at(1).key()).toEqual("12");
500+
expect(items.at(2).key()).toEqual("13");
501+
});
502+
});

src/List.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ export default class List extends React.Component<ListProps, ListState> {
5151
};
5252

5353
getId = (item: textToReplaceType | string): string => {
54+
const textToReplace = this.props.getTextToReplace(item);
55+
if (textToReplace.key) {
56+
return textToReplace.key;
57+
}
58+
5459
if (typeof item === 'string' || !item.key) {
55-
return this.props.getTextToReplace(item).text;
60+
return textToReplace.text;
5661
}
5762

5863
return item.key;

0 commit comments

Comments
 (0)