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

Custom template for checkbox/boolean and select #3879

Closed
1 task done
brampurnot opened this issue Sep 24, 2023 · 3 comments
Closed
1 task done

Custom template for checkbox/boolean and select #3879

brampurnot opened this issue Sep 24, 2023 · 3 comments

Comments

@brampurnot
Copy link

Prerequisites

What theme are you using?

core

What is your question?

Hi all,

I'm trying to implement custom templates since we are using tailwinds and headless components. I have noticed that the standard BaseInputTemplate is not working for checkboxes/booleans. Also, there doesn't seem to be a custom template for select elements.

Am I missing something in the documentation or am I right in saying that this is not supported?

Thanks,
Bram

@brampurnot brampurnot added needs triage Initial label given, to be assigned correct labels and assigned question labels Sep 24, 2023
@aularon
Copy link
Contributor

aularon commented Oct 1, 2023

I believe this is doable natively by passing the custom field/widget implementation to uiSchema. Below is a working solution using rjsf-layout however.
(You can extract the inline component to make it reusable)

demo.mp4

"use client";
import Form, { JSONSchemaObject } from "rjsf-layout";
import validator from "@rjsf/validator-ajv8";

const listItems = ["a", "b", "c"] as const;
const schema = {
  type: "object",
  properties: {
    isChosen: {
      type: "boolean",
    },
    chosenItem: {
      type: "string",
      enum: listItems,
    },
  },
} as const satisfies JSONSchemaObject;

export const CustomFields = () => (
  <Form {...{ schema, validator }} formData={{}}>
    {({ IsChosen, ChosenItem, formData }) => (
      <>
        <table>
          <tbody>
            <tr>
              <th>boolean field:</th>
              <td>
                <IsChosen>
                  {/* First inline component that handles boolean values */}
                  {({ $isChosen, setIsChosen }) => (
                    <button
                      className={$isChosen ? "active" : ""}
                      onClick={() => setIsChosen(!$isChosen)}
                    >
                      {$isChosen ? "Chosen" : "NOT chosen!"}
                    </button>
                  )}
                </IsChosen>
              </td>
            </tr>
            <tr>
              <th>list field:</th>
              <td>
                <ChosenItem>
                  {/* Second inline component that handles the items list */}
                  {({ $chosenItem, setChosenItem }) =>
                    listItems.map((item) => (
                      <button
                        key={item}
                        className={item === $chosenItem ? "active" : ""}
                        onClick={() => setChosenItem(item)}
                      >
                        {item}
                      </button>
                    ))
                  }
                </ChosenItem>
              </td>
            </tr>
          </tbody>
        </table>
        <pre>{JSON.stringify(formData)}</pre>
        <style
          dangerouslySetInnerHTML={{
            __html: "button.active{background: darkblue;color:white}",
          }}
        ></style>
      </>
    )}
  </Form>
);

@heath-freenome
Copy link
Member

@brampurnot Have you checked out the documentation around using custom widgets? Checkboxes and booleans are in fact not using the BaseInputTemplate as you can see from the implementation of BooleanField. It seems what you are really wanting is a new custom theme. I would look at how the existing themes are built on top of the core library

@heath-freenome heath-freenome added awaiting response and removed needs triage Initial label given, to be assigned correct labels and assigned labels Oct 6, 2023
@brampurnot
Copy link
Author

Thanks for responses. Custom widgets are indeed the way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants