feat: add add_to_top option to list widget (#4465)
This commit is contained in:
parent
fbc8963726
commit
4c962f9149
@ -180,16 +180,12 @@ export default class ListControl extends React.Component {
|
|||||||
|
|
||||||
handleAdd = e => {
|
handleAdd = e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const { value, onChange, field } = this.props;
|
const { field } = this.props;
|
||||||
const parsedValue =
|
const parsedValue =
|
||||||
this.getValueType() === valueTypes.SINGLE
|
this.getValueType() === valueTypes.SINGLE
|
||||||
? this.singleDefault()
|
? this.singleDefault()
|
||||||
: fromJS(this.multipleDefault(field.get('fields')));
|
: fromJS(this.multipleDefault(field.get('fields')));
|
||||||
this.setState({
|
this.addItem(parsedValue);
|
||||||
itemsCollapsed: [...this.state.itemsCollapsed, false],
|
|
||||||
keys: [...this.state.keys, uuid()],
|
|
||||||
});
|
|
||||||
onChange((value || List()).push(parsedValue));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
singleDefault = () => {
|
singleDefault = () => {
|
||||||
@ -201,13 +197,8 @@ export default class ListControl extends React.Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
handleAddType = (type, typeKey) => {
|
handleAddType = (type, typeKey) => {
|
||||||
const { value, onChange } = this.props;
|
|
||||||
const parsedValue = fromJS(this.mixedDefault(typeKey, type));
|
const parsedValue = fromJS(this.mixedDefault(typeKey, type));
|
||||||
this.setState({
|
this.addItem(parsedValue);
|
||||||
itemsCollapsed: [...this.state.itemsCollapsed, false],
|
|
||||||
keys: [...this.state.keys, uuid()],
|
|
||||||
});
|
|
||||||
onChange((value || List()).push(parsedValue));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mixedDefault = (typeKey, type) => {
|
mixedDefault = (typeKey, type) => {
|
||||||
@ -244,6 +235,26 @@ export default class ListControl extends React.Component {
|
|||||||
}, initialValue);
|
}, 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 => {
|
processControlRef = ref => {
|
||||||
if (!ref) return;
|
if (!ref) return;
|
||||||
const {
|
const {
|
||||||
|
@ -19,6 +19,7 @@ The list widget allows you to create a repeatable item in the UI which saves as
|
|||||||
* `fields`: a nested list of multiple widget fields to be included in each repeatable iteration
|
* `fields`: a nested list of multiple widget fields to be included in each repeatable iteration
|
||||||
* `max`: maximum number of items in the list
|
* `max`: maximum number of items in the list
|
||||||
* `min`: minimum number of items in the list
|
* `min`: minimum number of items in the list
|
||||||
|
* `add_to_top`: when `true`, new entries will be added to the top of the list
|
||||||
* **Example** (`field`/`fields` not specified):
|
* **Example** (`field`/`fields` not specified):
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@ -93,3 +94,10 @@ The list widget allows you to create a repeatable item in the UI which saves as
|
|||||||
min: 1
|
min: 1
|
||||||
default: ["news"]
|
default: ["news"]
|
||||||
```
|
```
|
||||||
|
* **Example** (`add_to_top` marked `true`):
|
||||||
|
```yaml
|
||||||
|
- label: "Tags"
|
||||||
|
name: "tags"
|
||||||
|
widget: "list"
|
||||||
|
add_to_top: true
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user