InteractiveComputer/UI/ComputerMessageBoxTextEntry.razor
@namespace PaneOS.InteractiveComputer
@using System
@using Microsoft.AspNetCore.Components
@using PaneOS.InteractiveComputer
@using Sandbox.UI
@inherits TextEntry

@code
{
	[Parameter] public ComputerActiveMessageBox? MessageBox { get; set; }
	[Parameter] public Action<string>? OnTextChanged { get; set; }
	private Guid boundId;

	protected override void OnAfterTreeRender( bool firstTime )
	{
		base.OnAfterTreeRender( firstTime );
		AddClass( "message-box-input" );
		OnTextEdited = HandleTextEdited;

		if ( MessageBox is null )
			return;

		Placeholder = MessageBox.Options.TextInputPlaceholder;
		if ( boundId != MessageBox.Id )
		{
			boundId = MessageBox.Id;
			Text = MessageBox.CurrentText;
			CaretPosition = TextLength;
			Focus();
		}
	}

	public override void Tick()
	{
		base.Tick();

		if ( MessageBox is null )
			return;

		if ( MessageBox.CurrentText != Text )
		{
			MessageBox.CurrentText = Text;
			OnTextChanged?.Invoke( Text );
		}
	}

	private void HandleTextEdited( string value )
	{
		if ( MessageBox is not null )
			MessageBox.CurrentText = value;

		OnTextChanged?.Invoke( value );
	}
}