@molecule/app-inline-edit-angular

Feature · inline-edit · App (browser) · v1.1.3 · Apache-2.0

Click-to-edit text + textarea + select with Save/Cancel

npm install @molecule/app-inline-edit-angular

npm · Source on GitHub

How it works

@molecule/app-inline-edit-angular is a ready-made inline-edit feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

import { Component, signal } from '@angular/core'
import { InlineEdit } from '@molecule/app-inline-edit-angular'

@Component({
  selector: 'app-deal',
  standalone: true,
  imports: [InlineEdit],
  template: `
    <mol-inline-edit [value]="title()" [onSubmit]="saveTitle" placeholder="Enter deal title" />
  `,
})
export class Deal {
  readonly title = signal('Acme renewal')
  saveTitle = async (next: string): Promise<void> => {
    this.title.set(next)
  }
}

Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular

Reference

Angular click-to-edit inline field — the counterpart of @molecule/app-inline-edit-react: the same props and the same DOM for the same input.

Exports <InlineEdit>: the value rendered as text, with a click (or Tab + Enter) swapping in an input or textarea plus Save/Cancel buttons. Enter submits (Cmd/Ctrl+Enter for the textarea variant), Escape cancels.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { Component, signal } from '@angular/core'
import { InlineEdit } from '@molecule/app-inline-edit-angular'

@Component({
  selector: 'app-deal',
  standalone: true,
  imports: [InlineEdit],
  template: `
    <mol-inline-edit [value]="title()" [onSubmit]="saveTitle" placeholder="Enter deal title" />
  `,
})
export class Deal {
  readonly title = signal('Acme renewal')
  saveTitle = async (next: string): Promise<void> => {
    this.title.set(next)
  }
}

Type

feature

Installation

npm install @molecule/app-inline-edit-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

InlineEditProps

Props accepted by InlineEdit — the same props React's InlineEdit accepts.

interface InlineEditProps {
  /** Current value. */
  value: string
  /** Called when the user commits a new value. Return a Promise to block the UI while saving. */
  onSubmit: (next: string) => void | Promise<void>
  /** Render mode for the editor. */
  variant?: 'input' | 'textarea'
  /** Renderer for the read state — an `ng-template` receiving the value as `$implicit` / `a0`. */
  renderRead?: TemplateRef<unknown> | null
  /**
   * Placeholder when value is empty. Also becomes the editor field's
   * accessible label while editing; without it the field falls back to the
   * translated `inlineEdit.aria.edit` default.
   */
  placeholder?: string
  /** Extra classes. */
  className?: string
}

Classes

InlineEdit

InlineEdit component.

Click-to-edit text, the Angular counterpart of @molecule/app-inline-edit-react's InlineEdit: the same props (React names), the same DOM for the same input. React's root element is a <span> in the read state and a <div> in the editor, so the rendered root stands in front of the host's place and the host itself leaves the page (placeContentBefore + setElementPresent, React's fragment root).

React's renderRead render prop is an ng-template receiving the value as $implicit / a0.

@Component({
  selector: 'mol-inline-edit',
  standalone: true,
  hostDirectives: [MolPlacement],
  imports: [Button, Input, NgTemplateOutlet, Textarea],
  inputs: ['value', 'onSubmit', 'variant', 'renderRead', 'placeholder', 'className'],
  template: `@if (!editing()) {
      <span
        role="button"
        tabindex="0"
        [class]="readClasses()"
        (click)="startEditing()"
        (keydown)="onReadKey($event)"
      >
        @if (value) {
          @if (renderReadTemplate(); as custom) {
            <ng-container [ngTemplateOutlet]="custom" [ngTemplateOutletContext]="readContext()" />
          } @else {
            <ng-container>{{ value }}</ng-container>
          }
        } @else {
          <span style="opacity: 0.5">{{ placeholderText() }}</span>
        }
      </span>
    } @else {
      <div #editorRoot [class]="editClasses()">
        @if (variantMode() === 'textarea') {
          <div
            mol-textarea
            [value]="draft()"
            (change)="onInput($event)"
            (keydown)="onKey($event)"
            [attr.aria-label]="ariaLabel()"
          ></div>
        } @else {
          <div
            mol-input
            [type]="'text'"
            [value]="draft()"
            (change)="onInput($event)"
            (keydown)="onKey($event)"
            [attr.aria-label]="ariaLabel()"
          ></div>
        }
        <button
          mol-button
          [variant]="'ghost'"
          [size]="'sm'"
          (click)="cancel()"
          [disabled]="saving()"
        >
          {{ cancelText() }}
        </button>
        <button
          mol-button
          [variant]="'solid'"
          [color]="'primary'"
          [size]="'sm'"
          (click)="save()"
          [disabled]="saving()"
        >
          {{ saveText() }}
        </button>
      </div>
    }`,
})
export class InlineEdit implements OnChanges {
  /** Current value. */
  value!: string
  /** Called when the user commits a new value (React's `onSubmit`). */
  onSubmit?: (next: string) => void | Promise<void>
  /** Render mode for the editor (React's `'input'` default at read time). */
  variant?: 'input' | 'textarea'
  /** Renderer for the read state — an `ng-template` receiving the value. */
  renderRead?: TemplateRef<unknown> | null
  /** Placeholder when value is empty. */
  placeholder?: string
  /** Extra classes. */
  className?: string

  /** Whether the editor is open. */
  protected readonly editing = signal(false)
  /** The draft value the field shows. */
  protected readonly draft = signal('')
  /** Whether a save is in flight (both buttons are disabled). */
  protected readonly saving = signal(false)

  /** The component host, whose subtree the editor field is found in. */
  private readonly host = inject(ElementRef<HTMLElement>)
  /** The injector `afterNextRender` needs outside a constructor. */
  private readonly injector = inject(Injector)

  /** The editor's root element (only present while editing). */
  @ViewChild('editorRoot') private editorRoot?: ElementRef<HTMLElement>

  /** Reactive translation, read in the injection context. */
  private readonly i18n = useTranslation()

  constructor() {
    usePlacement(() => this.adopt())
  }

  /** React's `useEffect(() => setDraft(value), [value])`: a changed `value` replaces the draft, including mid-edit. */
  ngOnChanges(changes: SimpleChanges): void {
    if ('value' in changes) this.draft.set(this.value)
  }

  /** React's root element does not exist as a host: the rendered root (the read `<span>` or the editor `<div>`) stands in front of the host's place and the host its… */
  private adopt(): void {
    const host = this.host.nativeElement
    placeContentBefore(host, host)
    setElementPresent(host, false)
  }

  /**
   * The current class map.
   * @returns The UIClassMap.
   */
  cm(): UIClassMap {
    return getClassMap()
  }

  /**
   * The read state's class list (React's `cm.cn(cm.cursorPointer, className)`).
   * @returns The class string.
   */
  readClasses(): string {
    return this.cm().cn(this.cm().cursorPointer, this.className)
  }

  /**
   * The editor's class list (React's `cm.cn(cm.flex({ align: 'center', gap: 'sm' }), className)`).
   * @returns The class string.
   */
  editClasses(): string {
    return this.cm().cn(this.cm().flex({ align: 'center', gap: 'sm' }), this.className)
  }

  /** The placeholder text React renders for an empty value. */
  placeholderText(): string {
    return (
      this.placeholder ?? this.i18n.t('inlineEdit.empty', {}, { defaultValue: 'Click to edit' })
    )
  }

  /**
   * The field's accessible label (React's
   * `aria-label={placeholder ?? t('inlineEdit.aria.edit', …)}`).
   * @returns The aria label.
   */
  ariaLabel(): string {
    return this.placeholder ?? this.i18n.t('inlineEdit.aria.edit', {}, { defaultValue: 'Edit' })
  }

  /**
   * The Cancel button's label.
   * @returns The translated label.
   */
  cancelText(): string {
    return this.i18n.t('inlineEdit.cancel', {}, { defaultValue: 'Cancel' })
  }

  /**
   * The Save button's label, or the saving label while a save is in flight.
   * @returns The translated label.
   */
  saveText(): string {
    return this.saving()
      ? this.i18n.t('inlineEdit.saving', {}, { defaultValue: 'Saving…' })
      : this.i18n.t('inlineEdit.save', {}, { defaultValue: 'Save' })
  }

  /**
   * The renderer mode with React's `'input'` default applied.
   * @returns The mode.
   */
  variantMode(): 'input' | 'textarea' {
    return this.variant ?? 'input'
  }

  /** The `renderRead` template, when the caller supplied one. */
  renderReadTemplate(): TemplateRef<unknown> | null {
    return isTemplate(this.renderRead) ? this.renderRead : null
  }

  /**
   * The context the `renderRead` template receives (the value as `$implicit` / `a0`).
   * @returns The template context.
   */
  readContext(): Record<string, unknown> {
    const current = this.value
    return { $implicit: current, a0: current, a1: undefined, a2: undefined }
  }

  /** Opens the editor, as React's click handler on the read state does. */
  startEditing(): void {
    this.editing.set(true)
    // React focuses the field in an effect after the editor mounts; there is
    // no field before this render, so focus after the next one.
    afterNextRender(
      () => {
        const field = this.editorRoot?.nativeElement.querySelector('input, textarea')
        if (field instanceof HTMLElement) field.focus()
      },
      { injector: this.injector },
    )
  }

  /**
   * React's read-state `onKeyDown`: Enter opens the editor.
   * @param e - The keyboard event.
   */
  onReadKey(e: KeyboardEvent): void {
    if (e.key === 'Enter') this.startEditing()
  }

  /** Receives each typed value (React's `onChange`). */
  onInput(e: Event): void {
    this.draft.set((e.target as HTMLInputElement | HTMLTextAreaElement).value)
  }

  /** Handles keyboard events: Escape cancels, Enter (or Cmd/Ctrl+Enter for the textarea) submits. */
  onKey(e: KeyboardEvent): void {
    if (e.key === 'Escape') {
      e.preventDefault()
      this.cancel()
    }
    if (e.key === 'Enter' && this.variantMode() === 'input') {
      e.preventDefault()
      void this.commit()
    }
    if (e.key === 'Enter' && (e.metaKey || e.ctrlKey) && this.variantMode() === 'textarea') {
      e.preventDefault()
      void this.commit()
    }
  }

  /** Cancels editing, restoring the draft to the current committed value. */
  cancel(): void {
    this.draft.set(this.value)
    this.editing.set(false)
  }

  /** Saves the draft, as React's Save button does. */
  save(): void {
    void this.commit()
  }

  /** Submits the current draft value by calling onSubmit, then closes the editor. */
  async commit(): Promise<void> {
    if (this.saving()) return
    this.saving.set(true)
    try {
      await this.onSubmit?.(this.draft())
      this.editing.set(false)
    } finally {
      this.saving.set(false)
    }
  }
}

(The 9 longest member comments are shortened to their first sentence to fit; the full declaration is in node_modules/@molecule/app-inline-edit-angular/dist/inline-edit.component.d.ts.)

Injection Notes

Requirements

Peer dependencies:

  • @angular/common ^22.0.0
  • @angular/core ^22.0.0
  • @angular/platform-browser ^22.0.0
  • @molecule/app-angular ^3.13.0 || ^4.0.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-angular ^1.1.0

Runtime Dependencies

  • @angular/common

  • @angular/core

  • @angular/platform-browser

  • @molecule/app-angular

  • @molecule/app-ui

  • @molecule/app-ui-angular

  • Requires a wired ClassMap bond and @molecule/app-angular's i18n provider above it — getClassMap() and useTranslation() both throw before wiring. Pair with @molecule/app-locales-inline-edit for the placeholder / button strings in 79 languages.

  • Return a Promise from onSubmit to disable the buttons while saving. If onSubmit REJECTS, the editor stays open with the draft intact but the error is not displayed — catch and surface errors inside your onSubmit (toast, form error, etc.).

  • The draft re-syncs from value whenever the input changes, including mid-edit — avoid mutating value while the user is typing.

  • React's renderRead render prop is an ng-template receiving the value as $implicit / a0.

Translations

Translation strings are provided by @molecule/app-locales-inline-edit.