Skip to content

Commit 134d6f0

Browse files
committed
Be sure to use a valid id for multiple component, remove dead code optionRenderer
1 parent 4b87949 commit 134d6f0

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

src/components/components/AddComponent.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,36 @@ export default class AddComponent extends React.Component {
2222

2323
let componentName = value.value;
2424

25-
var entity = this.props.entity;
25+
const entity = this.props.entity;
2626

2727
if (AFRAME.components[componentName].multiple) {
28-
const id = prompt(
28+
let id = prompt(
2929
`Provide an ID for this component (e.g., 'foo' for ${componentName}__foo).`
3030
);
31-
componentName = id ? `${componentName}__${id}` : componentName;
31+
if (id) {
32+
id = id
33+
.trim()
34+
.toLowerCase()
35+
.replace(/[^a-z0-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+
}
3255
}
3356

3457
entity.setAttribute(componentName, '');
@@ -40,33 +63,19 @@ export default class AddComponent extends React.Component {
4063
*/
4164
getComponentsOptions() {
4265
const usedComponents = Object.keys(this.props.entity.components);
43-
var commonOptions = Object.keys(AFRAME.components)
66+
return Object.keys(AFRAME.components)
4467
.filter(function (componentName) {
4568
return (
4669
AFRAME.components[componentName].multiple ||
4770
usedComponents.indexOf(componentName) === -1
4871
);
4972
})
50-
.sort()
5173
.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;
5378
});
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">&#9679;</span>
64-
);
65-
return (
66-
<strong className="option">
67-
{option.label} {option.origin === 'loaded' ? bullet : ''}
68-
</strong>
69-
);
7079
}
7180

7281
render() {
@@ -75,7 +84,7 @@ export default class AddComponent extends React.Component {
7584
return <div />;
7685
}
7786

78-
this.getComponentsOptions();
87+
const options = this.getComponentsOptions();
7988

8089
return (
8190
<div id="addComponentContainer">
@@ -84,13 +93,12 @@ export default class AddComponent extends React.Component {
8493
id="addComponent"
8594
className="addComponent"
8695
classNamePrefix="select"
87-
options={this.options}
96+
options={options}
8897
isClearable={false}
8998
isSearchable
9099
placeholder="Add component..."
91100
noOptionsMessage={() => 'No components found'}
92101
onChange={this.addComponent}
93-
optionRenderer={this.renderOption}
94102
value={this.state.value}
95103
/>
96104
</div>

src/style/components.styl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ span.subcomponent
183183
background #161616
184184
height 35px
185185
color $primary
186-
.option
187-
display flex
188-
justify-content space-between
189-
span
190-
color $primary
191186

192187
#addComponentHeader
193188
font-size 15px

0 commit comments

Comments
 (0)