-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce RecordContext and Base components for Edit, Create and Show #5422
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! This will ease Edit, Create and Show views customization big time.
title, | ||
version, | ||
...rest | ||
} = props; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CreateView should use the CreateContext to get the controller props. See how I did in ListView
undoable, | ||
version, | ||
...rest | ||
} = props; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should use useEditContext to get controller props
title, | ||
version, | ||
...rest | ||
} = props; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grab the show controller props from useShowContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So many changes. Well done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there!
import { Record } from '../types'; | ||
|
||
/** | ||
* Context to store the result of the useRecord() hook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Context to store the result of the useRecord() hook. | |
* Context to store the current record. |
/** | ||
* Context to store the result of the useRecord() hook. | ||
* | ||
* Use the useRecordContext() hook to read the context. That's what the Edit and Show components do in react-admn. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Use the useRecordContext() hook to read the context. That's what the Edit and Show components do in react-admn. | |
* Use the useRecordContext() hook to read the context. That's what the Edit and Show components do in react-admin. |
* import { useEditController, EditContext } from 'ra-core'; | ||
* | ||
* const Edit = props => { | ||
* const controllerProps = useEditController(props); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad code example. Shouldn't it be:
const { record }= useEditController(props);
return (
<RecordContextProvider value={record}>
...
</RecordContextProvider>
);
??
}; | ||
|
||
/** | ||
* Hook to read the record from a context which provide one, such as the EditContext or ShowContext. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Hook to read the record from a context which provide one, such as the EditContext or ShowContext. | |
* Hook to read the record from a RecordContext. |
>( | ||
context: RecordType | ||
) => { | ||
return pick(context, ['record']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking: every time something changes in a context (e.g. the selected ids in the list), the pick command will return a new instance of the record, re-rendering everything.
I think it's not a premature optimization to memoize the picking.
*/ | ||
export const useRecordContext = < | ||
RecordType extends Record | Omit<Record, 'id'> = Record | ||
>() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you must expect a record to be passed here to allow backwards compatibility
* saving | ||
* } = useSaveContext(); | ||
*/ | ||
export const useSaveContext = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
must accept props as params for BC
export const useSaveContext = () => { | ||
const context = useContext(SaveContext); | ||
|
||
if (!context) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will never happen: the default context is an object
Co-authored-by: Adrien Amoros <adrien.amoros@gmail.com>
I'm so happy to have this merged! |
No description provided.