Enabling interactivity

Event listeners

Event listener functions can be assigned to virtual nodes in the same way as specifying any other property when instantiating the node. When outputting VNodes, naming of event listeners in VNodeProperties mirrors the equivalent events on HTMLElement. Authors of custom widgets can name their events however they choose, but typically also follow a similar onEventName naming convention.

Function properties such as event handlers are automatically bound to the this context of the widget that instantiated the virtual node. However, if an already-bound function is given as a property value, this will not be bound again.

Handling focus

When outputting VNodes, widgets can use VNodeProperties‘s focus property to control whether the resulting DOM element should receive focus when rendering. This is a special property that accepts either a boolean or a function that returns a boolean.

When passing true directly, the element will only receive focus when the previous value was something other than true (similar to regular property change detection). When passing a function, the element will receive focus when true is returned, regardless of what the previous return value was.

For example:

Given element ordering, the following ‘firstFocus’ input will receive focus on the initial render, whereas the ‘subsequentFocus’ input will receive focus for all future renders as it uses a function for its focus property.

src/widgets/FocusExample.tsx

Function-based variant:

  1. import { create, tsx, invalidator } from '@dojo/framework/core/vdom';
  2. const factory = create({ invalidator });
  3. export default factory(function FocusExample({ middleware: { invalidator } }) {
  4. return (
  5. <div>
  6. <input key="subsequentFocus" type="text" focus={() => true} />
  7. <input key="firstFocus" type="text" focus={true} />
  8. <button onclick={() => invalidator()}>Re-render</button>
  9. </div>
  10. );
  11. });

Class-based variant:

  1. import WidgetBase from '@dojo/framework/core/WidgetBase';
  2. import { tsx } from '@dojo/framework/core/vdom';
  3. export default class FocusExample extends WidgetBase {
  4. protected render() {
  5. return (
  6. <div>
  7. <input key="subsequentFocus" type="text" focus={() => true} />
  8. <input key="firstFocus" type="text" focus={true} />
  9. <button onclick={() => this.invalidate()}>Re-render</button>
  10. </div>
  11. );
  12. }
  13. }

Delegating focus

Function-based widgets can use the focus middleware to provide focus to their children or to accept focus from a parent widget. Class-based widgets can use the FocusMixin (from @dojo/framework/core/mixins/Focus) to delegate focus in a similar way.

FocusMixin adds a this.shouldFocus() method to a widget’s class, whereas function-based widgets use the focus.shouldFocus() middleware method for the same purpose. This method checks if the widget is in a state to perform a focus action and will only return true for a single invocation, until the widget’s this.focus() method has been called again (function-based widgets use the focus.focus() middleware equivalent).

FocusMixin or the focus middleware also add a focus function property to a widget’s API. The framework uses the boolean result from this property to determine if the widget (or one of its children) should receive focus when rendering. Typically, widgets pass the shouldFocus method to a specific child widget or an output node via their focus property, allowing parent widgets to delegate focus to their children.

See the focus middleware delegation example in the Dojo middleware reference guide for an example for function-based widgets.

The following shows an example of delegating and controlling focus across a class-based widget hierarchy and output VNodes:

src/widgets/FocusableWidget.tsx

  1. import WidgetBase from '@dojo/framework/core/WidgetBase';
  2. import { tsx } from '@dojo/framework/core/vdom';
  3. import Focus from '@dojo/framework/core/mixins/Focus';
  4. interface FocusInputChildProperties {
  5. onFocus: () => void;
  6. }
  7. class FocusInputChild extends Focus(WidgetBase)<FocusInputChildProperties> {
  8. protected render() {
  9. /*
  10. The child widget's `this.shouldFocus()` method is assigned directly to the
  11. input node's `focus` property, allowing focus to be delegated from a higher
  12. level containing parent widget.
  13. The input's `onfocus()` event handler is also assigned to a method passed
  14. in from a parent widget, allowing user-driven focus changes to propagate back
  15. into the application.
  16. */
  17. return <input onfocus={this.properties.onFocus} focus={this.shouldFocus} />;
  18. }
  19. }
  20. export default class FocusableWidget extends Focus(WidgetBase) {
  21. private currentlyFocusedKey = 0;
  22. private childCount = 5;
  23. private onFocus(key: number) {
  24. this.currentlyFocusedKey = key;
  25. this.invalidate();
  26. }
  27. /*
  28. Calling `this.focus()` resets the widget so that `this.shouldFocus()` will return true when it is next invoked.
  29. */
  30. private focusPreviousChild() {
  31. --this.currentlyFocusedKey;
  32. if (this.currentlyFocusedKey < 0) {
  33. this.currentlyFocusedKey = this.childCount - 1;
  34. }
  35. this.focus();
  36. }
  37. private focusNextChild() {
  38. ++this.currentlyFocusedKey;
  39. if (this.currentlyFocusedKey === this.childCount) {
  40. this.currentlyFocusedKey = 0;
  41. }
  42. this.focus();
  43. }
  44. protected render() {
  45. /*
  46. The parent widget's `this.shouldFocus()` method is passed to the relevant child element
  47. that requires focus, based on the simple previous/next widget selection logic.
  48. This allows focus to be delegated to a specific child node based on higher-level logic in
  49. a container/parent widget.
  50. */
  51. return (
  52. <div>
  53. <button onclick={this.focusPreviousChild}>Previous</button>
  54. <button onclick={this.focusNextChild}>Next</button>
  55. <FocusInputChild
  56. key={0}
  57. focus={this.currentlyFocusedKey === 0 ? this.shouldFocus : undefined}
  58. onFocus={() => this.onFocus(0)}
  59. />
  60. <FocusInputChild
  61. key={1}
  62. focus={this.currentlyFocusedKey === 1 ? this.shouldFocus : undefined}
  63. onFocus={() => this.onFocus(1)}
  64. />
  65. <FocusInputChild
  66. key={2}
  67. focus={this.currentlyFocusedKey === 2 ? this.shouldFocus : undefined}
  68. onFocus={() => this.onFocus(2)}
  69. />
  70. <FocusInputChild
  71. key={3}
  72. focus={this.currentlyFocusedKey === 3 ? this.shouldFocus : undefined}
  73. onFocus={() => this.onFocus(3)}
  74. />
  75. <FocusInputChild
  76. key={4}
  77. focus={this.currentlyFocusedKey === 4 ? this.shouldFocus : undefined}
  78. onFocus={() => this.onFocus(4)}
  79. />
  80. </div>
  81. );
  82. }
  83. }