Skip to content

Commit 5936c27

Browse files
authored
fix: report loading, handle empty parent edge cases (#2)
1 parent 56fb45a commit 5936c27

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

src/ParentWidget.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import Select from 'react-select';
2+
import AsyncSelect from 'react-select/async';
33
import { reactSelectStyles } from 'netlify-cms-ui-default/dist/esm/styles';
44
import { NestedCollection } from './NestedCollection';
55

@@ -45,7 +45,7 @@ const Option = (props) => {
4545
export class ParentControl extends React.Component {
4646
constructor(props) {
4747
super(props);
48-
this.state = { entries: [], title: '' };
48+
this.state = { title: '', optionsLoaded: false, options: [] };
4949
this.selectRef = React.createRef();
5050
}
5151

@@ -93,56 +93,63 @@ export class ParentControl extends React.Component {
9393
}
9494

9595
async componentDidMount() {
96-
const { forID, query, collection } = this.props;
97-
98-
const collectionName = collection.get('name');
99-
100-
const {
101-
payload: {
102-
response: { hits = [] },
103-
},
104-
} = await query(forID, collectionName, ['path'], '');
105-
106-
this.setState({
107-
entries: hits,
108-
});
109-
11096
if (this.isNewRecord()) {
11197
this.props.onChange(this.getValue() + '/');
11298
// track title field so we can use it for the folder name
11399
const titleInput = document.querySelector('[id*=title-field]');
114100
titleInput.addEventListener('input', (e) => {
115101
const title = e.target.value;
116102
this.setState({ title });
117-
const parent = this.selectRef.current.props.options[0].value;
118-
this.handleChange(parent);
103+
const selectProps = this.selectRef.current.props;
104+
const currentParent = selectProps.value?.value || '/';
105+
this.handleChange(currentParent);
119106
});
120107
}
121108
}
122109

123-
render() {
124-
const { forID, classNameWrapper, setActiveStyle, setInactiveStyle, collection } = this.props;
110+
async loadOptions() {
111+
if (this.state.optionsLoaded) {
112+
return this.state.options;
113+
}
114+
const { forID, query, collection } = this.props;
115+
const collectionName = collection.get('name');
116+
const {
117+
payload: {
118+
response: { hits = [] },
119+
},
120+
} = await query(forID, collectionName, ['path'], '');
125121

126122
const fullPath = this.getFullPath();
127123
const parentPath = this.getParent(fullPath) || '';
128-
const parent = this.state.entries.find((e) => this.getPath(e.path) === parentPath);
124+
const parent = hits.find((e) => this.getPath(e.path) === parentPath);
129125
const label = (parent && parent.data.title) || collection.get('label');
130-
131126
const options = [
132127
{
133128
value: parentPath,
134129
label,
135130
collection: this.props.collection,
136-
entries: this.state.entries,
131+
entries: hits,
137132
onSelectNode: (value) => this.handleChange(value),
138133
},
139134
];
140135

136+
this.setState({
137+
optionsLoaded: true,
138+
options,
139+
});
140+
141+
return options;
142+
}
143+
144+
render() {
145+
const { forID, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;
146+
141147
return (
142-
<Select
143-
value={options[0]}
148+
<AsyncSelect
149+
value={this.state.options[0]}
150+
loadOptions={() => this.loadOptions()}
151+
defaultOptions
144152
inputId={forID}
145-
options={options}
146153
className={classNameWrapper}
147154
onFocus={setActiveStyle}
148155
onBlur={setInactiveStyle}

0 commit comments

Comments
 (0)