choiceComboboxHelpers.js 850 B

123456789101112131415161718192021222324252627282930313233
  1. export function findSelectedChoice (choices, modelValue) {
  2. if (!modelValue) {
  3. return null
  4. }
  5. return choices.find(choice => choice.value === modelValue) ?? null
  6. }
  7. export function choiceDisplayLabel (choice) {
  8. return choice.title || choice.value
  9. }
  10. export function displayLabelForModelValue (choices, modelValue) {
  11. const match = findSelectedChoice(choices, modelValue)
  12. return match ? choiceDisplayLabel(match) : ''
  13. }
  14. export function normalizedModelValue (choices, modelValue) {
  15. if (!modelValue) {
  16. return ''
  17. }
  18. return findSelectedChoice(choices, modelValue) ? modelValue : ''
  19. }
  20. export function syncStateFromModelValue (choices, modelValue) {
  21. const normalizedValue = normalizedModelValue(choices, modelValue)
  22. return {
  23. query: displayLabelForModelValue(choices, normalizedValue),
  24. modelValue: normalizedValue
  25. }
  26. }