Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onLoadInitial callback function #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ The following options are supported:
- `minLength`: Number of characters which must be entered before any suggestions is displayed. Defaults to `2`.
- `multiline`: Display address suggestions on multiple lines. Default `false`.
- `id`: Initialize the input field with the address specified by the given UUID. If the address does not exist, the input field is left empty.
- `onLoadInitial`: This function is called whenever an initial value is loaded, either with the `id` option, or the `component.id(guid)` function

### API
The component has the following api:
Expand Down
19 changes: 17 additions & 2 deletions html/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ <h3>stormodtagerpostnumre=true</h3>
<input type="search" id="autocomplete-stormodtagerpostnumre-true">
</div>
<h3>addressId</h3>
<div class="dawa-autocomplete-container">
<div class="dawa-autocomplete-container address-id">
<input type="search" id="autocomplete-address-id">
<ul>
<li>Kommunekode: <span data-field="kommunekode"></span></li>
<li>x: <span data-field="x"></span></li>
<li>y: <span data-field="y"></span></li>
</ul>
</div>
<h3>vejnavn</h3>
<div class="dawa-autocomplete-container">
Expand Down Expand Up @@ -124,12 +129,22 @@ <h3>vejnavn</h3>
},
stormodtagerpostnumre: true
});
dawaAutocomplete.dawaAutocomplete(document.getElementById("autocomplete-address-id"), {
const autocompleteAddressId = dawaAutocomplete.dawaAutocomplete(document.getElementById("autocomplete-address-id"), {
baseUrl:BASE_URL,
select: function(selected) {
console.log('Selected address:');
console.dir(selected);
},
onLoadInitial: function(value) {
const field = document.getElementById("autocomplete-address-id")
const fieldWrapper = field.closest('.dawa-autocomplete-container')
fieldWrapper.querySelector('[data-field="kommunekode"]').innerText = value ? value.data.kommunekode : ''
fieldWrapper.querySelector('[data-field="y"]').innerText = value ? value.data.y : ''
fieldWrapper.querySelector('[data-field="x"]').innerText = value ? value.data.x : ''
if (!value) {
field.value = ''
}
},
id: 'f5a68d45-935b-48d6-8d11-6363327ca1ae'
});
dawaAutocomplete.dawaAutocomplete(document.getElementById("autocomplete-vejnavn-id"), {
Expand Down
8 changes: 8 additions & 0 deletions src/autocomplete-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const defaultOptions = {
/*eslint no-console: 0*/
console.error('No initialRenderCallback supplied');
},
initalLoadCallback: () => null,
type: 'adresse',
baseUrl: 'https://dawa.aws.dk',
adgangsadresserOnly: false,
Expand Down Expand Up @@ -154,6 +155,9 @@ export class AutocompleteController {
if(result.length === 1) {
this.selected = result[0];
this.options.initialRenderCallback(result[0].tekst);
this.options.initalLoadCallback(result[0])
} else {
this.options.initalLoadCallback(null)
}
}
else {
Expand Down Expand Up @@ -185,6 +189,10 @@ export class AutocompleteController {
this.options.selectCallback = selectCallback;
}

setInitialLoadCallback(loadCallback) {
this.options.initalLoadCallback = loadCallback;
}

update(text, caretpos) {
const request = {
text,
Expand Down
5 changes: 5 additions & 0 deletions src/dawa-autocomplete2.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export function dawaAutocomplete(inputElm, options) {
if(options.id) {
controller.selectInitial(options.id);
}

if (options.onLoadInitial) {
controller.setInitialLoadCallback(options.onLoadInitial)
}

return {
id: id => controller.selectInitial(id),
destroy: () => ui.destroy(),
Expand Down
11 changes: 11 additions & 0 deletions test-e2e/autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,16 @@ describe('Autocomplete', () => {
await wd.waitForElementByRegex('li.dawa-selected', /Margretheparken/);
});

it('Kan give adgang til data bag den standard valgte adresse', async () => {
const wd = wdFn();
const inputElm = await wd.findElementByCss('#autocomplete-address-id');
const wrapper = await wd.findElementByCss('.dawa-autocomplete-container.address-id');

const contents = await wd.getElementText(wrapper)

assert.match(contents, /Kommunekode: 0101/)
assert.match(contents, /x: 12\.53/)
assert.match(contents, /y: 55\.70/)
})
});
});
10 changes: 9 additions & 1 deletion test-e2e/setup/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ const withWebdriverClient = (testDefFn) => {
setupBrowserstackLocal();
for(let capabilities of capabilitiesList) {
let wd = null;
const browserFilter = process.env.E2E_BROWSER_FILTER
if (browserFilter && !browserFilter.includes(capabilities.browserName)) {
// skip this browser name
describe.skip(capabilities.browserName, () => null)
continue
}

describe(capabilities.browserName, () => {
before(async () => {
const client = await WebDriver.newSession({
Expand All @@ -180,7 +187,8 @@ const withWebdriverClient = (testDefFn) => {
port: 80,
capabilities: capabilities,
user: process.env.BROWSERSTACK_USERNAME,
key: process.env.BROWSERSTACK_ACCESS_KEY
key: process.env.BROWSERSTACK_ACCESS_KEY,
logLevel: process.env.E2E_LOG_LEVEL,
});
wd = new WebdriverWrapper(client);
});
Expand Down