Skip to content

Render my custom components depending on the input type #4339

@arnaumanyosa

Description

@arnaumanyosa

Prerequisites

What theme are you using?

mui

What is your question?

If type is number I want to simply render the input that rjsf provides and just tweak some details (for example provide a translated label).

But if it's a checkbox I want to render a MUI Switch (by default rjsf renders a checkbox). I'm working on something like this (WiP, not working):

const CustomBaseInputTemplate = (props: BaseInputTemplateProps) => {
  const { type, label, schema, options, title, ...rest } = props
  const inputProps = { ...rest, ...getInputProps(schema, type, options) }
  const { BaseInputTemplate } = Templates

  switch (inputProps.type) {
    case 'number':
      return (
        <BaseInputTemplate
          {...props}
          label={translate(translationKey)}
          id={schema.title}
          variant='outlined'
        />
      )
     case 'checkbox':
      return (
        <Switch //MUI Switch component
          {...props}
          id={schema.title}
        />
      )
    default:
      return <></>
  }
}

<Form
  schema={schema}
  validator={validator}
  templates={{
    BaseInputTemplate: CustomBaseInputTemplate
  }}
/>

Is this approach correct?

Currently returning does not work. I followed #3481 suggestion, but it returns an error:

JSX element type 'BaseInputTemplate' does not have any construct or call signatures.ts(2604)
'BaseInputTemplate' cannot be used as a JSX component.
  Its type 'ComponentType<BaseInputTemplateProps<any, RJSFSchema, any>> | undefined' is not a valid JSX element type.
    Type 'undefined' is not assignable to type 'ElementType'.ts(2786)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions