-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure values not in options clear
Closes #817
- Loading branch information
Showing
9 changed files
with
358 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/form-js-viewer/src/render/hooks/useCleanupMultiSelectValues.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect } from 'preact/hooks'; | ||
import { LOAD_STATES } from './useOptionsAsync'; | ||
|
||
export default function(props) { | ||
|
||
const { | ||
field, | ||
options, | ||
loadState, | ||
onChange, | ||
values | ||
} = props; | ||
|
||
// Ensures that the values are always a subset of the possible options | ||
useEffect(() => { | ||
|
||
if (loadState !== LOAD_STATES.LOADED) { | ||
return; | ||
} | ||
|
||
const hasValuesNotInOptions = values.some(v => !options.map(o => o.value).includes(v)); | ||
|
||
if (hasValuesNotInOptions) { | ||
onChange({ | ||
field, | ||
value: values.filter(v => options.map(o => o.value).includes(v)) | ||
}); | ||
} | ||
|
||
}, [ field, options, onChange, JSON.stringify(values), loadState ]); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
packages/form-js-viewer/src/render/hooks/useCleanupSingleSelectValue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect } from 'preact/hooks'; | ||
import { LOAD_STATES } from './useOptionsAsync'; | ||
|
||
export default function(props) { | ||
|
||
const { | ||
field, | ||
options, | ||
loadState, | ||
onChange, | ||
value | ||
} = props; | ||
|
||
// Ensures that the value is always one of the possible options | ||
useEffect(() => { | ||
|
||
if (loadState !== LOAD_STATES.LOADED) { | ||
return; | ||
} | ||
|
||
const hasValueNotInOptions = value && !options.map(o => o.value).includes(value); | ||
|
||
if (hasValueNotInOptions) { | ||
onChange({ | ||
field, | ||
value: null | ||
}); | ||
} | ||
|
||
}, [ field, options, onChange, value, loadState ]); | ||
|
||
} |
123 changes: 123 additions & 0 deletions
123
packages/form-js-viewer/test/spec/render/hooks/useCleanupMultiSelectValues.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { | ||
render, | ||
} from '@testing-library/preact/pure'; | ||
|
||
import useCleanupMultiSelectValues from '../../../../src/render/hooks/useCleanupMultiSelectValues'; | ||
|
||
const spy = sinon.spy; | ||
let root; | ||
|
||
describe('useCleanupMultiSelectValue', function() { | ||
|
||
beforeEach(() => root = document.createElement('div')); | ||
|
||
afterEach(() => root.remove()); | ||
|
||
it('should fire onChange when any value is no longer in the options', function() { | ||
|
||
// given | ||
const onChangeSpy = spy(); | ||
const values = [ 'camunda-platform', 'camunda-cloud' ]; | ||
|
||
const { rerender } = render( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
}); | ||
|
||
// when | ||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ noPlatformOptions } values={ values } />, { | ||
container: root | ||
} | ||
); | ||
|
||
// then | ||
expect(onChangeSpy).to.have.been.calledOnce; | ||
expect(onChangeSpy).to.have.been.calledWith({ | ||
field: 'foo', | ||
value: [ 'camunda-cloud' ] | ||
}); | ||
|
||
}); | ||
|
||
|
||
it('should not fire onChange when all values are still in the options', function() { | ||
|
||
// given | ||
const onChangeSpy = spy(); | ||
const values = [ 'camunda-platform', 'camunda-cloud' ]; | ||
|
||
const { rerender } = render( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
}); | ||
|
||
// when | ||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
} | ||
); | ||
|
||
// then | ||
expect(onChangeSpy).to.not.have.been.called; | ||
|
||
}); | ||
|
||
|
||
it('should not reinstate the removed values when the options change back', function() { | ||
|
||
// given | ||
const onChangeSpy = spy(); | ||
const values = [ 'camunda-platform', 'camunda-cloud' ]; | ||
|
||
const { rerender } = render( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
}); | ||
|
||
// when | ||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ noPlatformOptions } values={ values } />, { | ||
container: root | ||
} | ||
); | ||
|
||
rerender( | ||
<TestComponent onChangeSpy={ onChangeSpy } options={ baseOptions } values={ values } />, { | ||
container: root | ||
} | ||
); | ||
|
||
// then | ||
expect(onChangeSpy).to.have.been.calledOnce; | ||
expect(onChangeSpy).to.have.been.calledWith({ | ||
field: 'foo', | ||
value: [ 'camunda-cloud' ] | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
const baseOptions = [ | ||
{ value: 'camunda-platform', label: 'Camunda Platform' }, | ||
{ value: 'camunda-cloud', label: 'Camunda Cloud' } | ||
]; | ||
|
||
const noPlatformOptions = [ | ||
{ value: 'camunda-cloud', label: 'Camunda Cloud' } | ||
]; | ||
|
||
function TestComponent({ onChangeSpy, options, values }) { | ||
|
||
useCleanupMultiSelectValues({ | ||
field: 'foo', | ||
loadState: 'loaded', | ||
onChange: onChangeSpy, | ||
options, | ||
values | ||
}); | ||
|
||
return <></>; | ||
} |
Oops, something went wrong.