Choice - guiled/LRE GitHub Wiki

Choice

Introduction

The Choice component can seem to be easy but there are many use cases that can make them difficult to handle. LRE allow you to live with them with ease.

setChoices, getChoices

choice.setChoices() does exactly the same thing than the original method, but it can receive a dynamic value like a Table, a Group, or a function that returns the object for setChoices. If your function uses some components, the choices will be refresed if any of these components has changed.

LRE provides the method choice.getChoices() that works only if you use setChoices before.

Add data to each choice

This method also accepts an elaborate object. Let's Role choice.setChoices() accept an object that matches the following format :

{
    value1: "Label 1",
    value2: "Label 2",
    value3: "Label 3",
}

LRE's choice.setChoices() can also accept an object that contains more data that respect the following format :

{
    value1: { value: "Label 1", data: /* anything you want */{} },
    value2: { value: "Label 2", data: /* anything you want */{} },
    value3: { value: "Label 3", data: /* anything you want */{} },
}

The value key will be used as the choice label, and the data key will be used as the value returned by choice.getChoiceData()

Specific methods

optional

choice.optional(optional: Boolean, labelForDefault: string = "")

By calling it with true as first parameter, as soon as you use choice.setChoices() a first choice will be added with the given label as the "no choice" value.

label

choice.label(): string | undefined

This method returns the label of the current value, provided by Let's Role choice.text().

choice.label(value: ChoiceValue): string | undefined

If provided a value as parameter, this method will try to return the label for this choice value. This case works only if you previously used choice.setChoices() in your script.

getChoiceData

choice.getChoiceData()

This method returns the associated data to the choices. This can be usefull when you used choice.setChoices() with a Table.

choice.getChoiceData(value: ChoiceValue): any

Returns the associated data for the given value.

choiceData or row

choice.choiceData(value = choice.value())

This method is an alias of choice.getChoiceData(value). If no value is provided as parameter, it returns the associated data of the choice current value.

const classes = Tables.get("classes");
sheet.get("choiceClass").setChoices(classes.select("name"));
sheet.get("strengthBonusClass").value(function () {
    return sheet.get("choiceClass").choiceData().bonusStrength;
});

choice.row() choice.valueData()

These methods is an alias of choice.choiceData()

valueProvider

choice.valueProvider(): DataProvider

This method returns a DataProvider based on the chosen value of the choice.

populate

choice.populate(tableName: string, columnName: string = "id", optional: boolean = false) choice.populate(data: Array | Object, columnName: string = "id", optional: boolean = false)

This methods will call choice.setChoices() based on the given parameters. You can fill the choice directly with a table content with choice.populate("tableName", "columnName"), or directly with an array or an object. The parameters can be dynamic setter.

Specific events

select, unselect

choice.on("select", function (component, selectedValue) {}) choice.on("unselect", function (component, unselectedValue) {})

These events are triggered as soon as change the choice value and provides you the value that is selected or unselected.

valselect, valunselect

choice.on("selectval:theValue", function (component) {}) choice.on("unselectval:theValue", function (component) {})

These events are specifically triggered when you select or unselect a specific value in the choice.