Dropdown Selected Label (Value Label)

For those looking to do the same thing, I added 3 custom props to the dropdown.

  1. selected_value - bound directly to the value prop of the dropdown
    a. Use a value change script to loop through the array of options and update the index and label.
  2. selected_index - updated from change script
  3. selected_label - updated from change script

change script:

def valueChanged(self, previousValue, currentValue, origin, missedEvents):

	val = currentValue.value
	if val is not None:
		count = len(self.props.options)
		for item in range(0, count):
			if self.props.options[item].value == val:
				self.custom.selected_index = item
				self.custom.selected_label = self.props.options[item].label
	else:
		self.custom.selected_index = None
		self.custom.selected_label = None

My reason for wanting to do this was to add buttons that would go to the next or previous item in the list. Fairly easy with scripts with the selected_index property.

next:

def runAction(self, event):
	
	current = self.getSibling('Dropdown').custom.selected_index
	maxIndex = len(self.getSibling('Dropdown').props.options)-1
	if current < maxIndex:
		next = current + 1
	else:
		next = 0
	
	self.getSibling('Dropdown').props.value = self.getSibling('Dropdown').props.options[next].value

previous:

def runAction(self, event):

	current = self.getSibling('Dropdown').custom.selected_index
	maxIndex = len(self.getSibling('Dropdown').props.options)-1
	if current > 0:
		next = current - 1
	else:
		next = maxIndex
	
	self.getSibling('Dropdown').props.value = self.getSibling('Dropdown').props.options[next].value

The enabled prop for each button is bound to the dropdown. If the value is null, the button is disabled.

1 Like