|
| 1 | +--- |
| 2 | +name: Phone |
| 3 | +menu: Forms |
| 4 | +route: /forms/phone |
| 5 | +--- |
| 6 | + |
| 7 | +import { Playground } from '@playground'; |
| 8 | +import { Form } from 'react-final-form'; |
| 9 | +import { FFInputPhone } from './phone'; |
| 10 | +import { validate } from '../../validation'; |
| 11 | +import { renderFields } from '../../renderFields'; |
| 12 | + |
| 13 | +# Input |
| 14 | + |
| 15 | +A basic widget for getting the user input as a phone field. Keyboard and mouse can be used for providing or changing data. |
| 16 | + |
| 17 | +## When To Use |
| 18 | + |
| 19 | +- A user input in a form field is needed. |
| 20 | +- A search input is required. |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +`Inside your React project directory, run the following:` |
| 25 | + |
| 26 | +```js |
| 27 | +yarn add @tpr/forms |
| 28 | +``` |
| 29 | + |
| 30 | +`or with npm` |
| 31 | + |
| 32 | +```js |
| 33 | +npm install @tpr/forms |
| 34 | +``` |
| 35 | + |
| 36 | +`import items you wish to use from the library` |
| 37 | + |
| 38 | +```js |
| 39 | +import { Form, FFInputPhone } from '@tpr/forms'; |
| 40 | +``` |
| 41 | + |
| 42 | +## Examples |
| 43 | + |
| 44 | +[CodeSandbox](https://codesandbox.io) |
| 45 | + |
| 46 | +Example using field level validation |
| 47 | + |
| 48 | +<Playground> |
| 49 | + <Form onSubmit={console.log}> |
| 50 | + {({ handleSubmit }) => ( |
| 51 | + <form onSubmit={handleSubmit}> |
| 52 | + <FFInputPhone |
| 53 | + name="event_name" |
| 54 | + label="Event name" |
| 55 | + placeholder="07543 221 321" |
| 56 | + hint="Must be between 6 and 8 digits long" |
| 57 | + validate={(value) => (value ? undefined : 'Enter an event name')} |
| 58 | + inputWidth={5} |
| 59 | + cfg={{ my: 5 }} |
| 60 | + /> |
| 61 | + <button type="submit" style={{ display: 'none' }} children="Submit" /> |
| 62 | + </form> |
| 63 | + )} |
| 64 | + </Form> |
| 65 | +</Playground> |
| 66 | + |
| 67 | +<br /> |
| 68 | +Example using record level validation |
| 69 | + |
| 70 | +```js |
| 71 | +import { Form, validate, renderFields } from '@tpr/forms'; |
| 72 | +``` |
| 73 | + |
| 74 | +<Playground> |
| 75 | + {() => { |
| 76 | + const fields = [ |
| 77 | + { |
| 78 | + type: 'phone', |
| 79 | + name: 'event_place', |
| 80 | + label: 'Event place', |
| 81 | + hint: 'The word must be London exactly', |
| 82 | + error: (value, _fields) => { |
| 83 | + return value === 'London' ? undefined : 'Must be in London'; |
| 84 | + }, |
| 85 | + placeholder: 'add some text here...', |
| 86 | + inputWidth: 5, |
| 87 | + cfg: { my: 5 }, |
| 88 | + }, |
| 89 | + { |
| 90 | + type: 'phone', |
| 91 | + name: 'event_name', |
| 92 | + label: 'Event name', |
| 93 | + hint: 'Cannot be empty', |
| 94 | + error: 'Enter an event name', |
| 95 | + placeholder: 'add some text here...', |
| 96 | + inputWidth: 5, |
| 97 | + cfg: { my: 5 }, |
| 98 | + }, |
| 99 | + ]; |
| 100 | + return ( |
| 101 | + <Form onSubmit={console.log} validate={validate(fields)}> |
| 102 | + {({ handleSubmit }) => ( |
| 103 | + <form onSubmit={handleSubmit}> |
| 104 | + {renderFields(fields)} |
| 105 | + <button |
| 106 | + type="submit" |
| 107 | + style={{ display: 'none' }} |
| 108 | + children="Submit" |
| 109 | + /> |
| 110 | + </form> |
| 111 | + )} |
| 112 | + </Form> |
| 113 | + ); |
| 114 | + }} |
| 115 | +</Playground> |
| 116 | + |
| 117 | +## API |
| 118 | + |
| 119 | +``` |
| 120 | +Accepted config props: FlexProps, SpaceProps |
| 121 | +``` |
| 122 | + |
| 123 | +### Props |
| 124 | + |
| 125 | +| Property | Required | Type | Description | |
| 126 | +| -------- | -------- | ------- | -------------------------------------------- | |
| 127 | +| cfg | false | object | FlexProps & SpaceProps | |
| 128 | +| disabled | false | boolean | Disable checkbox | |
| 129 | +| testId | false | string | data attribute for testers | |
| 130 | +| label | true | string | Checkbox description | |
| 131 | +| hint | false | string | More detailed description about the checkbox | |
0 commit comments