Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template support for MultiSchemaField(OneOfField/AnyOfField) #3918

Open
1 task done
Tracked by #3883
YuJianghao opened this issue Oct 24, 2023 · 1 comment
Open
1 task done
Tracked by #3883

Template support for MultiSchemaField(OneOfField/AnyOfField) #3918

YuJianghao opened this issue Oct 24, 2023 · 1 comment
Labels
core feature Is a feature request help wanted templates Related to the templates capabilities of RJSF utils Related to @rjsf/utils

Comments

@YuJianghao
Copy link

Prerequisites

What theme are you using?

other

Is your feature request related to a problem? Please describe.

When building the Carbon theme (#3883), I met the following problem:

Suppose I want to implement the following oneOf UI:

  • A key oneOf that told the user this is a oneOf
  • A gray background that indicates which part(in this case, the loram field) the oneOf is controlling.

image

Without modifying the default OneOfField. I got:

image

The problem is: How can I change the UI without rewriting the entire OneOfField?

Describe the solution you'd like

Provide a OneOfTemplate/AnyOfTemplate, and replace the render part of the OneOfField

return (
<div className='panel panel-default panel-body'>
<div className='form-group'>
<Widget
id={this.getFieldId()}
name={`${name}${schema.oneOf ? '__oneof_select' : '__anyof_select'}`}
schema={{ type: 'number', default: 0 } as S}
onChange={this.onOptionChange}
onBlur={onBlur}
onFocus={onFocus}
disabled={disabled || isEmpty(enumOptions)}
multiple={false}
rawErrors={rawErrors}
errorSchema={fieldErrorSchema}
value={selectedOption >= 0 ? selectedOption : undefined}
options={{ enumOptions, ...uiOptions }}
registry={registry}
formContext={formContext}
placeholder={placeholder}
autocomplete={autocomplete}
autofocus={autofocus}
label={title ?? name}
hideLabel={!displayLabel}
/>
</div>
{option !== null && <_SchemaField {...this.props} schema={optionSchema!} />}
</div>

to:

    const OneOfTemplateProps = {
      widget: <Widget /* some props for widget*/ />,
      content: option !== null && <_SchemaField {...this.props} schema={optionSchema!} />,
      // ...other props for OneOfTemplate
    }

    return <OneOfTemplate {... OneOfTemplateProps}/>

Then I can create the target UI by implementing a template without touching the complicated logic:

function OneOfTemplate({widget, content}){
  return (
    <FormGroup {...someProps}>
      <LayerBackground {...someProps}>
        {widget}
        {content}
      </LayerBackground>
    </FormGroup>
  );
}

Describe alternatives you've considered

Rewrite the whole OneOfField can do this, but not good enough.

@YuJianghao YuJianghao added feature Is a feature request needs triage Initial label given, to be assigned correct labels and assigned labels Oct 24, 2023
@heath-freenome
Copy link
Member

heath-freenome commented Oct 27, 2023

@YuJianghao You can provide, as part of your theme a custom AnyOfField and/or OneOfField that does what you want. See the docs

That said, I agree that having this new feature would be really helpful for theme builders like you. Are you willing to implement this first before completing your carbon theme?

@heath-freenome heath-freenome added help wanted core utils Related to @rjsf/utils templates Related to the templates capabilities of RJSF and removed needs triage Initial label given, to be assigned correct labels and assigned labels Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core feature Is a feature request help wanted templates Related to the templates capabilities of RJSF utils Related to @rjsf/utils
Projects
None yet
Development

No branches or pull requests

2 participants