fleet/frontend/components/forms
noahtalerman 23ce98ec51
Add Nunito Sans font and update global styles. (#59)
This is the first PR as a part of the Fleet UI Refresh #38.

Changes include:

- Add Nunito Sans font files and modify global styles to reflect the font change.
- Modify global font variables to reflect new sizing and weight naming conventions.
-- New sizing and naming conventions:
--- SIZE: xx-small: 12px, x-small: 14px, small: 16px, medium: 20px, large: 24px, x-large: 28px
--- WEIGHT: regular: 400, bold: 700
- Remove the old Oxygen font files.

Changes to other style sheets reflect the changes to the new font sizing and weight naming conventions for global variables. The changes don't necessarily use the correct size (as illustrated by mockups). Those "up to spec" sizing changes are to come.
2020-11-19 16:51:22 -08:00
..
admin Add Nunito Sans font and update global styles. (#59) 2020-11-19 16:51:22 -08:00
ChangeEmailForm Fix React deprecation warnings (#1976) 2019-01-06 17:25:33 -08:00
ChangePasswordForm Upgrade React to version 16 (#1983) 2019-01-14 13:45:28 -08:00
ConfigurePackQueryForm Update osquery versions available in schedule query UI (#2323) 2020-10-08 12:07:37 -07:00
ConfirmInviteForm Fix React deprecation warnings (#1976) 2019-01-06 17:25:33 -08:00
ConfirmSSOInviteForm Fix React deprecation warnings (#1976) 2019-01-06 17:25:33 -08:00
fields Add Nunito Sans font and update global styles. (#59) 2020-11-19 16:51:22 -08:00
ForgotPasswordForm Fix React deprecation warnings (#1976) 2019-01-06 17:25:33 -08:00
FormField Add Nunito Sans font and update global styles. (#59) 2020-11-19 16:51:22 -08:00
InviteUserForm Add Nunito Sans font and update global styles. (#59) 2020-11-19 16:51:22 -08:00
LabelForm Add Nunito Sans font and update global styles. (#59) 2020-11-19 16:51:22 -08:00
LoginForm Add Nunito Sans font and update global styles. (#59) 2020-11-19 16:51:22 -08:00
LogoutForm Fix React deprecation warnings (#1976) 2019-01-06 17:25:33 -08:00
packs Fix React deprecation warnings (#1976) 2019-01-06 17:25:33 -08:00
queries/QueryForm Add Nunito Sans font and update global styles. (#59) 2020-11-19 16:51:22 -08:00
RegistrationForm Add Nunito Sans font and update global styles. (#59) 2020-11-19 16:51:22 -08:00
ResetPasswordForm Fix React deprecation warnings (#1976) 2019-01-06 17:25:33 -08:00
UserSettingsForm Fix React deprecation warnings (#1976) 2019-01-06 17:25:33 -08:00
validators Implement osquery options page (#11) 2020-11-04 18:00:51 -08:00
_styles.scss Handle server errors (#730) 2017-01-05 19:01:17 -05:00
Form.jsx Fix React deprecation warnings (#1976) 2019-01-06 17:25:33 -08:00
README.md Replace uses of the term "Kolide" with "Fleet" (#1999) 2019-01-24 09:39:32 -08:00

Fleet Forms

Fleet Forms leverage the Form Higher Order Component to simplify implementation and state management. As a user fills out a form, the Form HOC collects the form data in state. When the form is submitted, the Form HOC calls the provided client-side validation function with the form data and, if valid, then calls the handleSubmit prop with the form data. If the client-side validation function returns errors, those errors are stored in the Form HOC state and displayed in the form input where the input name matches the error key.

The Form HOC takes 3 parameters:

  • Component Class: The Component Class is the individual form component. It is a React component that renders a form.
  • Options Hash: The Options Hash accepts 2 options:
    • fields: This option is an array of field name strings in the form.
    • validate: This option is a function that gets called with the form data to test validity of the form. The return value from the validate function is expected to be a Javascript object with a valid key that has a boolean value signifying whether or not the form is valid, and an errors key that has a Javascript object value containing the client side validation errors.
      type ValidateResponse = { valid: String, errors: Object };
      const validate Function = (formData: Object): ValidateResponse => { ... };
    

The Form HOC renders with Component Class with additional props. The added props to the form are:

  • fields: The fields prop is a Javascript object containing an object for each field string passed to the Form HOC in the fields array. Each field object contains the following:
    • error: A string containing client side validation errors from the validate function or from the serverErrors prop passed to the form.
    • name: The name of the form field.
    • onChange: A function used to handle change on a form field element. This function stores the value of the form field element in state, and then submits the form field element values when the form is submitted.
    • value: The value of the form field element.

Additionally, the Form HOC accepts the following props passed to the form instance:

  • serverErrors: A Javascript object containing errors returned by the server. The key should be the name of the form field and the value should be the error message string. (Defaults to {}).
  • formData: A Javascript object representing the entity that will populate the form with the entity's attributes. When updating an entity, pass the entity to the form as the formData prop.
  • handleSubmit: A function that is called when the form is submitted. The function will be called with the form data if the validate function is run without errors.
  • onChangeFunc: A function that is called when a form field is changed. It is passed 2 parameters, the form field name and value. This is useful for handling specific form field changes on the parent component.

Example:

// Defining the form

import Button from 'components/buttons/Button';
import Form from 'components/forms/Form';
import InputField from 'components/forms/fields/InputField';

class MyForm extends Component {
  render () {
    return (
      <form onSubmit={this.props.handleSubmit}>
        <InputField
          {...this.props.fields.first_name}
        />
        <InputField
          {...this.props.fields.last_name}
        />
        <Button type="submit" />
      </form>
    );
  }
}

export default Form(MyForm, {
  fields: ['first_name', 'last_name'],
  validate: (formData) => {
    return { errors: {}, valid: true };
  },
});


// Rendering the form
import MyForm from 'components/forms/MyForm';

class MyFormPage extends Component {
  handleSubmit = (formData) => {
    console.log(formData);
  }

  render () {
    return (
      <div>
        <MyForm handleSubmit={this.handleSubmit} />
      </div>
    );
  }
}

export default MyFormPage