import { Editable } from "@chakra-ui/react"
export const EditableBasic = () => (
<Editable.Root textAlign="start" defaultValue="Click to edit">
<Editable.Preview />
<Editable.Input />
</Editable.Root>
)
用法
¥Usage
import { Editable } from "@chakra-ui/react"
<Editable.Root>
<Editable.Preview />
<Editable.Input />
</Editable.Root>
示例
¥Examples
双精度点击
¥Double Click
使用 activationMode 属性使用户双击时内容可编辑。
¥Use the activationMode prop to make the content editable when users double
click.
import { Editable } from "@chakra-ui/react"
export const EditableWithDoubleClick = () => (
<Editable.Root defaultValue="Double click to edit" activationMode="dblclick">
<Editable.Preview />
<Editable.Input />
</Editable.Root>
)
已禁用
¥Disabled
使用 disabled 属性禁用可编辑组件。
¥Use the disabled prop to disable the editable component.
import { Editable } from "@chakra-ui/react"
const Demo = () => {
return (
<Editable.Root disabled defaultValue="Click to edit">
<Editable.Preview opacity={0.5} cursor="not-allowed" />
<Editable.Input />
</Editable.Root>
)
}
文本区域
¥Textarea
你可以使文本区域可编辑。
¥You can make a text area editable.
import { Editable } from "@chakra-ui/react"
const Demo = () => {
return (
<Editable.Root defaultValue="Click to edit">
<Editable.Preview minH="48px" alignItems="flex-start" width="full" />
<Editable.Textarea />
</Editable.Root>
)
}
包含控件
¥With Controls
为 Editable 添加控件,例如 "edit"、"取消" 和 "submit",以提升用户体验。
¥Add controls such as "edit", "cancel" and "submit" to Editable for better user
experience.
import { Editable, IconButton } from "@chakra-ui/react"
import { LuCheck, LuPencilLine, LuX } from "react-icons/lu"
const Demo = () => {
return (
<Editable.Root defaultValue="Click to edit">
<Editable.Preview />
<Editable.Input />
<Editable.Control>
<Editable.EditTrigger asChild>
<IconButton variant="ghost" size="xs">
<LuPencilLine />
</IconButton>
</Editable.EditTrigger>
<Editable.CancelTrigger asChild>
<IconButton variant="outline" size="xs">
<LuX />
</IconButton>
</Editable.CancelTrigger>
<Editable.SubmitTrigger asChild>
<IconButton variant="outline" size="xs">
<LuCheck />
</IconButton>
</Editable.SubmitTrigger>
</Editable.Control>
</Editable.Root>
)
}
受控
¥Controlled
使用 value 和 onValueChange 属性控制可编辑组件。
¥Use the value and onValueChange props to control the editable component.
"use client"
import { Editable } from "@chakra-ui/react"
import { useState } from "react"
const Demo = () => {
const [name, setName] = useState("")
return (
<Editable.Root
value={name}
onValueChange={(e) => setName(e.value)}
placeholder="Click to edit"
>
<Editable.Preview />
<Editable.Input />
</Editable.Root>
)
}
商店
¥Store
控制可编辑组件的另一种方法是使用 RootProvider 组件和 useEditable 存储钩子。
¥An alternative way to control the editable component is to use the
RootProvider component and the useEditable store hook.
这样,你就可以从可编辑状态外部访问可编辑状态和方法。
¥This way you can access the editable state and methods from outside the editable.
not editing"use client"
import { Code, Editable, Stack, useEditable } from "@chakra-ui/react"
const Demo = () => {
const editable = useEditable({
defaultValue: "Click to edit",
})
return (
<Stack align="flex-start">
<Editable.RootProvider value={editable}>
<Editable.Preview />
<Editable.Input />
</Editable.RootProvider>
<Code>{editable.editing ? "editing" : "not editing"}</Code>
</Stack>
)
}
属性
¥Props
根元素
¥Root
| Prop | Default | Type |
|---|---|---|
activationMode | '\'focus\'' | ActivationModeThe activation mode for the preview element. - "focus" - Enter edit mode when the preview is focused - "dblclick" - Enter edit mode when the preview is double-clicked - "click" - Enter edit mode when the preview is clicked - "none" - Edit can be triggered programmatically only |
selectOnFocus | true | booleanWhether to select the text in the input when it is focused. |
submitMode | '\'both\'' | SubmitModeThe action that triggers submit in the edit mode: - "enter" - Trigger submit when the enter key is pressed - "blur" - Trigger submit when the editable is blurred - "none" - No action will trigger submit. You need to use the submit button - "both" - Pressing `Enter` and blurring the input will trigger submit |
colorPalette | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'The color palette of the component |
size | 'md' | 'sm' | 'md' | 'lg'The size of the component |
as | React.ElementTypeThe underlying element to render. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | booleanWhether to remove the component's style. | |
autoResize | booleanWhether the editable should auto-resize to fit the content. | |
defaultEdit | booleanWhether the editable is in edit mode by default. | |
defaultValue | stringThe initial value of the editable when rendered. Use when you don't need to control the value of the editable. | |
disabled | booleanWhether the editable is disabled. | |
edit | booleanWhether the editable is in edit mode. | |
finalFocusEl | () => HTMLElement | nullThe element to receive focus when the editable is closed. | |
form | stringThe associate form of the underlying input. | |
id | stringThe unique identifier of the machine. | |
ids | Partial<{
root: string
area: string
label: string
preview: string
input: string
control: string
submitTrigger: string
cancelTrigger: string
editTrigger: string
}>The ids of the elements in the editable. Useful for composition. | |
invalid | booleanWhether the input's value is invalid. | |
maxLength | numberThe maximum number of characters allowed in the editable | |
name | stringThe name attribute of the editable component. Used for form submission. | |
onEditChange | (details: EditChangeDetails) => voidFunction to call when the edit mode changes. | |
onFocusOutside | (event: FocusOutsideEvent) => voidFunction called when the focus is moved outside the component | |
onInteractOutside | (event: InteractOutsideEvent) => voidFunction called when an interaction happens outside the component | |
onPointerDownOutside | (event: PointerDownOutsideEvent) => voidFunction called when the pointer is pressed down outside the component | |
onValueChange | (details: ValueChangeDetails) => voidFunction to call when the value changes. | |
onValueCommit | (details: ValueChangeDetails) => voidFunction to call when the value is committed. | |
onValueRevert | (details: ValueChangeDetails) => voidFunction to call when the value is reverted. | |
placeholder | string | { edit: string; preview: string }The placeholder text for the editable. | |
readOnly | booleanWhether the editable is read-only. | |
required | booleanWhether the editable is required. | |
translations | IntlTranslationsThe translations for the editable. | |
value | stringThe controlled value of the editable. |