@@ -22,13 +22,36 @@ export default class AddComponent extends React.Component {
22
22
23
23
let componentName = value . value ;
24
24
25
- var entity = this . props . entity ;
25
+ const entity = this . props . entity ;
26
26
27
27
if ( AFRAME . components [ componentName ] . multiple ) {
28
- const id = prompt (
28
+ let id = prompt (
29
29
`Provide an ID for this component (e.g., 'foo' for ${ componentName } __foo).`
30
30
) ;
31
- componentName = id ? `${ componentName } __${ id } ` : componentName ;
31
+ if ( id ) {
32
+ id = id
33
+ . trim ( )
34
+ . toLowerCase ( )
35
+ . replace ( / [ ^ a - z 0 - 9 ] / g, '' ) ;
36
+ // With the transform, id could be empty string, so we need to check again.
37
+ }
38
+ if ( id ) {
39
+ componentName = `${ componentName } __${ id } ` ;
40
+ } else {
41
+ // If components already exist, be sure to suffix with an id,
42
+ // if it's first one, use the component name without id.
43
+ const numberOfComponents = Object . keys (
44
+ this . props . entity . components
45
+ ) . filter ( function ( name ) {
46
+ return (
47
+ name === componentName || name . startsWith ( `${ componentName } __` )
48
+ ) ;
49
+ } ) . length ;
50
+ if ( numberOfComponents > 0 ) {
51
+ id = numberOfComponents + 1 ;
52
+ componentName = `${ componentName } __${ id } ` ;
53
+ }
54
+ }
32
55
}
33
56
34
57
entity . setAttribute ( componentName , '' ) ;
@@ -40,33 +63,19 @@ export default class AddComponent extends React.Component {
40
63
*/
41
64
getComponentsOptions ( ) {
42
65
const usedComponents = Object . keys ( this . props . entity . components ) ;
43
- var commonOptions = Object . keys ( AFRAME . components )
66
+ return Object . keys ( AFRAME . components )
44
67
. filter ( function ( componentName ) {
45
68
return (
46
69
AFRAME . components [ componentName ] . multiple ||
47
70
usedComponents . indexOf ( componentName ) === - 1
48
71
) ;
49
72
} )
50
- . sort ( )
51
73
. map ( function ( value ) {
52
- return { value : value , label : value , origin : 'loaded' } ;
74
+ return { value : value , label : value } ;
75
+ } )
76
+ . toSorted ( function ( a , b ) {
77
+ return a . label === b . label ? 0 : a . label < b . label ? - 1 : 1 ;
53
78
} ) ;
54
-
55
- this . options = commonOptions ;
56
- this . options = this . options . sort ( function ( a , b ) {
57
- return a . label === b . label ? 0 : a . label < b . label ? - 1 : 1 ;
58
- } ) ;
59
- }
60
-
61
- renderOption ( option ) {
62
- var bullet = (
63
- < span title = "Component already loaded in the scene" > ●</ span >
64
- ) ;
65
- return (
66
- < strong className = "option" >
67
- { option . label } { option . origin === 'loaded' ? bullet : '' }
68
- </ strong >
69
- ) ;
70
79
}
71
80
72
81
render ( ) {
@@ -75,7 +84,7 @@ export default class AddComponent extends React.Component {
75
84
return < div /> ;
76
85
}
77
86
78
- this . getComponentsOptions ( ) ;
87
+ const options = this . getComponentsOptions ( ) ;
79
88
80
89
return (
81
90
< div id = "addComponentContainer" >
@@ -84,13 +93,12 @@ export default class AddComponent extends React.Component {
84
93
id = "addComponent"
85
94
className = "addComponent"
86
95
classNamePrefix = "select"
87
- options = { this . options }
96
+ options = { options }
88
97
isClearable = { false }
89
98
isSearchable
90
99
placeholder = "Add component..."
91
100
noOptionsMessage = { ( ) => 'No components found' }
92
101
onChange = { this . addComponent }
93
- optionRenderer = { this . renderOption }
94
102
value = { this . state . value }
95
103
/>
96
104
</ div >
0 commit comments