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.

grommet-forms proposal

See original GitHub issue

This is a proposal for a new repository for grommet-forms.

The idea is to make it easier to build typical forms with grommet components. Here is an example of how it could be used:

import { Form, FormField, FormSubmitButton, FormTextInput, FormTextArea } from 'grommet-forms';

class FormExample extends Component {
  state = { value: {} }

  onSubmit = () => {
    const { value } = this.state;
    // whatever submitting the form should entail
  }

  render() {
    const { value } = this.state;
    return (
      <Form
        value={value}
        onChange={value => this.setState({ value })}
        onSubmit={this.onSubmit}
      >
        <FormField label="Name" name="name" required />
        <FormField label="Email" name="email" type="email" />
        <FormField label="Employee ID" name="employeeId" validate={/^[0-9]+$/} />
        <FormField label="Size" name="size" options={['small', 'medium', 'large']} /> {/* renders RadioButtons */}
        <FormField label="Comments" name="comments">
          <FormTextArea name="comments" />
        </FormField>
        <FormSubmitButton label="Update" />
      </Form>
    );
  }
}

FormField.propTypes = {
  label: PropTypes.string.description("Human readable label"),
  name: PropTypes.string
    .description("The key to use within the Form value to store the data.")
    .isRequired,
  options: PropTypes.arrayOf(PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.object,
  ])).description("Array of options. If less than four, RadioButtons, otherwise, Select."),
  optionLabelKey: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.func,
  ]).description("When options are objects, the key to get the label."),
  optionValueKey: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.func,
  ]).description("When options are objects, the key to get the value."),
  required: PropTypes.bool.description("Whether the field is required."),
  validate: PropTypes.oneOfType([
    PropTypes.object, // regular expression
    PropTypes.func,
  ]).description("Validation rule. Provide a regular expression or a function."),
};

Please let us know if you have any feedback. We’d like some discussion in this issue before proceeding further.

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
murbanowiczcommented, Dec 4, 2018

Formik is currently best or at least one of the best solutions for forms with React. Maybe instead of creating something new it is better to either just document how to use Formik with Grommet or create simple wrapper for Grommet & Formik ?

1reaction
krawallicommented, Dec 15, 2018

I’ve played a little bit with this and -> very nice and thumb up!

However, I’m struggling with e.g. CheckBox. If used as component= parameter - e.g. the label= is rendered twice. The OnChange Method seems to be implemented by user (it could be done with auto-magic in the background).The value itself seems not to be picked up by the submit method.

Beyond that:

  • Still having problems to get “validator” to do what i want.
  • for validation, there should be any i18n for the error text
  • if you have two password inputs, a validator should compare to other inputs in that form. How to reference them in code. Maybe a “auto-ref” could be handy here.
  • if I set “errors” via var before first submit, then these are not rendered (or at least i did it wrong)

i would suggest to have a form with all standard input types, so there is a kind of documentation for usage.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Form - Grommet
Form manages the collective state of its FormFields. There are two primary ways of ... A controlled Form is similar to a controlled...
Read more >
Documentation for Grommet - Medium
How do the Grommet docs currently work? Grommet Library. Each component has a doc.js file that contains documentation in the form of a...
Read more >
Connector header grommet for an implantable medical device
A penetrable grommet is disposed within the header grommet aperture, ... although proposals have been made to form hermetically sealed housings of a ......
Read more >
Grommet Machine Sales Points.pub - Edward Segal Inc -
Manual load, machine punches the hole and sets the grommet. • Does not require Self Piercing Grommets. Works with standard Plain and Rolled...
Read more >
Dritz 730-33-T 2-Part Eyelets Kit with Tools, Gunmetal, 1/4 ...
Our dress forms offer unique characteristics that make it easier to get a good ... 1/4 Inch Grommet Kit 200 Sets Grommets Eyelets...
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