@@ -4,11 +4,12 @@ This is much less complicated than the normal tags that map
4
4
to integers in the CRM. These are used at least for
5
5
accounts for users to indicate their interests.
6
6
*/
7
- import { Tag } from "antd" ;
8
-
7
+ import { Input , Tag } from "antd" ;
9
8
import { render } from "./register" ;
9
+ import { useEffect , useRef , useState } from "react" ;
10
+ import { useEditableContext } from "./context" ;
10
11
11
- render ( { type : "string-tags" } , ( { field, obj } ) => {
12
+ render ( { type : "string-tags" , editable : false } , ( { field, obj } ) => {
12
13
const tags = obj [ field ] ;
13
14
if ( tags == null ) return null ;
14
15
return (
@@ -19,3 +20,52 @@ render({ type: "string-tags" }, ({ field, obj }) => {
19
20
</ div >
20
21
) ;
21
22
} ) ;
23
+
24
+ render ( { type : "string-tags" , editable : true } , ( { field, obj, spec } ) => {
25
+ const ref = useRef < any > ( undefined ) ;
26
+ const { save, saving, counter, edit, error, ClickToEdit } =
27
+ useEditableContext < string > ( field ) ;
28
+ const [ value , setValue ] = useState < string > ( obj [ field ] ?. join ( "," ) ?? "" ) ;
29
+ useEffect ( ( ) => {
30
+ setValue ( obj [ field ] ) ;
31
+ } , [ counter , obj [ field ] ] ) ;
32
+
33
+ if ( spec . type != "string-tags" || ! spec . editable ) {
34
+ throw Error ( "bug" ) ;
35
+ }
36
+ if ( ! edit ) {
37
+ const tags = obj [ field ] ;
38
+ return (
39
+ < ClickToEdit empty = { ! tags || tags . length == 0 } >
40
+ < div style = { { lineHeight : "2em" , display : "inline-block" } } >
41
+ { tags ?. map ( ( value ) => (
42
+ < Tag key = { value } > { value } </ Tag >
43
+ ) ) }
44
+ </ div >
45
+ </ ClickToEdit >
46
+ ) ;
47
+ }
48
+ return (
49
+ < >
50
+ < Input
51
+ style = { { width : "100%" } }
52
+ disabled = { saving }
53
+ ref = { ref }
54
+ autoFocus
55
+ value = { value }
56
+ onChange = { ( e ) => {
57
+ setValue ( e . target . value ) ;
58
+ } }
59
+ onBlur = { ( ) => {
60
+ setValue ( ref . current . input . value ) ;
61
+ save ( obj , ref . current . input . value . split ( "," ) ) ;
62
+ } }
63
+ onPressEnter = { ( ) => {
64
+ setValue ( ref . current . input . value ) ;
65
+ save ( obj , ref . current . input . value . split ( "," ) ) ;
66
+ } }
67
+ />
68
+ { error }
69
+ </ >
70
+ ) ;
71
+ } ) ;
0 commit comments