Skip to content
Merged
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
10 changes: 8 additions & 2 deletions components/mdc/Select/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ const selectedTextID = generateRandomID('select-selected-text-')

let element = {}
let mdcSelect = {}
let previousOptionsIDsCSV = ''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be passed in, or how are you setting this to previous option IDs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happens on line 55 inside afterUpdate. The first time options is populated it will be empty, meaning optionsHaveChanged will return true. Is that correct @forevermatt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hobbitronics Yes, that's exactly it.


$: selectedIndex = options.findIndex((option) => option.id === selectedID)
$: dispatch('change', options[selectedIndex] || {})
$: mdcSelect.disabled = disabled
$: if (options && mdcSelect.layoutOptions) mdcSelect.layoutOptions()

const getIDsCSV = (options) => options.map(option => option.id).join(',')

const optionsHaveChanged = (options) => previousOptionsIDsCSV !== getIDsCSV(options)

const recordSelectedID = (event) => (selectedID = event.detail.value)

const isOptionSelected = (option) => option.id === selectedID
Expand All @@ -43,10 +48,11 @@ afterUpdate(() => {
// This makes sure the index is updated AFTER the select list contains the full list of options.
mdcSelect.selectedIndex = selectedIndex

// If options have been provided, give the current processes time to finish
// If options have been provided or changed, give the current processes time to finish
// what they're doing, then indicate that this Select is now populated with
// options. At this point, it's safe for the selectedID to be initialized.
if (options.length > 0) {
if (options.length > 0 && optionsHaveChanged(options)) {
previousOptionsIDsCSV = getIDsCSV(options)
setTimeout(() => {
dispatch('populated')
})
Expand Down