From 5c14a4c38450cc05208cfc9e33ffc3e3574b12f0 Mon Sep 17 00:00:00 2001 From: Henrik Lang Date: Wed, 12 Feb 2020 14:55:07 +0100 Subject: [PATCH 1/2] extended `Form` component with form reset functionality - this functionality is needed by new Pharos component `FilterBar` and is also very useful in general when using this Form component --- src/components/Form/Example.md | 75 +++++++++++++++++++++++++++++++++- src/components/Form/index.tsx | 51 ++++++++++++++++++++++- 2 files changed, 123 insertions(+), 3 deletions(-) diff --git a/src/components/Form/Example.md b/src/components/Form/Example.md index 01d98b89..4379455e 100644 --- a/src/components/Form/Example.md +++ b/src/components/Form/Example.md @@ -137,8 +137,8 @@ The `Form` with validation rules ```jsx const { Form, Button, TextField } = require('precise-ui'); -
alert(JSON.stringify(e))} + alert(JSON.stringify(e))} validationRules={{ first: (value) => value && value.length > 10 ? 'Should be less than 10' : undefined, last: () => 'Always some error', @@ -160,3 +160,74 @@ const { Form, Button, TextField } = require('precise-ui');
``` + +Managed `Form` with default value and reset button + +```jsx +const { Form, Button, TextField, DropdownField } = require('precise-ui'); + +const filterDefinitions = { + timeRanges: { + label: 'Time Range', + name: 'timeRanges', + data: [ + { content: 'Last 7 days', key: '7' }, + { content: 'Last 30 days', key: '30' }, + { content: 'Last 60 days', key: '60' }, + { content: 'Last 90 days', key: '90' }, + { content: 'Last year', key: '365' }, + ], + }, + types: { + label: 'Types', + name: 'types', + multiple: true, + data: [ + { content: 'Statement', key: 'Statement' }, + { content: 'Invoice', key: 'Invoice' }, + { content: 'Credit', key: 'Credit' }, + ], + }, + statis: { + label: 'Status', + name: 'statis', + data: [ + { content: 'Open', key: 'Open' }, + { content: 'Paid', key: 'Paid' }, + ], + } +}; + +
alert(JSON.stringify(e.data))} onReset={e => { console.info(e, 'reset'); }} onChange={e => { console.info(e, 'CHANGE'); }}> +
+ First: +
+
+ +
+
+ Last: +
+
+ +
+
+ Filters: +
+
+ +
+
+
+ +
+
+
+ +
+
+ + +
+
+``` diff --git a/src/components/Form/index.tsx b/src/components/Form/index.tsx index 18f5a852..5842bfc1 100644 --- a/src/components/Form/index.tsx +++ b/src/components/Form/index.tsx @@ -19,6 +19,17 @@ export interface FormSubmitEvent { changed: boolean; } +export interface FormResetEvent { + /** + * The current resetted values of the form fields. + */ + value: FormValuesData; + /** + * Indicates whether the data has changed from the initial state. + */ + changed: boolean; +} + export interface FormChangeEvent { /** * The current values of the form fields. @@ -61,6 +72,10 @@ export interface FormProps extends StandardProps { * Event emitted when a field of the form changed. */ onChange?(e: FormChangeEvent): void; + /** + * Event emitted when the form is reset. + */ + onReset?(e: FormResetEvent): void; /** * Event emitted when the form is submitted. */ @@ -306,6 +321,39 @@ export class Form extends React.Component) => { + const { onChange, onReset, disabled } = this.props; + const { initial, controlled } = this.state; + const changed = true; + + if (!disabled) { + const proposed = { + ...initial, + }; + + if (!controlled) { + this.setValues(proposed, changed); + } + + if (typeof onChange === 'function') { + onChange({ + changed, + value: proposed, + }); + } + + if (typeof onReset === 'function') { + onReset({ + changed, + value: proposed, + }); + } + } + + e.preventDefault(); + return false; + }; + render() { const { value: _0, @@ -313,13 +361,14 @@ export class Form extends React.Component + {prompt && } {children} From b0ab93dce6308f984509d0844f4f4419adadc282 Mon Sep 17 00:00:00 2001 From: Henrik Lang Date: Wed, 12 Feb 2020 16:28:42 +0100 Subject: [PATCH 2/2] fixed typo --- src/components/Form/Example.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/Form/Example.md b/src/components/Form/Example.md index 4379455e..50841bbf 100644 --- a/src/components/Form/Example.md +++ b/src/components/Form/Example.md @@ -188,9 +188,9 @@ const filterDefinitions = { { content: 'Credit', key: 'Credit' }, ], }, - statis: { + statuses: { label: 'Status', - name: 'statis', + name: 'statuses', data: [ { content: 'Open', key: 'Open' }, { content: 'Paid', key: 'Paid' }, @@ -198,7 +198,7 @@ const filterDefinitions = { } }; -
alert(JSON.stringify(e.data))} onReset={e => { console.info(e, 'reset'); }} onChange={e => { console.info(e, 'CHANGE'); }}> + alert(JSON.stringify(e.data))} onReset={e => { console.info(e, 'reset'); }} onChange={e => { console.info(e, 'CHANGE'); }}>
First:
@@ -219,7 +219,7 @@ const filterDefinitions = {
- +