react-mention-input 1.0.0 → 1.0.1

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.
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ interface User {
3
+ id: number;
4
+ name: string;
5
+ }
6
+ interface MentionInputProps {
7
+ users: User[];
8
+ placeholder?: string;
9
+ }
10
+ declare const MentionInput: React.FC<MentionInputProps>;
11
+ export default MentionInput;
@@ -1 +1,50 @@
1
- "use strict";
1
+ import React, { useState } from 'react';
2
+ var MentionInput = function (_a) {
3
+ var users = _a.users, placeholder = _a.placeholder;
4
+ var _b = useState(''), inputValue = _b[0], setInputValue = _b[1];
5
+ var _c = useState([]), suggestions = _c[0], setSuggestions = _c[1];
6
+ var _d = useState(false), showSuggestions = _d[0], setShowSuggestions = _d[1];
7
+ var handleInputChange = function (e) {
8
+ var value = e.target.value;
9
+ setInputValue(value);
10
+ var mentionMatch = value.match(/@(\w*)$/);
11
+ if (mentionMatch) {
12
+ var query_1 = mentionMatch[1].toLowerCase();
13
+ var filteredUsers = users.filter(function (user) {
14
+ return user.name.toLowerCase().startsWith(query_1);
15
+ });
16
+ setSuggestions(filteredUsers);
17
+ setShowSuggestions(true);
18
+ }
19
+ else {
20
+ setShowSuggestions(false);
21
+ }
22
+ };
23
+ var handleSuggestionClick = function (user) {
24
+ var mentionIndex = inputValue.lastIndexOf('@');
25
+ var newValue = "".concat(inputValue.substring(0, mentionIndex + 1)).concat(user.name, " ");
26
+ setInputValue(newValue);
27
+ setShowSuggestions(false);
28
+ };
29
+ return (React.createElement("div", { style: { position: 'relative' } },
30
+ React.createElement("input", { type: "text", value: inputValue, onChange: handleInputChange, placeholder: placeholder || 'Type a message...', style: { width: '100%', padding: '10px' } }),
31
+ showSuggestions && (React.createElement("ul", { style: {
32
+ position: 'absolute',
33
+ top: '100%',
34
+ left: 0,
35
+ right: 0,
36
+ backgroundColor: '#fff',
37
+ border: '1px solid #ddd',
38
+ listStyle: 'none',
39
+ margin: 0,
40
+ padding: 0,
41
+ maxHeight: '150px',
42
+ overflowY: 'auto',
43
+ zIndex: 1,
44
+ } }, suggestions.map(function (user) { return (React.createElement("li", { key: user.id, onClick: function () { return handleSuggestionClick(user); }, style: {
45
+ padding: '8px',
46
+ cursor: 'pointer',
47
+ borderBottom: '1px solid #ddd',
48
+ } }, user.name)); })))));
49
+ };
50
+ export default MentionInput;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "react-mention-input",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
7
- "build": "tsc"
7
+ "build": "tsc"
8
8
  },
9
9
  "keywords": [],
10
10
  "author": "",
@@ -15,8 +15,10 @@
15
15
  "react-dom": "^18.3.1"
16
16
  },
17
17
  "devDependencies": {
18
+ "@types/node": "^22.9.0",
18
19
  "@types/react": "^18.3.12",
19
20
  "@types/react-dom": "^18.3.1",
21
+ "csstype": "^3.1.3",
20
22
  "typescript": "^5.6.3"
21
23
  }
22
24
  }
@@ -0,0 +1,87 @@
1
+ import React, { useState, ChangeEvent, KeyboardEvent } from 'react';
2
+
3
+ interface User {
4
+ id: number;
5
+ name: string;
6
+ }
7
+
8
+ interface MentionInputProps {
9
+ users: User[];
10
+ placeholder?: string;
11
+ }
12
+
13
+ const MentionInput: React.FC<MentionInputProps> = ({ users, placeholder }) => {
14
+ const [inputValue, setInputValue] = useState<string>('');
15
+ const [suggestions, setSuggestions] = useState<User[]>([]);
16
+ const [showSuggestions, setShowSuggestions] = useState<boolean>(false);
17
+
18
+ const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
19
+ const value = e.target.value;
20
+ setInputValue(value);
21
+
22
+ const mentionMatch = value.match(/@(\w*)$/);
23
+ if (mentionMatch) {
24
+ const query = mentionMatch[1].toLowerCase();
25
+ const filteredUsers = users.filter((user) =>
26
+ user.name.toLowerCase().startsWith(query)
27
+ );
28
+ setSuggestions(filteredUsers);
29
+ setShowSuggestions(true);
30
+ } else {
31
+ setShowSuggestions(false);
32
+ }
33
+ };
34
+
35
+ const handleSuggestionClick = (user: User) => {
36
+ const mentionIndex = inputValue.lastIndexOf('@');
37
+ const newValue = `${inputValue.substring(0, mentionIndex + 1)}${user.name} `;
38
+ setInputValue(newValue);
39
+ setShowSuggestions(false);
40
+ };
41
+
42
+ return (
43
+ <div style={{ position: 'relative' }}>
44
+ <input
45
+ type="text"
46
+ value={inputValue}
47
+ onChange={handleInputChange}
48
+ placeholder={placeholder || 'Type a message...'}
49
+ style={{ width: '100%', padding: '10px' }}
50
+ />
51
+ {showSuggestions && (
52
+ <ul
53
+ style={{
54
+ position: 'absolute',
55
+ top: '100%',
56
+ left: 0,
57
+ right: 0,
58
+ backgroundColor: '#fff',
59
+ border: '1px solid #ddd',
60
+ listStyle: 'none',
61
+ margin: 0,
62
+ padding: 0,
63
+ maxHeight: '150px',
64
+ overflowY: 'auto',
65
+ zIndex: 1,
66
+ }}
67
+ >
68
+ {suggestions.map((user) => (
69
+ <li
70
+ key={user.id}
71
+ onClick={() => handleSuggestionClick(user)}
72
+ style={{
73
+ padding: '8px',
74
+ cursor: 'pointer',
75
+ borderBottom: '1px solid #ddd',
76
+ }}
77
+ >
78
+ {user.name}
79
+ </li>
80
+ ))}
81
+ </ul>
82
+ )}
83
+ </div>
84
+ );
85
+ };
86
+
87
+ export default MentionInput;
package/tsconfig.json CHANGED
@@ -2,11 +2,13 @@
2
2
  "compilerOptions": {
3
3
  "target": "ES5",
4
4
  "module": "ESNext",
5
+ "moduleResolution": "node",
5
6
  "jsx": "react",
7
+ "lib": ["dom", "es6"], // Ensure karein ki 'es6' included ho
6
8
  "declaration": true,
7
9
  "outDir": "./dist",
8
10
  "strict": true,
9
11
  "esModuleInterop": true
10
- },
11
- "include": ["src/**/*"]
12
+ }
13
+
12
14
  }