|
| 1 | +# Linked Controls |
| 2 | + |
| 3 | +Reference implementation of linked form controls. Can be used as a starting boilerplate for |
| 4 | +your own controls with inlined validation errors. |
| 5 | + |
| 6 | +Link can be bound to the any form control consuming `value` and `onChange` props, so linked controls from this package may be used but not really required. |
| 7 | + |
| 8 | +```javascript |
| 9 | +<input {...$value.props} /> |
| 10 | +``` |
| 11 | + |
| 12 | +However, it's beneficial to create a custom form control wrappers encapsulating your markup patterns for a form layout and validation in order to take a full advantage of the value link. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | + npm install linked-controls --save-dev |
| 17 | + |
| 18 | +## List of controls |
| 19 | + |
| 20 | +### Text and number form fields |
| 21 | + |
| 22 | +##### `<Input type="text"/>`, `<TextArea />` |
| 23 | + |
| 24 | +Wrappers for standard `<input>` and `<textarea>` tags which can be directly bound to the string links. |
| 25 | + |
| 26 | +These wrappers will add `invalid` class to enclosed HTML element if an error is present in the link. |
| 27 | + |
| 28 | +```jsx |
| 29 | +<Input type="text" $value={ $link } /> |
| 30 | +<TextArea $value={ $link } /> |
| 31 | +``` |
| 32 | + |
| 33 | +##### `<NumberInput/>` |
| 34 | + |
| 35 | +A cross-browser implementation of *numeric input* tag. It has following differences compared to `<Input>`: |
| 36 | + |
| 37 | +- Keyboard input which obviously leads to invalid values (e.g. letters) are rejected. |
| 38 | +- Value is being always converted to valid number. |
| 39 | +- There are `integer` and `positive` boolean props controlling input rejection. They can be combined. |
| 40 | + |
| 41 | +`<NumberInput>` validates its value and adds `invalid` class to enclosed input element if it's not a number. |
| 42 | + |
| 43 | +```jsx |
| 44 | +<NumberInput $value={ $link } /> |
| 45 | +<NumberInput $value={ $link } integer={ true }/> |
| 46 | +<NumberInput $value={ $link } positive={ true }/> |
| 47 | +``` |
| 48 | + |
| 49 | +### Checkboxes |
| 50 | + |
| 51 | +##### `<Input type="checkbox" />` |
| 52 | + |
| 53 | +Wrapper for the standard `<input>`. Directly binds boolean value with `checkedLink` property. |
| 54 | + |
| 55 | +```jsx |
| 56 | +<Input type="text" $checked={ booleanLink } /> |
| 57 | +<Input type="text" $checked={ $array.contains( 'option' ) } /> |
| 58 | +``` |
| 59 | + |
| 60 | +##### `<Checkbox/>` |
| 61 | + |
| 62 | +Internally, it's `<div>` element which toggles `selected` class on click. |
| 63 | +Thus, it can be easily styled. |
| 64 | + |
| 65 | +By default, it has `checkbox` CSS class which can be overridden by passing `className` prop. |
| 66 | + |
| 67 | +It passes through anything else including `children`. |
| 68 | + |
| 69 | +```jsx |
| 70 | +<Checkbox $checked={ $boolean } /> |
| 71 | +<Checkbox $checked={ $array.contains( 'option' ) } /> |
| 72 | +``` |
| 73 | + |
| 74 | +### Radio Groups and Select list |
| 75 | + |
| 76 | +##### `<Select/>` |
| 77 | + |
| 78 | +Wrapper for standard `<select/>`. Regular `<option/>` tags must be used. All props are passed through. |
| 79 | + |
| 80 | +```jsx |
| 81 | +<Select $value={ linkToSelectedValue }> |
| 82 | + <option value="a">A</option> |
| 83 | + <option value="b">B</option> |
| 84 | +</Select> |
| 85 | +``` |
| 86 | + |
| 87 | +##### `<Input type="radio"/>` |
| 88 | + |
| 89 | +Wrapper for the standard `<input>`. Directly binds boolean value with `$checked` property. |
| 90 | + |
| 91 | +Can be directly bound to the link of any type with `$value` or `$checked` property. |
| 92 | + |
| 93 | +```jsx |
| 94 | +<label> |
| 95 | + A: |
| 96 | + <Input type="radio" $value={ $flag } value="a" /> |
| 97 | +</label> |
| 98 | +<label> |
| 99 | + B: |
| 100 | + <Input type="radio" $checked={ $flag.equals( "b" ) } /> |
| 101 | +</label> |
| 102 | +``` |
| 103 | + |
| 104 | +##### `<Radio/>` |
| 105 | + |
| 106 | +Internally, it's `<div>` element which always sets `selected` class on click. Thus, |
| 107 | +it can be easily styled. |
| 108 | + |
| 109 | +By default, it has `radio` CSS class, which can be overridden by passing `className` prop. |
| 110 | +It passes through anything else, including `children`. |
| 111 | + |
| 112 | +It *must* be used in conjunction with `$link.equals( 'value' )` method. |
| 113 | + |
| 114 | +```jsx |
| 115 | +<label> |
| 116 | + A: |
| 117 | + <Radio $checked={ $flag.equals( 'a' ) } /> |
| 118 | +</label> |
| 119 | +<label> |
| 120 | + B: |
| 121 | + <Radio $checked={ $flag.equals( 'b' ) } /> |
| 122 | +</label> |
| 123 | +``` |
0 commit comments