|
| 1 | +import { UMB_NAMABLE_WORKSPACE_CONTEXT } from '../../namable/index.js'; |
| 2 | +import { UmbTextStyles } from '@umbraco-cms/backoffice/style'; |
| 3 | +import { css, html, customElement, state } from '@umbraco-cms/backoffice/external/lit'; |
| 4 | +import { umbFocus, UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; |
| 5 | +import type { UUIInputElement } from '@umbraco-cms/backoffice/external/uui'; |
| 6 | +import { UUIInputEvent } from '@umbraco-cms/backoffice/external/uui'; |
| 7 | +import { umbBindToValidation } from '@umbraco-cms/backoffice/validation'; |
| 8 | + |
| 9 | +@customElement('umb-workspace-name') |
| 10 | +export class UmbWorkspaceNameElement extends UmbLitElement { |
| 11 | + @state() |
| 12 | + private _name = ''; |
| 13 | + |
| 14 | + #workspaceContext?: typeof UMB_NAMABLE_WORKSPACE_CONTEXT.TYPE; |
| 15 | + |
| 16 | + constructor() { |
| 17 | + super(); |
| 18 | + |
| 19 | + this.consumeContext(UMB_NAMABLE_WORKSPACE_CONTEXT, (workspaceContext) => { |
| 20 | + this.#workspaceContext = workspaceContext; |
| 21 | + this.#observeName(); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + #observeName() { |
| 26 | + if (!this.#workspaceContext) return; |
| 27 | + this.observe(this.#workspaceContext.name, (name) => { |
| 28 | + if (name !== this._name) { |
| 29 | + this._name = name ?? ''; |
| 30 | + } |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + #onNameInput(event: UUIInputEvent) { |
| 35 | + if (event instanceof UUIInputEvent) { |
| 36 | + const target = event.composedPath()[0] as UUIInputElement; |
| 37 | + |
| 38 | + if (typeof target?.value === 'string') { |
| 39 | + this.#workspaceContext?.setName(target.value); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + override render() { |
| 45 | + return html`<uui-input |
| 46 | + id="nameInput" |
| 47 | + .value=${this._name} |
| 48 | + @input="${this.#onNameInput}" |
| 49 | + required |
| 50 | + ${umbBindToValidation(this, `$.name`, this._name)} |
| 51 | + ${umbFocus()}></uui-input>`; |
| 52 | + } |
| 53 | + |
| 54 | + static override styles = [ |
| 55 | + UmbTextStyles, |
| 56 | + css` |
| 57 | + :host { |
| 58 | + display: contents; |
| 59 | + } |
| 60 | +
|
| 61 | + #nameInput { |
| 62 | + flex: 1 1 auto; |
| 63 | + } |
| 64 | + `, |
| 65 | + ]; |
| 66 | +} |
| 67 | + |
| 68 | +declare global { |
| 69 | + interface HTMLElementTagNameMap { |
| 70 | + 'umb-workspace-name': UmbWorkspaceNameElement; |
| 71 | + } |
| 72 | +} |
0 commit comments