feat: add add_to_top option to list widget (#4465)

This commit is contained in:
Nick Holden
2020-10-25 12:32:00 -07:00
committed by GitHub
parent fbc8963726
commit 4c962f9149
2 changed files with 31 additions and 12 deletions

View File

@ -180,16 +180,12 @@ export default class ListControl extends React.Component {
handleAdd = e => {
e.preventDefault();
const { value, onChange, field } = this.props;
const { field } = this.props;
const parsedValue =
this.getValueType() === valueTypes.SINGLE
? this.singleDefault()
: fromJS(this.multipleDefault(field.get('fields')));
this.setState({
itemsCollapsed: [...this.state.itemsCollapsed, false],
keys: [...this.state.keys, uuid()],
});
onChange((value || List()).push(parsedValue));
this.addItem(parsedValue);
};
singleDefault = () => {
@ -201,13 +197,8 @@ export default class ListControl extends React.Component {
};
handleAddType = (type, typeKey) => {
const { value, onChange } = this.props;
const parsedValue = fromJS(this.mixedDefault(typeKey, type));
this.setState({
itemsCollapsed: [...this.state.itemsCollapsed, false],
keys: [...this.state.keys, uuid()],
});
onChange((value || List()).push(parsedValue));
this.addItem(parsedValue);
};
mixedDefault = (typeKey, type) => {
@ -244,6 +235,26 @@ export default class ListControl extends React.Component {
}, initialValue);
};
addItem = parsedValue => {
const { value, onChange, field } = this.props;
const addToTop = field.get('add_to_top', false);
const itemKey = uuid();
this.setState({
itemsCollapsed: addToTop
? [false, ...this.state.itemsCollapsed]
: [...this.state.itemsCollapsed, false],
keys: addToTop ? [itemKey, ...this.state.keys] : [...this.state.keys, itemKey],
});
const listValue = value || List();
if (addToTop) {
onChange(listValue.unshift(parsedValue));
} else {
onChange(listValue.push(parsedValue));
}
};
processControlRef = ref => {
if (!ref) return;
const {