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

FormField components #2311

Open
9 tasks
Tracked by #1045
mrosvik opened this issue Aug 22, 2024 · 1 comment
Open
9 tasks
Tracked by #1045

FormField components #2311

mrosvik opened this issue Aug 22, 2024 · 1 comment
Labels
css @digdir/designsystemet-css ☂️ epic Issues ready react @digdir/designsystemet-react

Comments

@mrosvik
Copy link
Member

mrosvik commented Aug 22, 2024

Some designsystems use generic form classes/components, usually called form-group or form-field for their form components to have consistent placement of label, descriptions and such around an input/textarea/select element.

Notes:

This could be possible if we split up Textfield into multiple sub-components or generic form scaffolding components.

More generic, example from Radix:

  <Form.Root>
    <Form.Field>
      <Form.Label />
      <Form.Control asChild>
        <input className="Input" type="email" required />
      </Form.Control>
      <Form.Message />
      <Form.ValidityState />
    </Form.Field>

    <Form.Message />
    <Form.ValidityState />

    <Form.Submit />
  </Form.Root>

Or more scoped, Example from Ark UI

import { Field } from '@ark-ui/react'

export const Input = () => {
  return (
    <Field.Root>
      <Field.Label>Label</Field.Label>
      <Field.Input />
      <Field.HelperText>Some additional Info</Field.HelperText>
      <Field.ErrorText>Error Info</Field.ErrorText>
    </Field.Root>
  )
}

Tasks

  1. react 🕵️ investigate
    eirikbacker
  2. css 🐛 bug
@mrosvik mrosvik added the 🕵️ investigate Needs investigation label Aug 22, 2024
@mimarz mimarz added react @digdir/designsystemet-react css @digdir/designsystemet-css labels Aug 22, 2024
@eirikbacker
Copy link
Contributor

eirikbacker commented Aug 23, 2024

This is a React specific issue. I would argue, that we should in general skip the .Root level, and just do for instance:

<Accordion>
  <Accordion.Heading></Accordion.Heading>
  <Accordion.Content></Accordion.Content>
</Accordion>

When the base component is rendering a actual HTML element, as this clean and a very common pattern:
https://www.patterns.dev/react/compound-pattern/

When using .Root (or .Provider), the common pattern is that this is an indication to the consumer that the React element does not render any HTML, but is there more to provide a functional context:
https://kyleshevlin.com/compound-components/
https://medium.com/@win.le/react-compound-component-with-typescript-d7944ac0d1d6

This separation makes it easier to resonate and exemplify about how our design system translates between React and HTML and other frameworks, as the functional useContext is mostly a React pattern.

That said, I think we can maybe flip the question, and ask;

  • Binding the label + helptext + input + error message is a somewhat repetitive wiring task. We could provide a optional utility for this, and this could both be exposed as a React component, but also also in time as maybe a custom element <ds-field>. If we want something like this as an end goal, does that change our implementation for React?

Could the interface then be something like:

<Field>
  <Label>Label</Label>
  <Input />
  <Field.Help>Some additional Info</Field.Help>
  <Field.Error>Error Info</Field.Error>
</Field.Root>

...where <Label> and <Input> is not prefixed - aligning with native elements and HTML standard, and indicating that Field is optional just like in pure HTML? Pure HTML-example of the same:

<ds-field>
  <label>Label</label>
  <input />
  <ds-field-help>Some additional Info</ds-field-help>
  <ds-field-error>Error Info</ds-field-error>
</ds-field>

(dropping form from the name as input-like elements does not actually require being placed inside a form).

@mimarz mimarz changed the title Should our form components support Root pattern? Should our form components support context pattern? Sep 10, 2024
@mimarz mimarz changed the title Should our form components support context pattern? Should we have generic form components? Sep 12, 2024
@mimarz mimarz removed the 🕵️ investigate Needs investigation label Sep 19, 2024
@mimarz mimarz changed the title Should we have generic form components? Form compound components Sep 19, 2024
@mimarz mimarz added the ☂️ epic Issues ready label Sep 25, 2024
@mimarz mimarz changed the title Form compound components FormField components Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css @digdir/designsystemet-css ☂️ epic Issues ready react @digdir/designsystemet-react
Projects
Status: ☂ Epics
Development

No branches or pull requests

3 participants