Skip to content

Commit 464e82c

Browse files
Fix react-select usage for add mixins, add component, array property (#794)
* Fix react-select usage, custom no results message now properly working, click on Add mixins select working, and clear select value after adding a component * Add support for editing a property of type array (fix #793) * Remove unused propTypes componentname entity and mark value as required in SelectWidget
1 parent 026475a commit 464e82c

File tree

7 files changed

+64
-42
lines changed

7 files changed

+64
-42
lines changed

examples/index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
<!--<script>window.AFRAME_SAMPLE_ASSETS_ROOT = "./sample-assets/";</script>-->
77
<script src="https://cdn.jsdelivr.net/gh/aframevr/aframe@master/dist/aframe-master.min.js"></script>
88
<script src="https://unpkg.com/aframe-environment-component@1.4.0/dist/aframe-environment-component.min.js"></script>
9+
<script>
10+
AFRAME.registerComponent('models-array', {
11+
schema: {
12+
models: {type: 'array', oneOf: ['one', 'two', 'three', 'four']}
13+
}
14+
})
15+
</script>
916
</head>
1017
<body>
1118
<a-scene debug="true">
@@ -30,7 +37,7 @@
3037
<a-entity id="shortOrangeBox" mixin="short orange box" position="-5 2 0"></a-entity>
3138
<a-entity id="shortYellowBox" mixin="short yellow box" position="5 2 0"></a-entity>
3239
<a-entity id="redBox" geometry="primitive: box" material="color: #f00" position="-4 1 0" animation="property: object3D.rotation.y; to: 360; loop: true; easing: linear; dur: 9600"></a-entity>
33-
<a-entity id="yellowSphere" geometry="primitive: sphere" material="color: #ff0; roughness: 1" position="-2 2 -2"></a-entity>
40+
<a-entity id="yellowSphere" geometry="primitive: sphere" material="color: #ff0; roughness: 1" position="-2 2 -2" models-array="models:one,two"></a-entity>
3441
<a-box src="https://aframe.io/sample-assets/assets/images/bricks/brick_bump.jpg" position="-5 5 -2" width="1" color="#F16745"></a-box>
3542
<a-box id="aBox" position="0 2 0" height="2" color="#FFC65D"></a-box>
3643

src/components/components/AddComponent.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ export default class AddComponent extends React.Component {
88
entity: PropTypes.object
99
};
1010

11+
constructor(props) {
12+
super(props);
13+
this.state = { value: null };
14+
}
15+
1116
/**
1217
* Add blank component.
1318
* If component is instanced, generate an ID.
1419
*/
1520
addComponent = (value) => {
21+
this.setState({ value: null });
22+
1623
let componentName = value.value;
1724

1825
var entity = this.props.entity;
@@ -78,27 +85,15 @@ export default class AddComponent extends React.Component {
7885
className="addComponent"
7986
classNamePrefix="select"
8087
options={this.options}
81-
simpleValue
82-
clearable={true}
88+
isClearable={false}
89+
isSearchable
8390
placeholder="Add component..."
84-
noResultsText="No components found"
91+
noOptionsMessage={() => 'No components found'}
8592
onChange={this.addComponent}
8693
optionRenderer={this.renderOption}
87-
searchable={true}
94+
value={this.state.value}
8895
/>
8996
</div>
9097
);
9198
}
9299
}
93-
94-
/* eslint-disable no-unused-vars */
95-
/**
96-
* Check if component has multiplicity.
97-
*/
98-
function isComponentInstanced(entity, componentName) {
99-
for (var component in entity.components) {
100-
if (component.substring(0, component.indexOf('__')) === componentName) {
101-
return true;
102-
}
103-
}
104-
}

src/components/components/Mixins.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ export default class Mixin extends React.Component {
6868
id="mixinSelect"
6969
classNamePrefix="select"
7070
options={this.getMixinOptions()}
71-
isMulti={true}
71+
isMulti
72+
isClearable={false}
73+
isSearchable
7274
placeholder="Add mixin..."
73-
noResultsText="No mixins found"
75+
noOptionsMessage={() => 'No mixins found'}
7476
onChange={this.updateMixins.bind(this)}
75-
simpleValue
7677
value={this.state.mixins}
7778
/>
7879
</span>

src/components/components/PropertyRow.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ export default class PropertyRow extends React.Component {
7272
};
7373

7474
if (props.schema.oneOf && props.schema.oneOf.length > 0) {
75-
return <SelectWidget {...widgetProps} options={props.schema.oneOf} />;
75+
return (
76+
<SelectWidget
77+
{...widgetProps}
78+
options={props.schema.oneOf}
79+
isMulti={props.schema.type === 'array'}
80+
/>
81+
);
7682
}
7783
if (type === 'map' || isMap) {
7884
return <TextureWidget {...widgetProps} />;

src/components/viewport/CameraToolbar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ export default class CameraToolbar extends React.Component {
9090
id="cameraSelect"
9191
classNamePrefix="select"
9292
options={options}
93-
simpleValue
94-
value={getOption(this.state.selectedCamera)}
93+
isClearable={false}
9594
isSearchable={false}
95+
value={getOption(this.state.selectedCamera)}
9696
onChange={this.onChange.bind(this)}
9797
/>
9898
</div>

src/components/widgets/SelectWidget.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,53 @@ import Select from 'react-select';
44

55
export default class SelectWidget extends React.Component {
66
static propTypes = {
7-
componentname: PropTypes.string.isRequired,
8-
entity: PropTypes.object,
7+
isMulti: PropTypes.bool,
98
name: PropTypes.string.isRequired,
109
onChange: PropTypes.func,
1110
options: PropTypes.array.isRequired,
12-
value: PropTypes.string
11+
value: PropTypes.oneOfType([PropTypes.string, PropTypes.array]).isRequired
12+
};
13+
14+
static defaultProps = {
15+
isMulti: false
1316
};
1417

1518
constructor(props) {
1619
super(props);
17-
const value = this.props.value || '';
18-
this.state = { value: { value: value, label: value } };
20+
if (this.props.isMulti) {
21+
const value = this.props.value;
22+
this.state = {
23+
value: value.map((choice) => ({ value: choice, label: choice }))
24+
};
25+
} else {
26+
const value = this.props.value;
27+
this.state = { value: { value: value, label: value } };
28+
}
1929
}
2030

2131
onChange = (value) => {
2232
this.setState({ value: value }, () => {
2333
if (this.props.onChange) {
24-
this.props.onChange(this.props.name, value.value);
34+
this.props.onChange(
35+
this.props.name,
36+
this.props.isMulti ? value.map((option) => option.value) : value.value
37+
);
2538
}
2639
});
2740
};
2841

2942
componentDidUpdate(prevProps) {
3043
const props = this.props;
3144
if (props.value !== prevProps.value) {
32-
this.setState({
33-
value: { value: props.value, label: props.value }
34-
});
45+
if (this.props.isMulti) {
46+
this.setState({
47+
value: props.value.map((choice) => ({ value: choice, label: choice }))
48+
});
49+
} else {
50+
this.setState({
51+
value: { value: props.value, label: props.value }
52+
});
53+
}
3554
}
3655
}
3756

@@ -45,13 +64,13 @@ export default class SelectWidget extends React.Component {
4564
className="select-widget"
4665
classNamePrefix="select"
4766
options={options}
48-
simpleValue
49-
clearable={true}
67+
isMulti={this.props.isMulti}
68+
isClearable={false}
69+
isSearchable
5070
placeholder=""
5171
value={this.state.value}
52-
noResultsText="No value found"
72+
noOptionsMessage={() => 'No value found'}
5373
onChange={this.onChange}
54-
searchable={true}
5574
/>
5675
);
5776
}

src/style/select.styl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
.select__option
3838
cursor pointer
3939

40-
.select__clear-indicator
41-
display none
42-
4340
.select__label
4441
font-size 11px
4542

@@ -63,9 +60,6 @@
6360
.select__multi-value__label
6461
color $primary
6562

66-
.select__value-container--is-multi > :last-child
67-
display none
68-
6963
.select__multi-value__remove:hover
7064
color #fff
7165
background $bg

0 commit comments

Comments
 (0)