Skip to content
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: Ensure 'Indicates a required field' text remains visible - 1996 #2119

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions frontend/src/components/BCDataGrid/BCGridEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { faPlus, faCaretDown } from '@fortawesome/free-solid-svg-icons'
import BCModal from '@/components/BCModal'
import { useTranslation } from 'react-i18next'
import { BCAlert2 } from '@/components/BCAlert'
import { RequiredHeader } from '@/components/BCDataGrid/components'

/**
* @typedef {import('ag-grid-community').GridOptions} GridOptions
Expand Down Expand Up @@ -54,8 +55,35 @@ export const BCGridEditor = ({
const [anchorEl, setAnchorEl] = useState(null)
const buttonRef = useRef(null)
const { t } = useTranslation(['common'])
const [showRequiredIndicator, setShowRequiredIndicator] = useState(false)

useEffect(() => {
if (!showRequiredIndicator && props.columnDefs?.length) {
const foundRequired = props.columnDefs.some(
(colDef) => colDef.headerComponent === RequiredHeader
)
if (foundRequired) {
setShowRequiredIndicator(true)
}
}
}, [props.columnDefs, showRequiredIndicator])

const handleGridReady = useCallback(
(params) => {
if (!showRequiredIndicator) {
const actualCols = params.api.getColumnDefs() || []
const foundRequired = actualCols.some(
(colDef) => colDef.headerComponent === RequiredHeader
)
if (foundRequired) {
setShowRequiredIndicator(true)
}
}
props.onGridReady?.(params)
},
[showRequiredIndicator, props.onGridReady]
)

// Helper function to find and cache first editable column
const findFirstEditableColumn = useCallback(() => {
if (!ref.current?.api) return null

Expand Down Expand Up @@ -215,7 +243,7 @@ export const BCGridEditor = ({
async (numRows) => {
alertRef.current.clearAlert()
let newRows = []

if (onAction) {
try {
for (let i = 0; i < numRows; i++) {
Expand Down Expand Up @@ -272,21 +300,12 @@ export const BCGridEditor = ({
return
}

setShowCloseModal(true)
}
const hasRequiredHeaderComponent = useCallback(() => {
const columnDefs = ref.current?.api?.getColumnDefs() || []
// Check if any column has `headerComponent` matching "RequiredHeader"
return (
columnDefs.some(
(colDef) => colDef.headerComponent?.name === 'RequiredHeader'
) || columnDefs.some((colDef) => !!colDef.headerComponent)
)
}, [ref])
setShowCloseModal(true)
}

return (
<BCBox my={2} component="div" style={{ height: '100%', width: '100%' }}>
{hasRequiredHeaderComponent() && (
{showRequiredIndicator && (
<BCTypography
variant="body4"
color="text"
Expand All @@ -297,6 +316,7 @@ export const BCGridEditor = ({
<BCGridBase
ref={ref}
className="ag-theme-quartz"
onGridReady={handleGridReady}
onCellValueChanged={handleOnCellValueChanged}
undoRedoCellEditing
undoRedoCellEditingLimit={5}
Expand Down Expand Up @@ -408,5 +428,6 @@ BCGridEditor.propTypes = {
onSave: PropTypes.func,
confirmText: PropTypes.string,
confirmLabel: PropTypes.string
})
}),
onGridReady: PropTypes.func
}