question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"ui:template" schema option

See original GitHub issue

Prerequisites

  • I have read the documentation;
  • In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.

Description

For more theming control a "ui:template" option would be helpful. From what I can tell this would be a fairly simple matter to integrate three places:

  • SchemaField
  • ObjectField
  • ArrayField

This would build on the work in #304.

Expected behavior

People could use custom templates to customize layout. This would be the most pertinent for objects and arrays, but can only be useful for leaf nodes too.

const uiSchema = {
  "ui:template": GroupedObjectTemplate,
  title: {
    "ui:template": InlineField
  }
  done: {
    "ui:template": ErrorsInTooltip
  },
  comments: {
    "ui:template": InlineArray
  }
};

Alternative 1 - External plugin

Currently as a workaround this behaviour can be (almost) achieved externally. The biggest blocker to making it easier to achieve externally is that the default template objects are not exported.

import React from "react";
// TODO: Would be useful to 
import {
  DefaultObjectFieldTemplate,
  DefaultFieldTemplate,
  DefaultArrayItem
} from "./DefaultTemplates";

export function FieldTemplate(props) {
  const Template = props.uiSchema["ui:template"];
  if (
    Template &&
    // TODO: LV: Seems strange that this is required to be plugged in... oh well
    props.schema.type !== "object" &&
    props.schema.type !== "array"
  ) {
    return <Template {...props} />;
  } else {
    return <DefaultFieldTemplate {...props} />;
  }
}

export const ObjectFieldTemplate = defaultOrComponent(
  DefaultObjectFieldTemplate
);
export const ArrayFieldTemplate = defaultOrComponent(DefaultArrayItem);

function defaultOrComponent(DefaultTemplate) {
  return function(props) {
    const Template = props.uiSchema["ui:template"];
    if (Template) {
      console.error("Can't render", Template, props);
      return <Template {...props} />;
    } else {
      return <DefaultTemplate {...props} />;
    }
  };
}

And then use that on your Form

  import * as UiTemplate from "...";
  
  const fm = <Form
    schema={schema}
    uiSchema={uiSchema}
    onChange={log("changed")}
    onSubmit={log("submitted")}
    onError={log("errors")}
    transformErrors={log("transformErrors")}
    liveValidate={true}
    formContext={{
      templates: Templates.GroupTemplates
    }}
    {...UiTemplate}
  >

Alternative 2 - “ui:field”

“ui:field” is very powerful, but it requires too much re-implementation of core behaviour to get right, particularly for things like arrays and objects.

Recommendation

I would recommend tackling this in phases, getting simpler functionality merged in earlier, and complex functionality merged in later.

Phase 1 allow templates as objects

  • Before: const Template = registry.ObjectFieldTemplate || DefaultObjectFieldTemplate;
  • After: const Template = uiSchema["ui:template"] || registry.ObjectFieldTemplate || DefaultObjectFieldTemplate;

Phase 2 allow templates as strings, add templates to registry, allow custom templates map, add default templates to default registry

Phase 3 themes for other ui libraries

Resolution

If the maintainers of this repository are on-board, then I can work on a pull request.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
loganvolkerscommented, Jan 22, 2019

@epicfaace Great I’ll work on getting something wired up.

1reaction
loganvolkerscommented, Aug 27, 2018

Nice, looks promising. Now we wait for contributors…

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Schema Templates in Rank Math PRO
Schema Templates are available only in Rank Math PRO. ... When you click the Your Templates option, all your existing Schema Templates will...
Read more >
Section schema - Shopify.dev
This tag allows you to define various attributes of a section, such as the section name, any section blocks, and settings to allow...
Read more >
Using Schemas | Cloud Deployment Manager Documentation
A schema describes the specifications of a Deployment Manager template. If a schema exists for a template, Deployment Manager uses the schema to...
Read more >
Template structure and syntax - Azure Resource Manager
https://schema.management.azure.com/schemas/2019-04-01/ ... You have a few options for adding comments and metadata to your template.
Read more >
Adding a data source schema to a template in ... - IBM
Select the Document Design tab. For the Use Schema annotations for display option, select No. Click OK to save the change.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found