1 line
8.2 KiB
Plaintext
1 line
8.2 KiB
Plaintext
{"version":3,"file":"react-final-form-arrays.umd.min.js","sources":["../src/defaultIsEqual.js","../src/useConstant.js","../src/useFieldArray.js","../src/FieldArray.js","../src/renderComponent.js"],"sourcesContent":["// @flow\nconst defaultIsEqual = (aArray: any[], bArray: any[]) =>\n aArray === bArray ||\n (Array.isArray(aArray) &&\n Array.isArray(bArray) &&\n aArray.length === bArray.length &&\n !aArray.some((a, index) => a !== bArray[index]))\n\nexport default defaultIsEqual\n","// @flow\nimport React from 'react'\n\n/**\n * A simple hook to create a constant value that lives for\n * the lifetime of the component.\n *\n * Plagiarized from https://github.com/Andarist/use-constant\n *\n * Do NOT reuse this code unless you know what you're doing.\n * Use Andarist's hook; it's more fault tolerant to things like\n * falsy values.\n *\n * @param {Function} init - A function to generate the value\n */\nexport default function useConstant<T>(init: () => T): T {\n const ref = React.useRef<?T>()\n if (!ref.current) {\n ref.current = init()\n }\n return ref.current\n}\n","// @flow\nimport { useForm, useField } from 'react-final-form'\nimport { fieldSubscriptionItems, ARRAY_ERROR } from 'final-form'\nimport type { Mutators } from 'final-form-arrays'\nimport type { FieldValidator, FieldSubscription } from 'final-form'\nimport type { FieldArrayRenderProps, UseFieldArrayConfig } from './types'\nimport defaultIsEqual from './defaultIsEqual'\nimport useConstant from './useConstant'\n\nconst all: FieldSubscription = fieldSubscriptionItems.reduce((result, key) => {\n result[key] = true\n return result\n}, {})\n\nconst useFieldArray = (\n name: string,\n {\n subscription = all,\n defaultValue,\n initialValue,\n isEqual = defaultIsEqual,\n validate: validateProp\n }: UseFieldArrayConfig = {}\n): FieldArrayRenderProps => {\n const form = useForm('useFieldArray')\n\n const formMutators: Mutators = form.mutators\n const hasMutators = !!(formMutators && formMutators.push && formMutators.pop)\n if (!hasMutators) {\n throw new Error(\n 'Array mutators not found. You need to provide the mutators from final-form-arrays to your form'\n )\n }\n const mutators = useConstant<Mutators>(() =>\n // curry the field name onto all mutator calls\n Object.keys(formMutators).reduce((result, key) => {\n result[key] = (...args) => formMutators[key](name, ...args)\n return result\n }, {})\n )\n\n const validate: FieldValidator = useConstant(\n () => (value, allValues, meta) => {\n if (!validateProp) return undefined\n const error = validateProp(value, allValues, meta)\n if (!error || Array.isArray(error)) {\n return error\n } else {\n const arrayError = []\n // gross, but we have to set a string key on the array\n ;((arrayError: any): Object)[ARRAY_ERROR] = error\n return arrayError\n }\n }\n )\n\n const {\n meta: { length, ...meta },\n input,\n ...fieldState\n } = useField(name, {\n subscription: { ...subscription, length: true },\n defaultValue,\n initialValue,\n isEqual,\n validate,\n format: v => v\n })\n\n const forEach = (iterator: (name: string, index: number) => void): void => {\n // required || for Flow, but results in uncovered line in Jest/Istanbul\n // istanbul ignore next\n const len = length || 0\n for (let i = 0; i < len; i++) {\n iterator(`${name}[${i}]`, i)\n }\n }\n\n const map = (iterator: (name: string, index: number) => any): any[] => {\n // required || for Flow, but results in uncovered line in Jest/Istanbul\n // istanbul ignore next\n const len = length || 0\n const results: any[] = []\n for (let i = 0; i < len; i++) {\n results.push(iterator(`${name}[${i}]`, i))\n }\n return results\n }\n\n return {\n fields: {\n name,\n forEach,\n length: length || 0,\n map,\n ...mutators,\n ...fieldState,\n value: input.value\n },\n meta\n }\n}\n\nexport default useFieldArray\n","// @flow\nimport { version as ffVersion } from 'final-form'\nimport { version as rffVersion } from 'react-final-form'\nimport type { FieldArrayProps } from './types'\nimport renderComponent from './renderComponent'\nimport useFieldArray from './useFieldArray'\nimport { version } from '../package.json'\n\nexport { version }\n\nconst versions = {\n 'final-form': ffVersion,\n 'react-final-form': rffVersion,\n 'react-final-form-arrays': version\n}\n\nconst FieldArray = ({\n name,\n subscription,\n defaultValue,\n initialValue,\n isEqual,\n validate,\n ...rest\n}: FieldArrayProps) => {\n const { fields, meta } = useFieldArray(name, {\n subscription,\n defaultValue,\n initialValue,\n isEqual,\n validate\n })\n\n return renderComponent(\n {\n fields,\n meta: {\n ...meta,\n __versions: versions\n },\n ...rest\n },\n `FieldArray(${name})`\n )\n}\n\nexport default FieldArray\n","// @flow\nimport * as React from 'react'\nimport type { RenderableProps } from './types'\n\n// shared logic between components that use either render prop,\n// children render function, or component prop\nexport default function renderComponent<T>(\n props: RenderableProps<T> & T,\n name: string\n): React.Node {\n const { render, children, component, ...rest } = props\n if (component) {\n return React.createElement(component, { ...rest, children, render }) // inject children back in\n }\n if (render) {\n return render(children === undefined ? rest : { ...rest, children }) // inject children back in\n }\n if (typeof children !== 'function') {\n throw new Error(\n `Must specify either a render prop, a render function as children, or a component prop to ${name}`\n )\n }\n return children(rest)\n}\n"],"names":["defaultIsEqual","aArray","bArray","Array","isArray","length","some","a","index","useConstant","init","ref","React","useRef","current","useFieldArray","name","subscription","all","defaultValue","initialValue","isEqual","validateProp","validate","formMutators","useForm","mutators","push","pop","Error","Object","keys","reduce","result","key","args","value","allValues","meta","error","arrayError","ARRAY_ERROR","useField","format","v","input","fieldState","fields","forEach","iterator","len","i","map","results","fieldSubscriptionItems","versions","ffVersion","rffVersion","version","rest","props","render","children","component","undefined","renderComponent","__versions"],"mappings":"stBACuB,SAAjBA,EAAkBC,EAAeC,UACrCD,IAAWC,GACVC,MAAMC,QAAQH,IACbE,MAAMC,QAAQF,IACdD,EAAOI,SAAWH,EAAOG,SACxBJ,EAAOK,KAAK,SAACC,EAAGC,UAAUD,IAAML,EAAOM,KCS7B,SAASC,EAAeC,OAC/BC,EAAMC,EAAMC,gBACbF,EAAIG,UACPH,EAAIG,QAAUJ,KAETC,EAAIG,QCNS,SAAhBC,EACJC,sBAOyB,SALvBC,aAAAA,aAAeC,IACfC,IAAAA,aACAC,IAAAA,iBACAC,QAAAA,aAAUrB,IACAsB,IAAVC,SAKIC,EAFOC,UAAQ,iBAEeC,gBACbF,GAAgBA,EAAaG,MAAQH,EAAaI,WAEjE,IAAIC,MACR,sGAGEH,EAAWjB,EAAsB,kBAErCqB,OAAOC,KAAKP,GAAcQ,OAAO,SAACC,EAAQC,UACxCD,EAAOC,GAAO,sCAAI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