UNPKG

ra-core

Version:

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

68 lines 3.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRecordFromLocation = exports.useRecordFromLocation = void 0; const react_1 = require("react"); const query_string_1 = require("query-string"); const isEqual_js_1 = __importDefault(require("lodash/isEqual.js")); const routing_1 = require("../routing/index.cjs"); /** * A hook that returns the record to use to override the values in a form * @param options The hook options * @param options.searchSource The key in the location search to use as a source for the record. Its content should be a stringified JSON object. * @param options.stateSource The key in the location state to use as a source for the record * @returns The record to use to override the values in a form */ const useRecordFromLocation = (props = {}) => { const { searchSource, stateSource } = props; const location = (0, routing_1.useLocation)(); const [recordFromLocation, setRecordFromLocation] = (0, react_1.useState)(() => (0, exports.getRecordFromLocation)(location, { stateSource, searchSource, })); // To avoid having the form resets when the location changes but the final record is the same // This is needed for forms such as TabbedForm or WizardForm that may change the location for their sections const previousRecordRef = (0, react_1.useRef)(recordFromLocation); (0, react_1.useEffect)(() => { const newRecordFromLocation = (0, exports.getRecordFromLocation)(location, { stateSource, searchSource, }); if (!(0, isEqual_js_1.default)(newRecordFromLocation, previousRecordRef.current)) { previousRecordRef.current = newRecordFromLocation; setRecordFromLocation(newRecordFromLocation); } }, [location, stateSource, searchSource]); return recordFromLocation; }; exports.useRecordFromLocation = useRecordFromLocation; /** * Get the initial record from the location, whether it comes from the location * state or is serialized in the url search part. */ const getRecordFromLocation = ({ state, search }, { searchSource = 'source', stateSource = 'record', } = {}) => { if (state && state[stateSource]) { return state[stateSource]; } if (search) { try { const searchParams = (0, query_string_1.parse)(search); const source = searchParams[searchSource]; if (source) { if (Array.isArray(source)) { console.error(`Failed to parse location ${searchSource} parameter '${search}'. To pre-fill some fields in the Create form, pass a stringified ${searchSource} parameter (e.g. '?${searchSource}={"title":"foo"}')`); return null; } return JSON.parse(source); } } catch (e) { console.error(`Failed to parse location ${searchSource} parameter '${search}'. To pre-fill some fields in the Create form, pass a stringified ${searchSource} parameter (e.g. '?${searchSource}={"title":"foo"}')`); } } return null; }; exports.getRecordFromLocation = getRecordFromLocation; //# sourceMappingURL=useRecordFromLocation.js.map