1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-12-24 00:01:36 +01:00

Core review docs fixes.

This commit is contained in:
Bogdan Bodnar 2019-09-30 11:26:06 +02:00
parent e4e28dbce4
commit 680871de62
No known key found for this signature in database
GPG Key ID: 49E4169AD3CA42B0
10 changed files with 80 additions and 10 deletions

View File

@ -1,2 +0,0 @@
`<ForisForm/>` is HOC which encapsulates entire form logic and provided children required props.
This component structure provides comfort API and allows to create typical Foris module forms easily.

View File

@ -11,6 +11,6 @@ const [value, setValue] = useState(false);
value={value}
label="Some label"
helpText="Read the small text!"
onChange={e => setValue(e.target.value)}
onChange={event =>setValue(event.target.value)}
/>
```

View File

@ -11,7 +11,7 @@ const [email, setEmail] = useState('Wrong email');
value={email}
label="Some label"
helpText="Read the small text!"
onChange={e => setEmail(e.target.value)}
onChange={event =>setEmail(event.target.value)}
/>
<button type="submit">Try to submit</button>
</form>

View File

@ -3,7 +3,7 @@ Bootstrap modal component.
it's required to have an element `<div id={"modal-container"}/>` somewhere on the page since modals are rendered in portals.
```js
<div id={"modal-container"}/>
<div id="modal-container"/>
```
I have no idea why example doesn't work here but you can investigate HTML code and Foris project.

View File

@ -12,6 +12,6 @@ const [value, setValue] = useState(42);
helpText="Read the small text!"
min='33'
max='54'
onChange={e => setValue(e.target.value)}
onChange={event =>setValue(event.target.value)}
/>
```

View File

@ -12,6 +12,6 @@ const [value, setValue] = useState('secret');
value={value}
label="Some password"
helpText="Read the small text!"
onChange={e => setValue(e.target.value)}
onChange={event =>setValue(event.target.value)}
/>
```

View File

@ -17,7 +17,7 @@ const [value, setValue] = useState(CHOICES[0].value);
value={value}
name='some-radio'
choices={CHOICES}
onChange={e => setValue(e.target.value)}
onChange={event =>setValue(event.target.value)}
/>
<p>Selected value: {value}</p>
</>

View File

@ -10,6 +10,6 @@ const [value, setValue] = useState('Bla bla');
value={value}
label="Some text"
helpText="Read the small text!"
onChange={e => setValue(e.target.value)}
onChange={event =>setValue(event.target.value)}
/>
```

View File

@ -0,0 +1,73 @@
`<ForisForm/>` is Higher-Order Component which encapsulates entire form logic and provides with children required props.
This component structure provides comfort API and allows to create typical Foris module forms easily.
## Example of usage of `<ForisForm/>`
You can pass more forms as children.
```js
<ForisForm
ws={ws}
forisConfig={{
endpoint: API_URLs.wan,
wsModule: "wan",
}}
prepData={prepData}
prepDataToSubmit={prepDataToSubmit}
validator={validator}
>
<WANForm />
<WAN6Form />
<MACForm />
</ForisForm>
```
### Example of children forms `props` usage
```js
export default function MACForm({
formData, formErrors, setFormValue, ...props
}) {
const macSettings = formData.mac_settings;
const errors = (formErrors || {}).mac_settings || {};
return (
<>
<h3>{_("MAC")}</h3>
<CheckBox
label={_("Custom MAC address")}
checked={macSettings.custom_mac_enabled}
helpText={HELP_TEXTS.custom_mac_enabled}
onChange={setFormValue(
(value) => ({ mac_settings: { custom_mac_enabled: { $set: value } } }),
)}
{...props}
/>
{macSettings.custom_mac_enabled
? (
<TextInput
label={_("MAC address")}
value={macSettings.custom_mac || ""}
helpText={HELP_TEXTS.custom_mac}
error={errors.custom_mac}
required
onChange={setFormValue(
(value) => ({ mac_settings: { custom_mac: { $set: value } } }),
)}
{...props}
/>
)
: null}
</>
);
}
```
The <ForisForm/> passes subsequent `props` to the child components.
| Prop | Type | Description |
|----------------|--------|----------------------------------------------------------------------------|
| `formData` | object | Data returned from API. |
| `formErrors` | object | Errors returned after validation via validator. |
| `setFormValue` | func | Function for data update. It takes update rule as arg (see example above). |
| `disabled` | bool | Flag to disable form elements (during updates or loadings e.t.c.). |

View File

@ -14,7 +14,6 @@ module.exports = {
},
{
name: "Foris forms",
content: "docs/forms.md",
components: [
"src/form/components/ForisForm.js",
"src/form/components/alerts.js",