createMultiFieldArray

← All Butter documentation
Parameters
createMultiFieldArray(makeItem: (index: number) => { [key: string]: field }, options?: { min?: number, max?: number, default?: number }): MultiFieldArray
  • makeItem

    Called once for each item, with the item's index. Create the item's fields and return them in an object. The keys of the object name the fields.

  • options

    min and max limit how many items the user can have (there is no limit by default). default is how many items there are to start with: min, or 1 if min is 0.

Sometimes you want a variable number of some field, e.g. creating multiple color fields to define a palette. Other times you may want each item to have multiple properties, such as an image + a slider to control its size. createMultiFieldArray is designed for these cases: with it, you define what fields count as one item, and then let the user create multiple of those.

The .value() of a multi field array is an array of objects. Each object has the same keys, and the values are the fields for that item. Call .value() on one of those item fields to read its value.

let rings

function setup() {
  createCanvas(300, 300)
  rings = createMultiFieldArray((i) => ({
    size: createSlider(10, 140, 40 + i * 30),
    color: createColorPicker('#ff6ec4'),
  }), { min: 1, max: 6, default: 3 })
}

function draw() {
  clear()
  noFill()
  strokeWeight(6)
  for (const ring of rings.value()) {
    stroke(ring.color.value())
    circle(width / 2, height / 2, ring.size.value())
  }
}

Defining an item

The function you pass in is called once for each item, with the item's index. Create the fields for one item and return them in an object; the keys will be how you reference sub-fields later. The only part of the function that varies from item to item is the index. You can use it to choose each item's starting values, like the size of each ring above. Everything else you create should be the same for every item.

load* functions work in an item too. Pass an empty string as the URL, as you would anywhere else, and each item gets its own asset picker:

photos = createMultiFieldArray(async (i) => ({
  photo: await loadImage(''),
  caption: createInput('Caption'),
}), { min: 1, max: 4 })

While you are editing the component, in the sidebar, changing a sub-field's label will change it for every item.

Things to know

  • Every value in the returned object must come straight from a create* or load* call. Numbers and other plain values are not fields.
  • Keep the same keys on every item. Creating different fields for different items is not supported.
  • The function runs again whenever the sketch's setup runs again, so it should only create fields and not have other effects.
  • Don't use an array to get several lines of text. A single createInput is already a text field that starts as one line and grows to three as the user types, so one input is enough for a message or a paragraph. Use an array when the user should control how many of something there are.
  • An array can't contain another createMultiFieldArray.
  • To make the canvas follow the number of items or their values, chain .updatesSize() on the array, e.g. createMultiFieldArray(...).updatesSize().
  • To keep something else in step with the items, such as a time slider for each item, use .changed() on the array. If you want setup to run again when the items change instead, chain .resetOnUpdate(), which does the same as .updatesSize().

For a longer example, see the Text Conversation example.