Import
import { useValueProps } from '@dnb/eufemia/extensions/forms'// Use useValueProps
Description
The useValueProps hook standardize handling of the value flow for a single consumer component representing one data point.
Relevant links
It also provides a way to transform the value.
This hook works perfectly together with ValueBlock.
How to use
import { useValueProps } from '@dnb/eufemia/extensions/forms'const MyValueComponent = (props) => {const { value, ...rest } = useValueProps(props)return <ValueBlock {...rest}>{value}</ValueBlock>}render(<MyValueComponent path="/dataSelector" />)
Internal Properties
All properties are optional and can be used as needed. These properties can be provided as part of your component properties.
valuethe input value (string).emptyValuedefines what value is considered to be empty. Defaults toundefined. But an empty string will also be validated when required is true.paththe JSON pointer that defines the entry name/key in the data structure.itemPathsimilar topath, but is used when run inside the Iterate context.
Return Parameters
It returns all of the given component properties, in addition to these:
valuethe output value.
Value transformers
The transformers are hooks to transform the value on different stages.
They should return a transformed value: (value) => value
-
toInputtransforms the value before it is returned. This applies whether the original source of the value is the value property or the data context. -
fromExternaltransforms the providedvalueproperty before any other operations are performed.
In addition there are value transformers which should be used outside of the value component (by the value consumer):
transformIntransforms thevaluebefore it's displayed in the value component.
Demos
const MyValueComponent = (props) => { const preparedProps = { label: 'Default Label', ...props, toInput: (value) => value + 10, } const { value, ...rest } = useValueProps(preparedProps) return <ValueBlock {...rest}>{formatCurrency(value)} kroner</ValueBlock> } render( <Form.Handler data={{ myValue: 10, }} > <MyValueComponent label="Amount" path="/myValue" transformIn={(value) => value * 2} /> </Form.Handler> )
Parameters
Properties passed to the useValueProps hook.
| Type | Description | |
|---|---|---|
value | string | (optional) Direct value to display. Takes precedence over path-based data from DataContext. |
defaultValue | string | (optional) Default value when no value is available from DataContext or value prop. |
path | string | (optional) JSON Pointer to the data location in DataContext. Links this Value component to its data source. |
itemPath | string | (optional) Path relative to the current Iterate element. Used when inside an Iterate context. |
label | React.ReactNode | (optional) Label to display with the value. Can be inherited from the associated Field component. |
inheritLabel | boolean | (optional) When true, inherits the label from the Field component at the same path. |
inheritVisibility | boolean | (optional) When true, inherits visibility state from the Field component at the same path. |
transformIn | (external: Value) => Value | (optional) Transforms the external value before processing. Receives the raw data value. |
toInput | (value: Value) => Value | (optional) Transforms the internal value for display (after transformIn). |
fromExternal | (value: Value) => Value | (optional) Transforms value when reading from external data sources. |
Return Values
Properties returned from the useValueProps hook (in addition to all input props).
| Type | Description | |
|---|---|---|
...props | object | (required) All input props are passed through in the return value. |
value | string undefined | (optional) The transformed value ready for display, or undefined if the associated field is not visible. |
label | React.ReactNode | (optional) The label to display, either from props or inherited from the associated Field. |