import isEmpty from 'lodash/isEmpty' ; import union from 'lodash/union' ; import type { JsonFormsCellRendererRegistryEntry } from '../reducers' ; import { getAjv , getConfig , getData , getErrorAt , getSchema , getTranslator , } from '../reducers' ; import { AnyAction , Dispatch } from './type' ; import { formatErrorMessage , Resolve , } from './util' ; import { isInherentlyEnabled , isVisible , } from './runtime' ; import { DispatchPropsOfControl , EnumOption , enumToEnumOptionMapper , mapDispatchToControlProps , oneOfToEnumOptionMapper , OwnPropsOfControl , OwnPropsOfEnum , StatePropsOfScopedRenderer , } from './renderer' ; import { JsonFormsState } from '../store' ; import { JsonSchema } from '../models' ; import { getI18nKeyPrefix } from '../i18n' ; export type { JsonFormsCellRendererRegistryEntry } ; export interface OwnPropsOfCell extends OwnPropsOfControl { data ? : any ; } /**
 * State props of a cell.
 */ export interface StatePropsOfCell extends StatePropsOfScopedRenderer { isValid : boolean ; rootSchema : JsonSchema ; } export interface OwnPropsOfEnumCell extends OwnPropsOfCell , OwnPropsOfEnum { } /**
 * State props of a cell for enum cell
 */ export interface StatePropsOfEnumCell extends StatePropsOfCell , OwnPropsOfEnum { } /**
 * Props of an enum cell.
 */ export interface EnumCellProps extends StatePropsOfEnumCell , DispatchPropsOfControl { } export type DispatchPropsOfCell = DispatchPropsOfControl ; /**
 * Props of a cell.
 */ export interface CellProps extends StatePropsOfCell , DispatchPropsOfCell { } param { RankedTester } tester param cell returns { any } export interface DispatchCellStateProps extends StatePropsOfCell { cells ? : JsonFormsCellRendererRegistryEntry [ ] ; } param state param ownProps returns { StatePropsOfCell } export const mapStateToCellProps = ( state , ownProps ) : StatePropsOfCell => { const { id , schema , path , uischema , renderers , cells } = ownProps ; const rootData = getData ( state ) ; const visible = ownProps . visible !== undefined ? ownProps . visible : isVisible ( uischema , rootData , undefined , getAjv ( state ) ) ; const rootSchema = getSchema ( state ) ; const config = getConfig ( state ) ; let enabled ; if ( state . jsonforms . readonly === true ) { enabled = false ; } else if ( typeof ownProps . enabled === 'boolean' ) { enabled = ownProps . enabled ; } else { enabled = isInherentlyEnabled ( state , ownProps , uischema , schema || rootSchema , rootData , config ) ; } const errors = formatErrorMessage ( union ( getErrorAt ( path , schema ) ( state ) . map ( error => error . message ) ) ) ; const isValid = isEmpty ( errors ) ; return { data : Resolve . data ( rootData , path ) , visible , enabled , id , path , errors , isValid , schema , uischema , config : getConfig ( state ) , rootSchema , renderers , cells } ; } ; export const mapStateToDispatchCellProps = ( state , ownProps ) : DispatchCellStateProps => { const props = mapStateToCellProps ( state , ownProps ) ; const { renderers , cells , ... otherOwnProps } = ownProps ; return { ... props , ... otherOwnProps , cells : cells || state . jsonforms . cells || [ ] } ; } ; export interface DispatchCellProps extends DispatchCellStateProps { } param state param ownProps returns { StatePropsOfEnumCell } export const defaultMapStateToEnumCellProps = ( state , ownProps ) : StatePropsOfEnumCell => { const props = mapStateToCellProps ( state , ownProps ) ; const options = ownProps . options || props . schema . enum ?. map ( e => enumToEnumOptionMapper ( e , getTranslator ( ) ( state ) , getI18nKeyPrefix ( props . schema , props . uischema , props . path ) ) ) || ( props . schema . const && [ enumToEnumOptionMapper ( props . schema . const , getTranslator ( ) ( state ) , getI18nKeyPrefix ( props . schema , props . uischema , props . path ) ) ] ) ; return { ... props , options } ; } ; param state param ownProps returns { StatePropsOfEnumCell } export const mapStateToOneOfEnumCellProps = ( state , ownProps ) : StatePropsOfEnumCell => { const props = mapStateToCellProps ( state , ownProps ) ; const options = ownProps . options || ( props . schema . oneOf as JsonSchema [ ] ) ?. map ( oneOfSubSchema => oneOfToEnumOptionMapper ( oneOfSubSchema , getTranslator ( ) ( state ) , getI18nKeyPrefix ( props . schema , props . uischema , props . path ) ) ) ; return { ... props , options } ; } ; type { ( dispatch ) => { handleChange ( path , value ) : void } } export const mapDispatchToCellProps = mapDispatchToControlProps ; /**
 * Default dispatch to control props which can be customized to set handleChange action
 *
 */ export const defaultMapDispatchToControlProps = ( dispatch , ownProps ) : DispatchPropsOfControl => { const { handleChange } = mapDispatchToCellProps ( dispatch ) ; return { handleChange : ownProps . handleChange || handleChange } ; } ;