Skip to content

Commit

Permalink
Serve the id separate from props
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Jan 6, 2025
1 parent b0dd276 commit 94df92f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,47 +426,6 @@ export const ConditionalInfo = () => {
return (
<ComponentBox scope={{ FormError }}>
{() => {
const conditionalInfo = (
maximum: number,
{ renderMode, getValueByPath, getFieldByPath },
) => {
renderMode('interactive')

const amount = getValueByPath('/amount')
const { props } = getFieldByPath('/amount')

if (maximum < amount && props) {
const anchor = (
<Anchor
href="#"
onClick={(event) => {
event.preventDefault()
const el = document.getElementById(props.id + '-label')
el?.scrollIntoView()
}}
>
{props.label}
</Anchor>
)

return (
<>
Remember to adjust the {anchor} to be {maximum} or lower.
</>
)
}
}
const onBlurValidator = (amount: number, { connectWithPath }) => {
const { getValue: getMaximum } = connectWithPath('/maximum')

if (amount > getMaximum()) {
return new FormError('NumberField.errorMaximum', {
messageValues: {
maximum: getMaximum(),
},
})
}
}
return (
<Form.Handler
defaultData={{
Expand All @@ -487,7 +446,37 @@ export const ConditionalInfo = () => {
</>
}
path="/maximum"
info={conditionalInfo}
info={(
maximum: number,
{ renderMode, getValueByPath, getFieldByPath },
) => {
renderMode('interactive')

const amount = getValueByPath('/amount')
const { props, id } = getFieldByPath('/amount')

if (maximum < amount && props) {
const anchor = (
<Anchor
href={'#' + id + '-label'}
onClick={(event) => {
event.preventDefault()
const el = document.getElementById(id + '-label')
el?.scrollIntoView()
}}
>
{props.label}
</Anchor>
)

return (
<>
Remember to adjust the {anchor} to be {maximum} or
lower.
</>
)
}
}}
/>
<Field.Number
label="Amount"
Expand All @@ -498,7 +487,18 @@ export const ConditionalInfo = () => {
</>
}
path="/amount"
onBlurValidator={onBlurValidator}
onBlurValidator={(amount: number, { connectWithPath }) => {
const { getValue: getMaximum } =
connectWithPath('/maximum')

if (amount > getMaximum()) {
return new FormError('NumberField.errorMaximum', {
messageValues: {
maximum: String(getMaximum()),
},
})
}
}}
/>
</Form.Card>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ export const ConditionalInfo = () => {
maximum: number,
{ renderMode, getValueByPath, getFieldByPath }
) => {
renderMode('interactive') // Can also be 'initially' or 'continuously'
renderMode('initially') // Can also be 'initially' or 'continuously'

const amount = getValueByPath('/amount')
const { props } = getFieldByPath('/amount')
const { props, id } = getFieldByPath('/amount')

if (maximum < amount && props) {
const anchor = (
<Anchor
href={`#${props?.id}-label`}
href={`#${id}-label`}
onClick={(event: React.MouseEvent<HTMLAnchorElement>) => {
event.preventDefault()
const el = document.getElementById(`${props.id}-label`)
const el = document.getElementById(`${id}-label`)
el?.scrollIntoView()
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ export default function useFieldProps<Value, EmptyValue, Props>(
useCallback(
(path) => {
const props = fieldPropsRef.current?.[path]
return { props }
return { props, id }
},
[fieldPropsRef]
[fieldPropsRef, id]
)

const messageCacheRef = useRef<{
Expand Down
1 change: 1 addition & 0 deletions packages/dnb-eufemia/src/extensions/forms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export type MessagePropParams<Value> = {
getValueByPath: GetValueByPath<Value>
getFieldByPath: (path: Path) => {
props: FieldProps
id: Identifier
}
}
export type MessageProp<Value, T> =
Expand Down

0 comments on commit 94df92f

Please sign in to comment.