Merge pull request #6053 from strapi/fix/ctb-default-date-input

Fix/ctb default date input
This commit is contained in:
Alexandre BODIN 2020-05-25 15:21:18 +02:00 committed by GitHub
commit d91c5f19c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 2 deletions

View File

@ -40,6 +40,16 @@ const reducer = (state, action) => {
value, value,
oneThatIsCreatingARelationWithAnother, oneThatIsCreatingARelationWithAnother,
} = action; } = action;
const hasDefaultValue = Boolean(obj.getIn(['default']));
// There is no need to remove the default key if the default value isn't defined
if (hasDefaultValue && keys.length === 1 && keys.includes('type')) {
const previousType = obj.getIn(['type']);
if (previousType && ['date', 'datetime', 'time'].includes(previousType)) {
return obj.updateIn(keys, () => value).remove('default');
}
}
if (keys.length === 1 && keys.includes('nature')) { if (keys.length === 1 && keys.includes('nature')) {
return obj return obj

View File

@ -217,6 +217,49 @@ describe('CTB | containers | FormModal | reducer | actions', () => {
expect(reducer(state, action)).toEqual(expected); expect(reducer(state, action)).toEqual(expected);
}); });
it('should remove the default value if the type of date input type has been changed', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'short_movie_time',
type: 'time',
default: '00:30:00',
})
);
const action = {
type: 'ON_CHANGE',
keys: ['type'],
value: 'datetime',
};
const expected = state
.setIn(['modifiedData', 'name'], 'short_movie_time')
.setIn(['modifiedData', 'type'], 'datetime')
.removeIn(['modifiedData', 'default']);
expect(reducer(state, action)).toEqual(expected);
});
it('should not remove the default value if the type of another input type has been changed', () => {
const state = initialState.setIn(
['modifiedData'],
fromJS({
name: 'number_of_movies',
type: 'integer',
default: '0',
})
);
const action = {
type: 'ON_CHANGE',
keys: ['type'],
value: 'biginteger',
};
const expected = state
.setIn(['modifiedData', 'name'], 'number_of_movies')
.setIn(['modifiedData', 'type'], 'biginteger');
expect(reducer(state, action)).toEqual(expected);
});
}); });
describe('ON_CHANGE_ALLOWED_TYPE', () => { describe('ON_CHANGE_ALLOWED_TYPE', () => {

View File

@ -488,10 +488,11 @@ const forms = {
items.splice(0, 1, [ items.splice(0, 1, [
{ {
...fields.default, ...fields.default,
type: 'date', type: data.type || 'date',
value: null, value: null,
withDefaultValue: false, withDefaultValue: false,
disabled: data.type !== 'date', disabled: !data.type,
autoFocus: false,
}, },
]); ]);
} else if (type === 'richtext') { } else if (type === 'richtext') {