Skip to content

Commit

Permalink
feat: example community-specific field
Browse files Browse the repository at this point in the history
  • Loading branch information
phette23 committed Oct 18, 2024
1 parent b1c8c4f commit 4320cef
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions assets/templates/custom_fields/CommunityField.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
// field is only visible when record is submitted to a particular community
import React, {useState, useEffect} from "react"
import React, {Component} from "react"
import {TextField} from "react-invenio-forms"

// I believe this needs to be a class component and then use something in the props
// passed to the constructor to determine what community it's in.
// ! Neither the values from useFormikContext nor the record from the props tell you what community the record is in.
// ! We can only look for ?community=id in the URL which is only present when the record is submitted to a particular community.
export class CommunityField extends Component {
constructor(props) {
super(props)
this.state = {
community: this.props.community || this.props.record.community || "",
}
}
render() {
const {fieldPath} = this.props
const community = new URLSearchParams(window.location.search).get("community")

render() {this.state.community == "community-id" &&
<TextField
fieldPath={fieldPath}
helpText="This field is only visible when record is submitted to a particular community."
label="Community-specific Field"
/>
if (community === 'test') {
return <TextField
fieldPath={fieldPath}
helpText="This field is only visible when record is submitted to the Test Community."
label="Community-specific Field"
/>
}
return null
}
}

Expand Down

0 comments on commit 4320cef

Please sign in to comment.