I’ve created a customized Multiselect widget which includes a “Select All” option.
I’ve added a new property to the widget called select_all
which can be used to select all the options.
In order to implement this behavior I need to set the value
property to include all the possible options. The issue is that when I initialize the widget I don’t want to invoke the on_change
callback that’s attached to the value
property (in main.py
).
Is there a way to evaluate the value
property on initialization without triggering the on_change
callback?
This is the relevant TypeScript code (without the View class):
export namespace CustomMultiSelect {
export type Attrs = p.AttrsOf<Props>
export type Props = InputWidget.Props & {
value: p.Property<string[]>
options: p.Property<(string | [string, string])[]>
include_select_all: p.Property<boolean>
select_all: p.Property<boolean>
}
}
export interface CustomMultiSelect extends CustomMultiSelect.Attrs {}
export class CustomMultiSelect extends InputWidget {
properties: CustomMultiSelect.Props
__view_type__: CustomMultiSelectView
constructor(attrs?: Partial<CustomMultiSelect.Attrs>) {
super(attrs)
}
initialize(): void {
super.initialize()
if (this.select_all && this.options.length)
this.value = this.options.map(opt => isString(opt) ? opt : opt[0])
}
static init_CustomMultiSelect(): void {
this.prototype.default_view = CustomMultiSelectView
this.define<CustomMultiSelect.Props>(({String, Array, Tuple, Or, Boolean}) => ({
value: [ Array(String), [] ],
options: [ Array(Or(String, Tuple(String, String))), [] ],
include_select_all: [ Boolean, false ],
select_all: [ Boolean, false ],
}))
}
}