-
Notifications
You must be signed in to change notification settings - Fork 180
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
fix(crud): expandable readonly document COMPASS-4635 #6447
Changes from all commits
f00ba9c
4563d33
82a5c87
5278c0c
c77bd3e
1e4f0e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import type Document from 'hadron-document'; | |
import type { TypeCastMap } from 'hadron-type-checker'; | ||
import { withPreferences } from 'compass-preferences-model/provider'; | ||
import { getInsightsForDocument } from '../utils'; | ||
import { DocumentEvents } from 'hadron-document'; | ||
type BSONObject = TypeCastMap['Object']; | ||
|
||
export const documentStyles = css({ | ||
|
@@ -30,10 +31,73 @@ export type ReadonlyDocumentProps = { | |
showInsights?: boolean; | ||
}; | ||
|
||
type ReadonlyDocumentState = { | ||
expanded: boolean; | ||
}; | ||
|
||
/** | ||
* Component for a single readonly document in a list of documents. | ||
*/ | ||
class ReadonlyDocument extends React.Component<ReadonlyDocumentProps> { | ||
class ReadonlyDocument extends React.Component< | ||
ReadonlyDocumentProps, | ||
ReadonlyDocumentState | ||
> { | ||
constructor(props: ReadonlyDocumentProps) { | ||
super(props); | ||
this.state = { | ||
expanded: props.doc.expanded, | ||
}; | ||
} | ||
|
||
/** | ||
* Subscribe to the update store on mount. | ||
*/ | ||
componentDidMount() { | ||
this.subscribeToDocumentEvents(this.props.doc); | ||
} | ||
|
||
/** | ||
* Refreshing the list updates the doc in the props so we should update the | ||
* document on the instance. | ||
*/ | ||
componentDidUpdate(prevProps: ReadonlyDocumentProps) { | ||
if (prevProps.doc !== this.props.doc) { | ||
this.unsubscribeFromDocumentEvents(prevProps.doc); | ||
this.subscribeToDocumentEvents(this.props.doc); | ||
} | ||
} | ||
|
||
/** | ||
* Unsubscribe from the update store on unmount. | ||
*/ | ||
componentWillUnmount() { | ||
this.unsubscribeFromDocumentEvents(this.props.doc); | ||
} | ||
|
||
/** | ||
* Subscribe to the document events. | ||
*/ | ||
subscribeToDocumentEvents(doc: Document) { | ||
doc.on(DocumentEvents.Expanded, this.handleExpanded); | ||
doc.on(DocumentEvents.Collapsed, this.handleCollapsed); | ||
} | ||
|
||
/** | ||
* Unsubscribe from the document events. | ||
*/ | ||
unsubscribeFromDocumentEvents(doc: Document) { | ||
doc.on(DocumentEvents.Expanded, this.handleExpanded); | ||
doc.on(DocumentEvents.Collapsed, this.handleCollapsed); | ||
} | ||
|
||
handleExpanded = () => { | ||
this.setState({ expanded: true }); | ||
}; | ||
|
||
handleCollapsed = () => { | ||
this.setState({ expanded: false }); | ||
}; | ||
|
||
handleClone = () => { | ||
const clonedDoc = this.props.doc.generateObject({ | ||
excludeInternalFields: true, | ||
|
@@ -48,13 +112,32 @@ class ReadonlyDocument extends React.Component<ReadonlyDocumentProps> { | |
this.props.copyToClipboard?.(this.props.doc); | ||
}; | ||
|
||
/** | ||
* Handle clicking the expand all button. | ||
*/ | ||
handleExpandAll = () => { | ||
const { doc } = this.props; | ||
// Update the doc directly - the components internal state will update via events | ||
if (doc.expanded) { | ||
doc.collapse(); | ||
} else { | ||
doc.expand(); | ||
} | ||
}; | ||
|
||
/** | ||
* Get the elements for the document. | ||
* | ||
* @returns {Array} The elements. | ||
*/ | ||
renderElements() { | ||
return <DocumentList.Document value={this.props.doc} />; | ||
return ( | ||
<DocumentList.Document | ||
value={this.props.doc} | ||
// Provide extra whitespace for the expand button | ||
extraGutterWidth={spacing[900]} | ||
Comment on lines
+137
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want this to be very flexible in the component interface we probably should note that this value should match exactly the space left by hidden editing action buttons in the editable card, so that in case the editable card gets updated we can find this and update the value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there a requirement that this offset match any other state as such. Just that it's enough for the expand button to not overlay the icons of the individual elements. |
||
/> | ||
); | ||
} | ||
|
||
renderActions() { | ||
|
@@ -64,6 +147,8 @@ class ReadonlyDocument extends React.Component<ReadonlyDocumentProps> { | |
onClone={ | ||
this.props.openInsertDocumentDialog ? this.handleClone : undefined | ||
} | ||
onExpand={this.handleExpandAll} | ||
expanded={this.state.expanded} | ||
insights={ | ||
this.props.showInsights | ||
? getInsightsForDocument(this.props.doc) | ||
|
@@ -94,7 +179,6 @@ class ReadonlyDocument extends React.Component<ReadonlyDocumentProps> { | |
static propTypes = { | ||
copyToClipboard: PropTypes.func, | ||
doc: PropTypes.object.isRequired, | ||
expandAll: PropTypes.bool, | ||
openInsertDocumentDialog: PropTypes.func, | ||
showInsights: PropTypes.bool, | ||
}; | ||
|
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.
Since this takes the
editable
as an argument and displaying the spacer isn't conditional on the value ofeditable
I've moved it out of theOFFSET_WHEN_EDITABLE
(noweditableOffset
).