{"version":3,"file":"FieldView.cjs","sources":["../../../../../packages/react-binding/src/helperComponents/FieldView.tsx"],"sourcesContent":["import { useArrayMapMemo, useConstantLengthInvariant } from '@contember/react-utils'\nimport { ReactElement, ReactNode, useCallback, useMemo } from 'react'\nimport { useEntity } from '../accessorPropagation'\nimport type { FieldAccessor, FieldValue, RelativeSingleField, SugaredRelativeSingleField } from '@contember/binding'\nimport { QueryLanguage, throwBindingError } from '@contember/binding'\nimport { Component } from '../coreComponents'\nimport { SugaredField } from './SugaredField'\n\nexport interface FieldViewCommonProps {\n\tfallbackIfUnpersisted?: ReactNode\n}\n\nexport type FieldViewProps =\n\t& FieldViewCommonProps\n\t& (\n\t\t| {\n\t\t\trender: (...accessors: FieldAccessor[]) => ReactNode\n\t\t\tfield: string | SugaredRelativeSingleField\n\t\t  }\n\t\t| {\n\t\t\trender: (...accessors: FieldAccessor[]) => ReactNode\n\t\t\tfields: Array<string | SugaredRelativeSingleField>\n\t\t  }\n\t)\n\n/**\n * Base field view component with no default formatting.\n *\n * @example\n * ```\n * <FieldView\n *   field=\"startsAt\"\n *   render={(accessor) => <>{accessor.value}</>}\n * />\n * ```\n *\n * @group Field Views\n */\nexport const FieldView = Component<FieldViewProps>(\n\tprops => {\n\t\tconst { fallbackIfUnpersisted, render } = props\n\t\tconst fields =\n\t\t\t'fields' in props && Array.isArray(props.fields)\n\t\t\t\t? props.fields\n\t\t\t\t: 'field' in props && props.field\n\t\t\t\t\t? [props.field]\n\t\t\t\t\t: throwBindingError(`FieldView: failed to supply either the 'field' or the 'fields' prop.`)\n\n\t\tuseConstantLengthInvariant(\n\t\t\tfields,\n\t\t\t`The number of fields in the 'fields' prop of the 'FieldView' must remain constant between renders!`,\n\t\t)\n\n\t\t// TODO over-subscribe less\n\t\tconst entityAccessor = useEntity()\n\t\tconst environment = entityAccessor.environment\n\n\t\tconst desugarField = useCallback(\n\t\t\t(rsf: string | SugaredRelativeSingleField) => QueryLanguage.desugarRelativeSingleField(rsf, environment),\n\t\t\t[environment],\n\t\t)\n\t\tconst desugaredFields = useArrayMapMemo(fields, desugarField)\n\n\t\tconst retrieveField = useCallback(\n\t\t\t(desugaredRsf: RelativeSingleField) => entityAccessor.getField(desugaredRsf),\n\t\t\t[entityAccessor],\n\t\t)\n\t\tconst accessors = useArrayMapMemo(desugaredFields, retrieveField)\n\n\t\t// TODO we probably want something like useDeferredValue from here.\n\t\t//      Will probably just wait for Concurrent React though.\n\t\tconst output = useMemo(() => {\n\t\t\tif (!entityAccessor.existsOnServer && fallbackIfUnpersisted !== undefined) {\n\t\t\t\treturn fallbackIfUnpersisted\n\t\t\t}\n\t\t\treturn render(...accessors)\n\t\t}, [accessors, entityAccessor.existsOnServer, fallbackIfUnpersisted, render])\n\n\t\treturn <>{output}</>\n\t},\n\tprops => {\n\t\tconst fields =\n\t\t\t'fields' in props && Array.isArray(props.fields)\n\t\t\t\t? props.fields\n\t\t\t\t: 'field' in props && props.field\n\t\t\t\t\t? [props.field]\n\t\t\t\t\t: throwBindingError(`FieldView: failed to supply either the 'field' or the 'fields' prop.`)\n\t\treturn (\n\t\t\t<>\n\t\t\t\t{props.fallbackIfUnpersisted}\n\t\t\t\t{fields.map((field, i) => (\n\t\t\t\t\t<SugaredField key={i} field={field} />\n\t\t\t\t))}\n\t\t\t</>\n\t\t)\n\t},\n\t'FieldView',\n) as FieldViewComponentSignature\n\nexport type FieldViewComponentSignature = {\n\t<FV1 extends FieldValue>(\n\t\tprops:\n\t\t\t& FieldViewCommonProps\n\t\t\t& {\n\t\t\t\tfield: string | SugaredRelativeSingleField\n\t\t\t\trender: (field1: FieldAccessor<FV1>) => ReactNode\n\t\t\t},\n\t): ReactElement | null\n\n\t<FV1 extends FieldValue>(\n\t\tprops:\n\t\t\t& FieldViewCommonProps\n\t\t\t& {\n\t\t\t\tfields: [string | SugaredRelativeSingleField]\n\t\t\t\trender: (field1: FieldAccessor<FV1>) => ReactNode\n\t\t\t},\n\t): ReactElement | null\n\n\t<FV1 extends FieldValue, FV2 extends FieldValue>(\n\t\tprops:\n\t\t\t& FieldViewCommonProps\n\t\t\t& {\n\t\t\t\tfields: [string | SugaredRelativeSingleField, string | SugaredRelativeSingleField]\n\t\t\t\trender: (field1: FieldAccessor<FV1>, field2: FieldAccessor<FV2>) => ReactNode\n\t\t\t},\n\t): ReactElement | null\n\n\t<FV1 extends FieldValue, FV2 extends FieldValue, FV3 extends FieldValue>(\n\t\tprops:\n\t\t\t& FieldViewCommonProps\n\t\t\t& {\n\t\t\t\tfields: [string | SugaredRelativeSingleField, string | SugaredRelativeSingleField, string | SugaredRelativeSingleField]\n\t\t\t\trender: (field1: FieldAccessor<FV1>, field2: FieldAccessor<FV2>, field3: FieldAccessor<FV3>) => ReactNode\n\t\t\t},\n\t): ReactElement | null\n\n\t<FV1 extends FieldValue, FV2 extends FieldValue, FV3 extends FieldValue, FV4 extends FieldValue>(\n\t\tprops:\n\t\t\t& FieldViewCommonProps\n\t\t\t& {\n\t\t\t\tfields: [string | SugaredRelativeSingleField, string | SugaredRelativeSingleField, string | SugaredRelativeSingleField, string | SugaredRelativeSingleField]\n\t\t\t\trender: (field1: FieldAccessor<FV1>, field2: FieldAccessor<FV2>, field3: FieldAccessor<FV3>, field4: FieldAccessor<FV4>) => ReactNode\n\t\t\t},\n\t): ReactElement | null\n\n\t<FV1 extends FieldValue, FV2 extends FieldValue, FV3 extends FieldValue, FV4 extends FieldValue, FV5 extends FieldValue>(\n\t\tprops:\n\t\t\t& FieldViewCommonProps\n\t\t\t& {\n\t\t\t\tfields: [string | SugaredRelativeSingleField, string | SugaredRelativeSingleField, string | SugaredRelativeSingleField, string | SugaredRelativeSingleField, string | SugaredRelativeSingleField]\n\t\t\t\trender: (field1: FieldAccessor<FV1>, field2: FieldAccessor<FV2>, field3: FieldAccessor<FV3>, field4: FieldAccessor<FV4>, field5: FieldAccessor<FV5>) => ReactNode\n\t\t\t},\n\t): ReactElement | null\n\n\t(props: FieldViewProps): ReactElement | null\n}\n"],"names":["Component","throwBindingError","useConstantLengthInvariant","useEntity","useCallback","QueryLanguage","useArrayMapMemo","useMemo","this","jsxDEV","Fragment","SugaredField"],"mappings":";;;;;;;;;AAsCO,MAAM,YAAYA,UAAA;AAAA,EACxB,CAAS,UAAA;AACF,UAAA,EAAE,uBAAuB,OAAA,IAAW;AAC1C,UAAM,SACL,YAAY,SAAS,MAAM,QAAQ,MAAM,MAAM,IAC5C,MAAM,SACN,WAAW,SAAS,MAAM,QACzB,CAAC,MAAM,KAAK,IACZC,QAAAA,kBAAkB,sEAAsE;AAE7FC,eAAA;AAAA,MACC;AAAA,MACA;AAAA,IACD;AAGA,UAAM,iBAAiBC,UAAAA,UAAU;AACjC,UAAM,cAAc,eAAe;AAEnC,UAAM,eAAeC,MAAA;AAAA,MACpB,CAAC,QAA6CC,QAAc,cAAA,2BAA2B,KAAK,WAAW;AAAA,MACvG,CAAC,WAAW;AAAA,IACb;AACM,UAAA,kBAAkBC,WAAAA,gBAAgB,QAAQ,YAAY;AAE5D,UAAM,gBAAgBF,MAAA;AAAA,MACrB,CAAC,iBAAsC,eAAe,SAAS,YAAY;AAAA,MAC3E,CAAC,cAAc;AAAA,IAChB;AACM,UAAA,YAAYE,WAAAA,gBAAgB,iBAAiB,aAAa;AAI1D,UAAA,SAASC,MAAAA,QAAQ,MAAM;AAC5B,UAAI,CAAC,eAAe,kBAAkB,0BAA0B,QAAW;AACnE,eAAA;AAAA,MAAA;AAED,aAAA,OAAO,GAAG,SAAS;AAAA,IAAA,GACxB,CAAC,WAAW,eAAe,gBAAgB,uBAAuB,MAAM,CAAC;AAE5E,0EAAU,UAAH,UAAA,QAAA,OAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAAU,GAAAC,MAAA;AAAA,EAClB;AAAA,EACA,CAAS,UAAA;AACR,UAAM,SACL,YAAY,SAAS,MAAM,QAAQ,MAAM,MAAM,IAC5C,MAAM,SACN,WAAW,SAAS,MAAM,QACzB,CAAC,MAAM,KAAK,IACZP,QAAAA,kBAAkB,sEAAsE;AAC7F,WAEGQ,8BAAA,OAAAC,wBAAA,EAAA,UAAA;AAAA,MAAM,MAAA;AAAA,MACN,OAAO,IAAI,CAAC,OAAO,MAClBD,8BAAAA,OAAAE,aAAA,cAAA,EAAqB,SAAH,GAAnB,OAAA;AAAA,QAAA,UAAA;AAAA,QAAA,YAAA;AAAA,QAAA,cAAA;AAAA,MAAA,GAAAH,MAAoC,CACpC;AAAA,IAAA,KAJF,QAAA,MAAA;AAAA,MAAA,UAAA;AAAA,MAAA,YAAA;AAAA,MAAA,cAAA;AAAA,IAKA,GAAAA,MAAA;AAAA,EAEF;AAAA,EACA;AACD;;"}