Skip to content

Commit b182d89

Browse files
committed
Add model watch to City Autocomplete
We display multiple city fields on our donation form, one for each address type. These fields each have their own internal state for holding their value, and they emit events to update the model. When the model was changed by one city field it wasn't being picked up by the others meaning if a donor changed their address type after selecting a city the other city field would be blank. This adds a watcher for changes in the model value and updates the internal state when it changes. Ticket: https://phabricator.wikimedia.org/T397635
1 parent c098a53 commit b182d89

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/components/shared/form_fields/CityAutocompleteField.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ watch( city, ( newCity: string ) => {
181181
emit( 'update:modelValue', newCity );
182182
} );
183183
184+
watch( () => props.modelValue, ( newValue: string ) => {
185+
city.value = newValue;
186+
} );
187+
184188
</script>
185189

186190
<style lang="scss">

tests/unit/components/shared/form_fields/CityAutocompleteField.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,14 @@ describe( 'CityAutocompleteField.vue', () => {
261261

262262
expect( wrapper.emitted( 'field-changed' ).length ).toStrictEqual( 1 );
263263
} );
264+
265+
it( 'updates value on model change', async () => {
266+
const wrapper = getWrapper();
267+
268+
expect( wrapper.find<HTMLInputElement>( '#city' ).element.value ).toBe( '' );
269+
270+
await wrapper.setProps( { modelValue: 'Berlin' } );
271+
272+
expect( wrapper.find<HTMLInputElement>( '#city' ).element.value ).toBe( 'Berlin' );
273+
} );
264274
} );

0 commit comments

Comments
 (0)