Skip to content

Commit 3a8e733

Browse files
committed
feat: channel key inputs to input when focused
1 parent 3104d52 commit 3a8e733

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

examples/gauge/Components/Input.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { ComponentProps, DisplayComponent, FSComponent, Subscribable, UUID, VNode } from "@microsoft/msfs-sdk"
2+
3+
interface InputProps extends ComponentProps {
4+
type: HTMLInputElement["type"]
5+
name?: string
6+
value?: string
7+
class?: string | Subscribable<string>
8+
}
9+
10+
export class Input extends DisplayComponent<InputProps> {
11+
private readonly inputId = UUID.GenerateUuid()
12+
private readonly inputRef = FSComponent.createRef<HTMLInputElement>()
13+
14+
get value() {
15+
return this.inputRef.instance.value
16+
}
17+
18+
onAfterRender(node: VNode): void {
19+
super.onAfterRender(node)
20+
21+
this.inputRef.instance.onfocus = this.onInputFocus
22+
this.inputRef.instance.onblur = this.onInputBlur
23+
}
24+
25+
private getInputProps() {
26+
return { name: this.props.name, value: this.props.value, class: this.props.class }
27+
}
28+
29+
/**
30+
* Method to handle when input focus is set
31+
* @param e The focus event.
32+
*/
33+
private onInputFocus = (e: FocusEvent): void => {
34+
e.preventDefault()
35+
36+
Coherent.trigger("FOCUS_INPUT_FIELD", this.inputId, "", "", this.inputRef.instance.value, false)
37+
Coherent.on("mousePressOutsideView", () => this.inputRef.instance.blur())
38+
}
39+
40+
/**
41+
* Method to handle on input blur
42+
*/
43+
private onInputBlur = (): void => {
44+
Coherent.trigger("UNFOCUS_INPUT_FIELD", "")
45+
Coherent.off("mousePressOutsideView")
46+
}
47+
48+
render() {
49+
return <input ref={this.inputRef} {...this.getInputProps()} />
50+
}
51+
}

examples/gauge/Components/InterfaceSample.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
NavigraphNavigationDataInterface,
1010
} from "@navigraph/msfs-navigation-data-interface"
1111
import { Dropdown } from "./Dropdown"
12+
import { Input } from "./Input"
1213

1314
interface InterfaceSampleProps extends ComponentProps {
1415
bus: EventBus
@@ -22,7 +23,7 @@ export class InterfaceSample extends DisplayComponent<InterfaceSampleProps> {
2223
private readonly dropdownRef = FSComponent.createRef<Dropdown>()
2324
private readonly downloadButtonRef = FSComponent.createRef<HTMLButtonElement>()
2425
private readonly executeButtonRef = FSComponent.createRef<HTMLButtonElement>()
25-
private readonly inputRef = FSComponent.createRef<HTMLInputElement>()
26+
private readonly inputRef = FSComponent.createRef<Input>()
2627

2728
private cancelSource = CancelToken.source()
2829

@@ -68,7 +69,7 @@ export class InterfaceSample extends DisplayComponent<InterfaceSampleProps> {
6869
<div ref={this.downloadButtonRef} class="button">
6970
Download
7071
</div>
71-
<input ref={this.inputRef} type="text" id="sql" name="sql" value="ESSA" class="text-field" />
72+
<Input ref={this.inputRef} type="text" value="ESSA" class="text-field" />
7273
<div ref={this.executeButtonRef} class="button">
7374
Fetch Airport
7475
</div>

examples/gauge/global.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
interface CoherentEngine {
2+
/**
3+
* Asynchronously call a C++ handler and retrieve the result
4+
* @param name name of the C++ handler to be called
5+
* @param args any extra parameters to be passed to the C++ handler
6+
* @return promise for the result of the C++ function
7+
*/
8+
call(name: "PLAY_INSTRUMENT_SOUND", soundName: string): Promise<void>
9+
call(name: string, ...args: unknown[]): Promise<unknown>
10+
11+
on(name: "SetInputTextFromOS" | "mousePressOutsideView", cb: () => void): void
12+
off(name: "SetInputTextFromOS" | "mousePressOutsideView", cb?: () => void): void
13+
14+
trigger(name: "FOCUS_INPUT_FIELD" | "UNFOCUS_INPUT_FIELD", ...args: unknown[])
15+
}

0 commit comments

Comments
 (0)