Add notification for min when deleting a group field

This commit is contained in:
soupette 2019-07-16 16:06:46 +02:00
parent d57588f728
commit cf6e3e0536
5 changed files with 55 additions and 13 deletions

View File

@ -32,6 +32,6 @@ module.exports = {
// .default,
'strapi-plugin-email': require('../../../strapi-plugin-email/admin/src')
.default,
// 'strapi-plugin-upload': require('../../../strapi-plugin-upload/admin/src')
// .default,
'strapi-plugin-upload': require('../../../strapi-plugin-upload/admin/src')
.default,
};

View File

@ -36,7 +36,16 @@ function reducer(state, action) {
});
});
case 'REMOVE_COLLAPSE':
return state.removeIn(['collapses', action.index]);
return state
.removeIn(['collapses', action.index])
.update('collapses', list => list.map(obj => obj.set('isOpen', false)))
.update('collapses', list => {
if (action.shouldAddEmptyField) {
return list.push(fromJS({ isOpen: true }));
}
return list;
});
default:
return state;
}
@ -61,7 +70,7 @@ function Group({
isRepeatable,
label,
layout,
// min,
min,
max,
modifiedData,
moveGroupField,
@ -177,11 +186,20 @@ function Group({
removeField={e => {
e.stopPropagation();
if (groupValue.length - 1 < min) {
strapi.notification.info(
'A empty field has been added to match the requirements'
);
}
const shouldAddEmptyField = groupValue.length - 1 < min;
dispatch({
type: 'REMOVE_COLLAPSE',
index,
shouldAddEmptyField,
});
removeField(`${name}.${index}`);
removeField(`${name}.${index}`, shouldAddEmptyField);
}}
/>
</div>
@ -224,6 +242,7 @@ Group.defaultProps = {
label: '',
layout: {},
max: Infinity,
min: -Infinity,
modifiedData: {},
onChange: () => {},
};
@ -235,6 +254,7 @@ Group.propTypes = {
label: PropTypes.string,
layout: PropTypes.object,
max: PropTypes.number,
min: PropTypes.number,
modifiedData: PropTypes.object,
moveGroupField: PropTypes.func.isRequired,
name: PropTypes.string.isRequired,

View File

@ -352,10 +352,11 @@ function EditView({
});
}}
layout={get(groupLayoutsData, group.group, {})}
removeField={keys => {
removeField={(keys, shouldAddEmptyField) => {
dispatch({
type: 'ON_REMOVE_FIELD',
keys: keys.split('.'),
shouldAddEmptyField,
});
}}
/>

View File

@ -10,6 +10,13 @@ const initialState = fromJS({
defaultGroupValues: {},
});
const getMax = arr => {
if (arr.size === 0) {
return -1;
}
return Math.max.apply(Math, arr.toJS().map(o => o._temp__id));
};
function reducer(state, action) {
switch (action.type) {
case 'ADD_FIELD_TO_GROUP':
@ -21,12 +28,7 @@ function reducer(state, action) {
]);
if (list) {
const max = Math.max.apply(
Math,
list.toJS().map(function(o) {
return o._temp__id;
})
);
const max = getMax(list);
return list.push(
fromJS(
@ -81,7 +83,22 @@ function reducer(state, action) {
() => action.value
);
case 'ON_REMOVE_FIELD':
return state.removeIn(['modifiedData', ...action.keys]);
return state
.removeIn(['modifiedData', ...action.keys])
.updateIn(['modifiedData', action.keys[0]], list => {
if (action.shouldAddEmptyField) {
const defaultAttribute = state.getIn([
'defaultGroupValues',
action.keys[0],
'defaultRepeatable',
]);
const max = getMax(list);
return list.push(defaultAttribute.set('_temp__id', max + 1));
}
return list;
});
case 'RESET_FORM':
return state.update('modifiedData', () => state.get('initialData'));
case 'SET_COLLAPSES_COMPONENTS_STATE':

View File

@ -201,6 +201,7 @@ module.exports = {
articles: {
type: 'relation',
targetModel: 'article',
relationType: 'manyToMany',
},
},
@ -305,6 +306,7 @@ module.exports = {
},
tags: {
type: 'relation',
targetModel: 'tag',
relationType: 'manyToMany',
},
pics: {
@ -520,6 +522,8 @@ module.exports = {
},
role: {
type: 'relation',
targetModel: 'role',
plugin: 'users-permissions',
relationType: 'manyToOne',
},
},