/
editable.tsx
234 lines (203 loc) · 5.17 KB
/
editable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import {
chakra,
forwardRef,
omitThemingProps,
StylesProvider,
SystemStyleObject,
ThemingProps,
useMultiStyleConfig,
useStyles,
HTMLChakraProps,
} from "@chakra-ui/system"
import { cx, runIfFn, __DEV__ } from "@chakra-ui/utils"
import { createContext, MaybeRenderProp } from "@chakra-ui/react-utils"
import * as React from "react"
import {
useEditable,
UseEditableProps,
UseEditableReturn,
} from "./use-editable"
type EditableContext = Omit<UseEditableReturn, "htmlProps">
const [EditableProvider, useEditableContext] = createContext<EditableContext>({
name: "EditableContext",
errorMessage:
"useEditableContext: context is undefined. Seems you forgot to wrap the editable components in `<Editable />`",
})
type RenderProps = Pick<
UseEditableReturn,
"isEditing" | "onSubmit" | "onCancel" | "onEdit"
>
interface BaseEditableProps
extends Omit<
HTMLChakraProps<"div">,
"onChange" | "value" | "defaultValue" | "onSubmit"
> {}
export interface EditableProps
extends UseEditableProps,
BaseEditableProps,
ThemingProps<"Editable"> {
children?: MaybeRenderProp<RenderProps>
}
/**
* Editable
*
* The wrapper that provides context and logic for all editable
* components. It renders a `div`
*/
export const Editable = forwardRef<EditableProps, "div">((props, ref) => {
const styles = useMultiStyleConfig("Editable", props)
const ownProps = omitThemingProps(props)
const { htmlProps, ...context } = useEditable(ownProps)
const { isEditing, onSubmit, onCancel, onEdit } = context
const _className = cx("chakra-editable", props.className)
const children = runIfFn(props.children, {
isEditing,
onSubmit,
onCancel,
onEdit,
})
return (
<EditableProvider value={context}>
<StylesProvider value={styles}>
<chakra.div
ref={ref}
{...(htmlProps as HTMLChakraProps<"div">)}
className={_className}
>
{children}
</chakra.div>
</StylesProvider>
</EditableProvider>
)
})
if (__DEV__) {
Editable.displayName = "Editable"
}
const commonStyles: SystemStyleObject = {
fontSize: "inherit",
fontWeight: "inherit",
textAlign: "inherit",
bg: "transparent",
}
export interface EditablePreviewProps extends HTMLChakraProps<"div"> {}
/**
* EditablePreview
*
* The `span` used to display the final value, in the `preview` mode
*/
export const EditablePreview = forwardRef<EditablePreviewProps, "span">(
(props, ref) => {
const { getPreviewProps } = useEditableContext()
const styles = useStyles()
const previewProps = getPreviewProps(props, ref) as HTMLChakraProps<"span">
const _className = cx("chakra-editable__preview", props.className)
return (
<chakra.span
{...previewProps}
__css={{
cursor: "text",
display: "inline-block",
...commonStyles,
...styles.preview,
}}
className={_className}
/>
)
},
)
if (__DEV__) {
EditablePreview.displayName = "EditablePreview"
}
export interface EditableInputProps extends HTMLChakraProps<"input"> {}
/**
* EditableInput
*
* The input used in the `edit` mode
*/
export const EditableInput = forwardRef<EditableInputProps, "input">(
(props, ref) => {
const { getInputProps } = useEditableContext()
const styles = useStyles()
const inputProps = getInputProps(props, ref)
const _className = cx("chakra-editable__input", props.className)
return (
<chakra.input
{...inputProps}
__css={{
outline: 0,
...commonStyles,
...styles.input,
}}
className={_className}
/>
)
},
)
if (__DEV__) {
EditableInput.displayName = "EditableInput"
}
export interface EditableTextareaProps extends HTMLChakraProps<"textarea"> {}
/**
* EditableTextarea
*
* The textarea used in the `edit` mode
*/
export const EditableTextarea = forwardRef<EditableTextareaProps, "textarea">(
(props, ref) => {
const { getTextareaProps } = useEditableContext()
const styles = useStyles()
const textareaProps = getTextareaProps(props, ref)
const _className = cx("chakra-editable__textarea", props.className)
return (
<chakra.textarea
{...textareaProps}
__css={{
outline: 0,
...commonStyles,
...styles.textarea,
}}
className={_className}
/>
)
},
)
if (__DEV__) {
EditableTextarea.displayName = "EditableTextarea"
}
/**
* React hook use to gain access to the editable state and actions.
*/
export function useEditableState() {
const { isEditing, onSubmit, onCancel, onEdit, isDisabled } =
useEditableContext()
return {
isEditing,
onSubmit,
onCancel,
onEdit,
isDisabled,
}
}
/**
* React hook use to create controls for the editable component
*/
export function useEditableControls(): Pick<
EditableContext,
| "isEditing"
| "getEditButtonProps"
| "getCancelButtonProps"
| "getSubmitButtonProps"
> {
const {
isEditing,
getEditButtonProps,
getCancelButtonProps,
getSubmitButtonProps,
} = useEditableContext()
return {
isEditing,
getEditButtonProps,
getCancelButtonProps,
getSubmitButtonProps,
}
}