EditableText
Switch between saved text and an editor for names, notes and other small fields in detail views.
Import
import { HuiEditableText } from '@nscode/handy-ui'
import type { EditableTextProps, EditableTextExpose } from '@nscode/handy-ui'
Examples
Four triggers and empty values
icon uses the edit button, click uses a single click, and dblclick uses a double click. The latter two also support Enter and Space. Use external editing or edit() with none. An empty empty-text remains an explicit override.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiEditableText, HuiButton, HuiSpace } from '@nscode/handy-ui'
const icon = ref('Use the edit icon')
const click = ref('Click this text')
const double = ref('Double-click this text')
const external = ref('Controlled externally')
const editing = ref(false)
const empty = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiEditableText v-model="icon" trigger="icon" />
<HuiEditableText v-model="click" trigger="click" />
<HuiEditableText v-model="double" trigger="dblclick" />
<HuiSpace wrap>
<HuiButton @click="editing = true">Edit the controlled field</HuiButton>
<HuiEditableText v-model="external" v-model:editing="editing" trigger="none" />
<span>editing: {{ editing }}</span>
</HuiSpace>
<HuiEditableText
v-model="empty"
empty-text="Add a project alias"
placeholder="For example: Eastern project"
/>
</HuiSpace>
</template>
Sizes and disabled state
Display text, editors and action buttons share the selected size. The inherited order is InputGroup, FormItem, Form and ConfigProvider when size is omitted.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiEditableText, HuiCheckbox, HuiSpace } from '@nscode/handy-ui'
const large = ref('large 40px')
const normal = ref('default 32px')
const small = ref('small 24px')
const disabled = ref(false)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCheckbox v-model="disabled">Disabled</HuiCheckbox>
<HuiEditableText v-model="large" size="large" :disabled="disabled" show-actions />
<HuiEditableText v-model="normal" size="default" :disabled="disabled" show-actions />
<HuiEditableText v-model="small" size="small" :disabled="disabled" show-actions />
</HuiSpace>
</template>
Multiline, autosize and length limits
Enter saves a single-line editor. In a textarea, Enter inserts a newline; Ctrl+Enter or ⌘Enter saves. Enter and Escape do not commit during IME composition.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiEditableText, HuiSpace } from '@nscode/handy-ui'
const note = ref('First line\nSecond line')
const short = ref('Up to 20 units')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiEditableText
v-model="note"
type="textarea"
:autosize="{ minRows: 2, maxRows: 5 }"
:maxlength="120"
placeholder="Add a note"
show-actions
style="width: 100%"
/>
<HuiEditableText v-model="short" :maxlength="20" placeholder="Short description" />
<span>Saved note: {{ note }}</span>
</HuiSpace>
</template>
Async saving and action buttons
Enter “error” to simulate a failure. Editors become readonly and retain focus while waiting; action buttons use aria-disabled. Escape, cancellation and repeated saving wait for the result, avoiding loss of an already saved result. A failure keeps the draft and focuses the editor again. Unchanged values exit without calling before-save.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiEditableText, HuiCheckbox, HuiMessage, HuiSpace } from '@nscode/handy-ui'
const value = ref('A name saved to the server')
const actions = ref(true)
const result = ref('Ready')
const beforeSave = async (next: string, previous: string) => {
result.value = `Saving: ${previous} → ${next}`
await new Promise((resolve) => setTimeout(resolve, 1000))
if (next === 'error') {
HuiMessage.error('Saving failed. Change the text and retry.')
result.value = 'Failed; the draft is preserved'
return false
}
return true
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCheckbox v-model="actions">Show save and cancel buttons</HuiCheckbox>
<HuiEditableText
v-model="value"
:before-save="beforeSave"
:show-actions="actions"
@edit-start="result = 'Editing started'"
@save="(next, old) => (result = `Saved: ${old} → ${next}`)"
@cancel="result = 'Cancelled'"
/>
<span>{{ result }}</span>
</HuiSpace>
</template>
Save or cancel on blur
Moving focus to the component's save or cancel button keeps the session open. Moving outside follows save-on-blur and preserves the external focus.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiEditableText, HuiButton, HuiSpace } from '@nscode/handy-ui'
const save = ref('Saved when focus leaves')
const cancel = ref('Cancelled when focus leaves')
const event = ref('Waiting for focus to leave')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiEditableText v-model="save" :save-on-blur="true" @save="event = 'First field saved'" />
<HuiEditableText
v-model="cancel"
:save-on-blur="false"
@cancel="event = 'Second field cancelled'"
/>
<HuiButton>Move focus here</HuiButton>
<span>{{ event }}</span>
</HuiSpace>
</template>
Controlled editing
The presence of editing makes it controlled; explicitly supplied undefined means false. The trigger requests update:editing, and the editor opens only after the parent confirms true. Refusing a false request keeps it open. Without the attribute, the component maintains edit state; an update listener alone does not make it controlled. Adding or removing the attribute warns during development. When model-value is supplied, displayed text follows the parent, so handle value updates as well.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiEditableText, HuiButton, HuiCheckbox, HuiSpace } from '@nscode/handy-ui'
const value = ref('A controlled business name')
const editing = ref(false)
const allow = ref(true)
const undefinedValue = ref('Explicit undefined is controlled')
const undefinedEditing = ref<boolean | undefined>(undefined)
const lastRequest = ref('No request yet')
const request = (next: boolean) => {
if (!next || allow.value) editing.value = next
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCheckbox v-model="allow">Allow entering edit mode</HuiCheckbox>
<HuiEditableText v-model="value" :editing="editing" @update:editing="request" show-actions />
<span>Current editing: {{ editing }}</span>
<HuiEditableText
v-model="undefinedValue"
:editing="undefinedEditing"
@update:editing="
lastRequest = `Requested: ${$event}; the external buttons decide whether to accept`
"
/>
<HuiSpace wrap>
<HuiButton @click="undefinedEditing = true">Confirm editing externally</HuiButton>
<HuiButton @click="undefinedEditing = undefined">Stay controlled and exit</HuiButton>
<span>{{ lastRequest }}</span>
</HuiSpace>
</HuiSpace>
</template>
Uncontrolled values and explicit undefined
Without model-value, the saved value starts as an empty string and is maintained internally. An update listener alone keeps it uncontrolled. Explicit :model-value="undefined" is controlled and displays an empty string: saving emits a request, but refusing to accept it leaves the display empty. Both modes emit update:modelValue before save. Adding or removing model-value warns during development; switching back to uncontrolled mode restores the previous internal saved value.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiEditableText, HuiSpace } from '@nscode/handy-ui'
const event = ref('Waiting for a save')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiEditableText
empty-text="No bound value: saved internally"
@save="(next, old) => (event = `Saved internally: ${old} → ${next}`)"
/>
<HuiEditableText
:model-value="undefined"
empty-text="Explicit undefined: stays empty without acceptance"
@update:model-value="event = `Controlled value request: ${$event}`"
/>
<span>{{ event }}</span>
</HuiSpace>
</template>
Custom display and editor
The editor slot receives draft value, readonly busy, update(value), save() and cancel(). Entering focuses its first focusable element. All three callbacks ignore new actions while waiting. Map busy to editor readonly state and button aria-disabled, retaining focus without native disabled or inert. This example uses Form's readonly context for Select.
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiEditableText,
HuiSelect,
HuiTag,
HuiButton,
HuiSpace,
HuiForm
} from '@nscode/handy-ui'
const status = ref('open')
const options = [
{ value: 'open', label: 'In progress' },
{ value: 'done', label: 'Completed' }
]
const label = (value: string | undefined) =>
options.find((item) => item.value === value)?.label ?? 'Not selected'
</script>
<template>
<HuiEditableText v-model="status">
<template #default="{ value }"
><HuiTag>{{ label(value) }}</HuiTag></template
>
<template #editor="{ value, busy, update, save, cancel }">
<HuiForm :readonly="busy">
<HuiSpace wrap>
<HuiSelect
:model-value="value"
:options="options"
@update:model-value="(next) => update(String(next))"
style="width: 160px"
/>
<HuiButton :aria-disabled="busy || undefined" @click="save">Confirm status</HuiButton>
<HuiButton :aria-disabled="busy || undefined" @click="cancel">Cancel</HuiButton>
</HuiSpace>
</HuiForm>
</template>
</HuiEditableText>
</template>
Attributes and methods
class, style, data-* and undeclared business events stay on the root. id and aria-* are placed on the current display trigger or built-in native editor, avoiding duplicate IDs. Set native attributes on custom editors through the slot.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiEditableText, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { EditableTextExpose } from '@nscode/handy-ui'
const text = ref<EditableTextExpose>()
const value = ref('Supports external methods')
const event = ref('Ready')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @click="text?.edit()">edit()</HuiButton>
<HuiButton @mousedown.prevent @click="text?.save()">save()</HuiButton>
<HuiButton @mousedown.prevent @click="text?.cancel()">cancel()</HuiButton>
</HuiSpace>
<HuiEditableText
ref="text"
v-model="value"
trigger="none"
id="editable-business-title-en"
class="business-title"
data-field="title"
aria-label="Business title"
aria-describedby="editable-title-help-en"
style="min-width: 240px"
@save="event = 'Saved by method'"
@cancel="event = 'Cancelled by method'"
/>
<span id="editable-title-help-en">Enter the complete business title.</span>
<span>{{ event }}</span>
</HuiSpace>
</template>
Form inheritance and detail views
A readonly Form preserves the display and blocks editing and saving. FormItem readonly=false explicitly allows its field. Disabled state also blocks methods. Built-in editors do not register a second FormItem input; labels reference the current trigger or native editor.
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiEditableText,
HuiDescriptions,
HuiDescriptionsItem,
HuiForm,
HuiFormItem,
HuiCheckbox,
HuiSpace
} from '@nscode/handy-ui'
const person = reactive({ name: 'Alex', note: 'Eastern region', alias: 'Al' })
const readonly = ref(false)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiCheckbox v-model="readonly">Readonly form</HuiCheckbox>
<HuiForm :model="person" :readonly="readonly">
<HuiFormItem label="Name" prop="name"><HuiEditableText v-model="person.name" /></HuiFormItem>
<HuiFormItem label="Alias (editable)" prop="alias" :readonly="false"
><HuiEditableText v-model="person.alias"
/></HuiFormItem>
</HuiForm>
<HuiDescriptions title="Person details" :column="1" border>
<HuiDescriptionsItem label="Name"
><HuiEditableText v-model="person.name" show-actions
/></HuiDescriptionsItem>
<HuiDescriptionsItem label="Notes"
><HuiEditableText v-model="person.note" type="textarea" autosize show-actions
/></HuiDescriptionsItem>
</HuiDescriptions>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string | — | Attribute presence makes it controlled; undefined is empty; omitted values are stored internally |
| editing | boolean | — | Attribute presence makes it controlled; undefined means false; supports v-model:editing |
| trigger | 'icon' | 'click' | 'dblclick' | 'none' | 'icon' | Entry trigger |
| type | 'input' | 'textarea' | 'input' | Built-in editor |
| beforeSave | (value: string, oldValue: string) => boolean | void | Promise<boolean | void> | — | False, exceptions and rejected promises keep editing |
| saveOnBlur | boolean | true | Save when focus leaves; false cancels |
| showActions | boolean | false | Show save and cancel buttons |
| emptyText | string | Locale | Empty-value text; empty string is valid |
| placeholder | string | — | Built-in editor placeholder |
| maxlength | number | — | Maximum UTF-16 length for built-in editors |
| autosize | boolean | { minRows?: number; maxRows?: number } | false | Textarea autosizing |
| disabled | boolean | false | Also inherits InputGroup and Form disabled state |
| size | 'large' | 'default' | 'small' | Inherited | Component size |
Events
| Event | Arguments | Description |
|---|---|---|
| update:modelValue | value: string | Request the saved value update |
| update:editing | editing: boolean | Request an edit-state change |
| editStart | None | Editing actually starts |
| save | value: string, oldValue: string | Validation passed and the value changed |
| cancel | None | User or method cancelled editing |
Slots
| Slot | Parameters | Description |
|---|---|---|
| default | { value: string | undefined } | Saved-value display |
| editor | { value: string, readonly busy: boolean, update: (value: string) => void, save: () => Promise<void>, cancel: () => void } | Custom editor; readonly and focus-preserving while busy |
Methods
| Method | Signature | Description |
|---|---|---|
| edit | () => void | Request editing; respects disabled and readonly state |
| save | () => Promise<void> | Request saving; does not guarantee parent value acceptance |
| cancel | () => void | Revert the draft and request exit; ignored while saving |
Types
Exports: EditableTextProps, EditableTextEmits, EditableTextSlots, EditableTextDisplaySlotProps, EditableTextEditorSlotProps, EditableTextExpose, EditableTextTrigger, EditableTextType. Property, event and slot signatures appear above.
interface EditableTextExpose {
edit: () => void
save: () => Promise<void>
cancel: () => void
}
type EditableTextTrigger = 'icon' | 'click' | 'dblclick' | 'none'
type EditableTextType = 'input' | 'textarea'