Skip to main content
Back to React

useConsentDetailField()

The useConsentDetailField hook gives the state and handlers of one field of an RFC 9396 authorization detail on the consent step, for drawing the field in a custom UI. <ConsentDetailField /> is built on it and applies the same bounds and validation.

For a number or date field, the hook keeps the text the user typed. It writes the text to the consent decision only while the value stays within the field's bound. Otherwise it clears the field's change and marks the field invalid, which blocks approval through isApprovalBlocked. For a list field, it exposes the items and toggles them as the field's rule allows.

Usage​

src/DetailField.tsx
tsx
import { type ConsentDetailFieldProps, useConsentDetailField } from '@thunderid/react'
import { type ReactElement } from 'react'

export default function DetailField({
purposeId,
detailId,
row,
formValues,
onInputChange,
}: ConsentDetailFieldProps): ReactElement {
const field = useConsentDetailField({ purposeId, detailId, row, formValues, onInputChange })

if (field.kind === 'removeItems' || field.kind === 'addItems') {
return (
<fieldset>
<legend>{field.label}</legend>
{field.items.map((item) => (
<button
key={String(item.value)}
type="button"
aria-pressed={item.selected}
disabled={!item.toggleable}
onClick={() => field.handleToggle(item.value)}
>
{String(item.value)}
</button>
))}
{field.isTypedItemAllowed && (
<input
aria-label={`Add to ${field.label}`}
value={field.text}
onChange={(event) => field.handleChange(event.target.value)}
onKeyDown={(event) => {
if (event.key === 'Enter') {
event.preventDefault()
field.handleAdd(event.currentTarget.value)
}
}}
/>
)}
</fieldset>
)
}

if (field.kind !== 'number' && field.kind !== 'date') {
return <p>{`${field.label}: ${JSON.stringify(row.value)}`}</p>
}

return (
<div>
<label htmlFor={field.inputId}>{field.label}</label>
<input
id={field.inputId}
type={field.kind}
step={field.kind === 'number' ? 'any' : undefined}
value={field.text}
max={field.max}
min={field.min}
onChange={(event) => field.handleChange(event.target.value)}
onBlur={field.handleBlur}
/>
{field.error && <p role="alert">{field.error.message}</p>}
</div>
)
}

Render it for each of a detail's rows from the render props of <ConsentAuthorizationDetail />.

Parameters​

ParameterTypeRequiredDescription
purposeIdstring✅The purposeId of the authorization details purpose.
detailIdstring✅The name of the detail's element.
rowConsentDetailRow✅The field. See ConsentDetailRow.
formValuesRecord<string, string>✅The consent form values.
onInputChange(name: string, value: string) => void✅Called with a form key and its new value.
tUseTranslation['t']❌Translation function for the error message. The SDK's default bundle is used without one.

Return Value​

UseConsentDetailFieldResult:

PropertyTypeDescription
kind'number' | 'date' | 'removeItems' | 'addItems' | undefinedHow the user may change the field. undefined for a read-only field.
labelstringThe field's label, or its path when it has none.
inputIdstringAn identifier for the field's input element.
textstringThe text to show in a number or date input: what the user typed, the value they chose, or the value shown. For an addItems list that takes typed items, the item being typed.
maxstring | undefinedUpper bound of a field that may decrease: the value shown.
minstring | undefinedLower bound of a field that may increase: the value shown.
isValidbooleanWhether the text may be submitted. Always true for a list or a read-only field.
errorConsentDetailFieldError | undefinedWhy the text cannot be submitted, with the translated message, its translation key, and params.
handleChange(text: string) => voidCall with the text the user entered.
handleBlur() => voidCall when the input loses focus. An empty input returns to the value shown.
itemsConsentDetailListItem[]The items of a list field, each with value, selected, toggleable, and origin ('shown', 'added', or 'offered'). Empty for any other field.
handleToggle(item: string | number | boolean) => voidToggles an item of a list field, within what its rule allows.
isSelected(item: string | number | boolean) => booleanWhether an item is part of the value that would be granted.
isToggleable(item: string | number | boolean) => booleanWhether the user may toggle an item.
isTypedItemAllowedbooleanWhether an addItems list takes typed items, which holds when its type lists no allowed values.
handleAdd(text: string) => booleanAdds a typed item to such a list. Returns whether it was added.
isPlainListbooleanWhether the value is a list of plain values.
listDisplayConsentDetailListDisplayHow many items of a long list to show, and whether the list can collapse.
isListExpandedbooleanWhether the user expanded a long list.
handleListToggle(expanded: boolean) => voidExpands or collapses a long list.

Explore with AI

ThunderID LogoThunderID Logo

Product

DocsAPIsSDKs
© Copyright Linux Foundation Europe.For web site terms of use, trademark policy and other project policies please see https://linuxfoundation.eu/en/policies.