UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

28 lines 1.41 kB
import get from 'lodash/get'; import { useRecordContext } from '../controller'; /** * A hook that gets the value of a field of the current record. * @param params The hook parameters * @param params.source The field source * @param params.record The record to use. Uses the record from the RecordContext if not provided * @param params.defaultValue The value to return when the field value is empty * @returns The field value * * @example * const MyField = (props: { source: string }) => { * const value = useFieldValue(props); * return <span>{value}</span>; * } */ export var useFieldValue = function (params) { var defaultValue = params.defaultValue, source = params.source; // We use the record from the RecordContext and do not rely on the SourceContext on purpose to // avoid having the wrong source targeting the record. // Indeed, some components may create a sub record context (SimpleFormIterator, TranslatableInputs, etc.). In this case, // it they used the SourceContext as well, they would have the wrong source. // Inputs needs the SourceContext as they rely on the Form value and you can't have nested forms. // Fields needs the RecordContext as they rely on the Record value and you can have nested RecordContext. var record = useRecordContext(params); return get(record, source, defaultValue); }; //# sourceMappingURL=useFieldValue.js.map