Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dpck/form
The Bootstrap Form Component For Entering Data.
https://github.com/dpck/form
Last synced: about 1 month ago
JSON representation
The Bootstrap Form Component For Entering Data.
- Host: GitHub
- URL: https://github.com/dpck/form
- Owner: dpck
- License: mit
- Created: 2019-02-07T09:14:33.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-28T22:44:04.000Z (over 3 years ago)
- Last Synced: 2024-10-07T23:36:09.995Z (3 months ago)
- Language: JavaScript
- Homepage: https://dpck.github.io/form/
- Size: 740 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @depack/form
[![npm version](https://badge.fury.io/js/%40depack%2Fform.svg)](https://www.npmjs.com/package/@depack/form)
`@depack/form` is The Bootstrap Form Component For Entering Data.
![@depack/form demo](doc/demo.gif)
Click For The Demo```sh
yarn add -E @depack/form
```## Table Of Contents
- [Table Of Contents](#table-of-contents)
- [API](#api)
* [**Form**](#form)
* [**FormGroup**](#formgroup)
* [**Input**](#input)
* [**Select**](#select)
* [**Textarea**](#textarea)
* [**SubmitForm**](#submitform)
* [`reset(): void`](#reset-void)
* [**SubmitButton**](#submitbutton)
- [Custom Components](#custom-components)
- [Copyright](#copyright)## API
The package is available by importing its default and named functions:
```js
import Form, {
FormGroup, Input, TextArea, Select,
} from '@depack/form'
```### **Form**
Creates the form that maintains the values of each field that is found inside its children. Any additional properties will be passed down to the form. Each child component will receive `values` in its context.
`_depackForm.FormProps`: Options for the Form component.
| Name | Type | Description |
| -------- | ------------------ | ----------------------------------------------------------------------------------- |
| onChange | !Function | The callback to call when a change is made to any of the inputs inside of the form. |
| formRef | !Function | The function to call with the reference to the form HTML. |
| onSubmit | !Function | The function to call on form submit. |```jsx
import Form, {
FormGroup, TextArea, Input, Select, SubmitButton, SubmitForm,
} from '@depack/form'class ExampleForm extends SubmitForm {
render({ onChange, ...props }) {
const { formLoading, error, success } = this.statereturn ( {
this.reset()
if(onChange) onChange(values)
}}>
One must still have chaos in oneself to be able to give birth to a dancing star.
{error && `Error: ${error}`}
{success && `OK`}
)
}
}export default ExampleForm
```
```html
Input
Type in something...
Select
Free will
Unfree will
Please select...
TextArea
One must still have chaos in oneself to be able to give birth to a dancing star.
Multiple row input...
Submit Data```
### **FormGroup**
The form group is used to represent a logical combination of a label, input, help text and validation error message. The _FormGroup_ component generates `id` and `hid` values and passes them to children components in the context.
`_depackForm.FormGroupProps`: Options for the FormGroup component.
| Name | Type | Description |
| -------------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| label | string | The label to display for the group. |
| className | string | The additional class name to add to `form-group`. |
| labelClassName | string | The additional class name to add to the label. |
| col | string | If any of the `col` properties are passed (e.g., `col-12`, `col-sm-8`, _etc_), they will be set on the label. |
| row | boolean | Whether the group should be displayed in a row. Children must manually be wrapped in `div`s with `col` classes. Adds the `col-form-label` class to the label and the `row` class to the group. |
| form-row | boolean | Same as `row`, but adds the `form-row` class to the group. |
| details | boolean | Whether to display the group in `details` block. |
| help | string | The help text to show in `<small className="form-text text-muted">{help}</small>`. To support validation with `valid` and `invalid` classes, set help on inputs rather than group. |```jsx
import Form, { FormGroup, Input } from '@depack/form'const Example = () => (
)
```
```html
What is your name?
Your name, your name, what is your name?
```
### **Input**
The input is a one-line entry field.
`_depackForm.InputProps`: The rest is all other options to be passed to the input element. When compiling with _Depack_, the props must be added like ``.
| Name | Type | Description | Default |
| ----------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| required | boolean | Whether this is a required field. | - |
| name | string | The input name. | - |
| placeholder | string | The input placeholder. | - |
| file | boolean | Whether the input is for selecting files. | - |
| value | string | The initial value. | - |
| className | string | The additional class name to add to `form-control` and `form-control-file`. | - |
| col | string | If any of the `col` properties are passed (e.g., `col-12`, `col-sm-8`, _etc_), the _Form_ will create a `div` wrapper around the input with the column class. | - |
| type | string | The input type. | `text` |
| help | string | The help text to show under the input. Supports validation classes. | - |
| invalid | boolean | Adds the `invalid-feedback` class to help text. | - |
| valid | boolean | Adds the `valid-feedback` class to help text. | - |```jsx
import { Input } from '@depack/form'const Example = () => (
)
```
```html```
### **Select**
This element present the values to select from.
`_depackForm.SelectProps`: Options for the Select component.
| Name | Type | Description |
| ----------- | -------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| required | boolean | Whether this is a required field. |
| name | string | The select name. |
| value | string | The initial value. |
| col | string | If any of the `col` properties are passed (e.g., `col-12`, `col-sm-8`, _etc_), the _Form_ will create a `div` wrapper around the select with the column class. |
| className | string | The additional class name to add to `custom-select`. |
| defaultText | ?string | The default option's text. Pass `null` to disable the default option. |
| options | !Array<{ value: *, title: string }> | The array with options to render inside of the `select` element. |```jsx
import { Select } from '@depack/form'const Example = () => (
)
```
```html
hello
world```
### **Textarea**
The input field with multiple lines. The child of the component will set the initial value inside of the textarea.
`_depackForm.TextAreaProps`: Options for the TextAreaProps component.
| Name | Type | Description | Default |
| ----------- | ---------------- | --------------------------------------- | ------- |
| required | boolean | Whether this is a required field. | - |
| name | string | The textarea name. | - |
| placeholder | string | The textarea placeholder. | - |
| rows | number | How many rows should the textarea have. | `3` |```jsx
import { TextArea } from '@depack/form'const Example = () => (
Hello World
)
```
```html
Hello World
```### **SubmitForm**
This class extends the `Preact.Component` and implements the `submit` method which will send the data to the server and await for the response while setting the `formLoading` property of the state to `true`. The `error` and `success` properties will also be set upon the arrival of data, with the JSON response being used to extract the error. The `submitFinish` callback can be used to receive the result of the form submission. Components implementing this abstract class must write their own render method.
`_depackForm.SubmitFormProps`: Options for the SubmitForm component.
| Name | Type | Description |
| ------------ | ----------------------------------- | --------------------------------------------------------------------------------- |
| __path*__ | string | The path where to send data. |
| submitFinish | (arg0: Object) => !Promise | The callback after the data has been sent with possible response from the server. |`_depackForm.SubmitFormState`: The state structure for the SubmitForm.
| Name | Type | Description |
| ---------------- | ----------------- | ------------------------------------------------- |
| __formLoading*__ | boolean | Whether the data has been sent for submission. |
| __error*__ | ?string | The error returned by the server. |
| __success*__ | ?boolean | Whether the form has been submitted successfully. |```jsx
import Form, { SubmitForm, Input } from '@depack/form'class DataForm extends SubmitForm {
render() {
const { error, success, formLoading } = this.state
return (
{error && `Error: ${error}`}
{success && 'Success!'}
Submit
)
}
}
const Example = () => (
{
console.log('hooray!')
}} />
)
```
```html
Submit
```####
reset(): void
Resets the `error` and `success` properties of the form.
### **SubmitButton**
The button that can be placed inside the form and used for submission since it has `type="submit"` property. It also has the `loading` property to disable the button and show the spinning wheel indicator.
`_depackForm.SubmitButtonProps`: Options for the SubmitButton component.
| Name | Type | Description | Default |
| ---------------- | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- |
| loading | boolean | Whether the button should display as loading. | `false` |
| loadingText | string | The text to show during the loading progress. | - |
| __confirmText*__ | string | The text for the normal state. | - |
| className | string | The class name, such as `btn-lg`. | - |
| type | string | The type of the button to add to the class as `btn-{type}`. One of `('primary' \| 'secondary' \| 'success' \| 'danger' \| 'warning' \| 'info' \| 'light' \| 'dark')`. | `primary` |
| outline | boolean | Display the outline style of the button via setting the `btn-outline-{type}` class. | `false` |
| disabled | boolean | Whether the button is disabled. It becomes disabled when the form is loading by default. | `false` |```jsx
import { SubmitButton } from '@depack/form'const Example = ({ formLoading }) => (
)
```
```html
Add Data
```## Custom Components
To implement a custom component, one must write a class component that would report its initial value in `componentDidMount` method via the `onChange` method that it receives in the context. Overall, there are 4 properties that a component can receive in the context:
- `id`: If placed in the _FormGroup_, this will be set to the ID that the component should set on the input so that the label can focus on it on click.
- `hid`: If placed in the _FormGroup_, this will be set to auto-generated value for the help field.
- `values`: This is the overall values hash containing all the values of the fields in the form. It is set by the _Form_ parent component.
- `onChange`: This is the callback set by the _Form_ to report changes to the values of the component. It must also be fired after the component is mounted to set its initial model value in the form (i.e. update the `values` field).The components are controlled which means their values are set via the model, and are contained in the `values` context property. Whenever an update is needed, the `onChange` method has to be fired. To allow server-side rendering of the component when the initial value is not going to be reported to the _Form_ via the `componentDidMount`, it must be set manually after checking if `values` contain the name of the component. If the component for some reason is going to be used also outside of the form, the `values` must be defaulted to `{}`.
Here is an example of the _Input_ component which accounts for all the above points:
```jsx
import { Component } from 'preact'
import { shouldComponentUpdate, getClasses } from './lib'
import Help from './help'export default class Input extends Component {
constructor() {
super()
/** @type {!_depackForm.InputProps} */
this.props = this.props
}
shouldComponentUpdate(newProps, __, newContext) {
const res = shouldComponentUpdate.call(this, newProps, newContext)
return res
}
componentDidMount() {
const { value, name } = this.props
const { onChange } = this.context
if (value !== undefined && onChange) onChange(name, value)
}
/**
* Triggers the onchange event on the form.
* @param {string} value
*/
onChange(value) {
this.context.onChange(this.props.name, value)
}
/**
* @param {!_depackForm.InputProps} [props]
*/
render({
required, name, placeholder, type = 'text', file, value, className,
invalid, valid, help, ...props
}) {
const { colClasses, prop } = getClasses(props)
const c = [
`form-control${file ? '-file' : ''}`, className,
invalid ? 'is-invalid' : null,
valid ? 'is-valid' : null,
]
.filter(Boolean).join(' ')
const { hid, id, values = {} } = this.context
const rendered = name in values // for SSR
const input = ( {
this.onChange(e.currentTarget.value)
}}
{...prop}
/>)
if (colClasses.length) {
const he = help ? () : null
return ()
{input}
{he}
}
return input
}
}/**
* @suppress {nonStandardJsDocs}
* @typedef {import('../types').InputProps} _depackForm.InputProps
*/
```## Copyright
© Art Deco for Depack 2020
Tech Nation Visa Sucks