@@ -14,25 +14,39 @@ import { Box, IconButton, type SxProps } from '@mui/material';
1414import SaveIcon from '@mui/icons-material/Save' ;
1515import CloseIcon from '@mui/icons-material/Close' ;
1616
17- import {
18- InPlaceEditorContext ,
19- type InPlaceEditorValue ,
20- type InPlaceEditorAction ,
21- } from './InPlaceEditorContext' ;
2217import { TextInput } from '../TextInput' ;
2318import { TextField } from '../../field' ;
2419
20+ export type InPlaceEditorAction =
21+ | { type : 'edit' }
22+ | { type : 'save' ; values : any }
23+ | { type : 'cancel' }
24+ | { type : 'success' }
25+ | { type : 'error' ; error : any } ;
26+
27+ export type InPlaceEditorValue =
28+ | { state : 'editing' }
29+ | { state : 'saving' ; values : any }
30+ | { state : 'reading' } ;
31+
2532export interface InPlaceEditorProps {
2633 source ?: string ;
2734 mutationMode ?: 'optimistic' | 'pessimistic' | 'undoable' ;
2835 cancelOnBlur ?: boolean ;
2936 notifyOnSuccess ?: boolean ;
37+ resource ?: string ;
3038 showButtons ?: boolean ;
3139 children ?: React . ReactNode ;
3240 editor ?: React . ReactNode ;
3341 sx ?: SxProps ;
3442}
3543
44+ /**
45+ * Renders a value, and on click it turns into an editable field.
46+ *
47+ * The editable field is rendered inside a Form component, so InPlaceEditor
48+ * cannot be used inside another Form component.
49+ */
3650export const InPlaceEditor = ( props : InPlaceEditorProps ) => {
3751 const {
3852 source,
@@ -97,10 +111,10 @@ export const InPlaceEditor = (props: InPlaceEditorProps) => {
97111 ) ;
98112
99113 const record = useRecordContext ( ) ;
100- const resource = useResourceContext ( ) ;
114+ const resource = useResourceContext ( props ) ;
101115 const notify = useNotify ( ) ;
102- const [ update ] = useUpdate ( ) ;
103116 const translate = useTranslate ( ) ;
117+ const [ update ] = useUpdate ( ) ;
104118
105119 const handleSave = async values => {
106120 if ( ! record ) {
@@ -165,9 +179,9 @@ export const InPlaceEditor = (props: InPlaceEditorProps) => {
165179 }
166180 } ;
167181
168- return (
169- < InPlaceEditorContext . Provider value = { { state , dispatch } } >
170- { state . state === 'reading' ? (
182+ switch ( state . state ) {
183+ case 'reading' :
184+ return (
171185 < Box
172186 onClick = { handleEdit }
173187 sx = { {
@@ -179,7 +193,9 @@ export const InPlaceEditor = (props: InPlaceEditorProps) => {
179193 >
180194 { children }
181195 </ Box >
182- ) : state . state === 'editing' ? (
196+ ) ;
197+ case 'editing' :
198+ return (
183199 < Form onSubmit = { handleSave } >
184200 < Box
185201 onKeyDown = { event => {
@@ -222,13 +238,14 @@ export const InPlaceEditor = (props: InPlaceEditorProps) => {
222238 ) }
223239 </ Box >
224240 </ Form >
225- ) : state . state === 'saving' ? (
241+ ) ;
242+ case 'saving' :
243+ return (
226244 < RecordContextProvider value = { state . values } >
227245 < Box sx = { { opacity : 0.5 } } > { children } </ Box >
228246 </ RecordContextProvider >
229- ) : (
230- ''
231- ) }
232- </ InPlaceEditorContext . Provider >
233- ) ;
247+ ) ;
248+ default :
249+ throw new Error ( 'Unhandled state' ) ;
250+ }
234251} ;
0 commit comments