Savvy Docs
  • Welcome
  • Guides
    • Intro to the builder
    • Designer Card
    • Options Card
    • Working with Logic
  • How-To's
    • General How-To's
      • ✔️How to select a container when I can’t click over it on the screen
      • 👁️How to hide a global component on a specific page
      • 📊How to calculate statistical significance from split test
      • 🅰️How to install a Google font.
      • 🗒️How to use "Logs" for debugging
      • ↩️How to change pages order
      • 🌒How to select components that are behind the Embeddables card
      • 📄How to clone a page
      • ➕How to add new components to a page
      • 📄How to select the whole page
      • 💻How to view your flow on different screen sizes
      • ⛵How to move between pages while editing your flow
    • Design How-To's
      • 🖋️How to style a specific component element (E.g. buttons image)
      • ⭕How to style individual buttons (by button key) in Options Selectors
      • 🧿How to style a rounded bullet-like checkbox for options buttons
      • 🔲How to get rid of the default input outline
      • 🖼️How to style a background image
      • 🦚How to style components (basics)
      • ↪️How to change a component's location
      • ⬆️How to upload and apply images to your design
      • 🔘How to style hover, selected, disabled button states
      • 🪶How to style CSS properties that are not there in the designer card (custom properties)
      • 📱How to make my flow responsive
    • Logic How-To's
      • 👁️How to set show/hide conditions
      • 📺How to display a value previously entered by the user
      • ✖️How to add a “Deselect All” button in a Multi-Select
      • 🔨How to use a computed field in a condition
      • 👁️How to hide a global component on a specific page
  • Other useful docs
    • Useful Docs
      • 🚦Conditions
      • 👩‍💻Computed Fields
      • 🔖Tags
    • WORK IN PROGRESS
      • ◻️Pending
        • Work in Progress: How the New Rendering Engine Works
        • Work in Progress: How to create a new Split test
        • Work in Progress: Preloading images
        • Work in Progress: How to show “loading…” while fetching or processing data
        • Work in Progress: How to send data to Tiktok
        • Work in Progress: How to send data to Postcript
        • Work in Progress: How to send data to Klaviyo
        • Work in Progress: How to send data to Google Tag
        • Work in Progress: How to send data to Google Analytics
        • Work in Progress: How to Fetch User Location Data to use in your Flow
        • Work in Progress: How to fetch job offers from Lever’s API
        • Work in Progress: How to fetch data and display the results as options in a dropdown
        • Work in Progress: How to create a new split test with 3 variants
        • Work in Progress: How to Duplicate a Container with All its Content
        • Work in Progress: How to add Chart.js to your flow
        • Work in Progress: How to Create Repeatable Button Components, Fetching from a Database
        • Work in Progress: How to Create a Global Column
        • Work in Progress: How to Clone a Flow
        • Work in Progress: How to Create and Style a Carousel
        • Work in Progress: How to Add Default User Data
        • Work in Progress: How to Show Validation Messages on Required Fields
        • Work in Progress: How to create an Option Selector Results Page
        • Work in Progress: How to create a combined Option Selector + Input component
        • Work in Progress: How to Add an Info Box Popup
        • Work in Progress: How to set up a Micro-Product as a Popup on your Site
        • Work in Progress: How to make a Full-Page Flow
        • Work in Progress: How to Use Custom Selectors
        • Work in Progress: How to Add a Video/GIF Placeholder to a Video Component
        • Work in Progress: How to Style Buttons
        • Work in Progress: How to Center Components
        • Work in Progress: How to Bring Data from Airtable
        • Work in Progress: How to add a custom font from a website:
        • Work in Progress: How to Create Computed Fields
        • Work in Progress: How to Ping an API
        • Work in Progress: How to Store a Password
        • Work in Progress: How to Bring Data from Google Sheet
        • Work in Progress: How to Fetch Safely from Savvy
        • Work in Progress: How To Fetch Data from a specific row in Airtable in a Computed Field
Powered by GitBook
On this page
  1. Other useful docs
  2. WORK IN PROGRESS
  3. Pending

Work in Progress: How to fetch data and display the results as options in a dropdown

<aside> 💡 The following example shows how to fetch data to display a dropdown of places from Google Places API

</aside>

  • Add an Input component on the page

  • Add an Option Buttons component below it

  • Create a new computed field and add to USER DATA INPUT FIELDS the input field

  • Edit the computed field, following the structure below and replacing YOUR_KEY:

function result( {
  input_field,
}) {
  if (!input_field) return []
  const input = document.querySelector("input.ComponentKey-input_field");
    return fetch(`https://savvy-api-proxy.heysavvy.workers.dev/?url=${encodeURIComponent(`https://maps.googleapis.com/maps/api/place/autocomplete/json?input=${input_field}&types=address&components=country:us&key=YOUR_KEY`)}`)
    .then(response => response.json())
    .then(data => {
      console.log(data)
      let results = data.predictions.map(result => {
        return {
          key:result.place_id,
          text:result.description
        }
      })
      return results
    })
}
  • Select the Option Buttons and go to More Options

  • Select the Advanced dropdown and then in the OPTIONAL - BUTTON REPEATER KEY select the Computed Field that fetches the options

  • Create a new computed field for the Placeholder text in the input field. Add to USER DATA INPUT FIELDS the option buttons and the current page key. It should be similar to:

function result( {
  option_buttons,
  current_page_key,
},
{
  setUserData
} ) {
  if(current_page_key === 'input_field' && option_buttons) {
    const input = document.querySelector("input.ComponentKey-input_field");
    const dropdownButton = document.querySelector(`.ElementKey-${option_buttons}`);
    
    if (option_buttons && dropdownButton) {
      setTimeout(() => {
        input.value = dropdownButton.innerText
        setUserData('input_field', dropdownButton.innerText)
      }, 250)
      return input.value
    }
    return 'Search for address'
  }
  return 'Search for address'
}
  • Select the input field and add the computed field for the placeholder text into PLACEHOLDER and into DEFAULT VALUE in More Options

  • Next, create a new computed field to open the dropdown when the input field is filled. Add to USER DATA INPUT FIELDS the input field and the option buttons:

function result({ input_field, option_buttons }) {
  const dropdownButton = document.querySelector(`.ElementKey-${option_buttons}`);
  
  if (!input_field) {
    // No search query, so don't allow selection -> close dropdown
    return false
  }

  if (!option_buttons) {
    // No address selected, so allow selection -> open dropdown
    return true
  }
  
  if (dropdownButton && input_field !== dropdownButton.innerText) {
    // Text changed since address selected, so allow selection -> open dropdown    
    return true
  }

  // Otherwise, don't allow selection -> close dropdown
  return false
}
  • And create a fourth computed field that contains the following code:

function result({}, { setUserData }) {
  setUserData('option_buttons', null)
}
  • Select the Option Buttons and add a condition to display it only if open_address_dropdown Is True

  • Create a new Data Output, select Manual trigger Only and trigger it on page load of the page that contains the input and the option buttons. It should look like:

function output(userData, context) {
  document
    .querySelector("input.ComponentKey-input_field")
    .addEventListener("keyup", function () {
      context.setUserData('open_address_dropdown', true);
    });
}
  • Finally, style the option buttons component to look like a dropdown.

PreviousWork in Progress: How to fetch job offers from Lever’s APINextWork in Progress: How to create a new split test with 3 variants

Last updated 1 year ago

◻️