Skip to content

Commit 1c44b61

Browse files
committed
Simplify implementation
1 parent 15dd083 commit 1c44b61

File tree

3 files changed

+35
-40
lines changed

3 files changed

+35
-40
lines changed

packages/ra-ui-materialui/src/input/InPlaceEditor/InPlaceEditor.Card.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import englishMessages from 'ra-language-english';
3232

3333
import { InPlaceEditor } from './InPlaceEditor';
3434
import { SelectInput } from '../SelectInput';
35-
import { ChipField, SelectField, TextField } from '../../field';
35+
import { ChipField, SelectField } from '../../field';
3636
import { Notification } from '../../layout';
3737

3838
export default {

packages/ra-ui-materialui/src/input/InPlaceEditor/InPlaceEditor.tsx

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,39 @@ import { Box, IconButton, type SxProps } from '@mui/material';
1414
import SaveIcon from '@mui/icons-material/Save';
1515
import CloseIcon from '@mui/icons-material/Close';
1616

17-
import {
18-
InPlaceEditorContext,
19-
type InPlaceEditorValue,
20-
type InPlaceEditorAction,
21-
} from './InPlaceEditorContext';
2217
import { TextInput } from '../TextInput';
2318
import { 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+
2532
export 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+
*/
3650
export 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
};

packages/ra-ui-materialui/src/input/InPlaceEditor/InPlaceEditorContext.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)