guestbell-forms 3.0.85 → 3.0.87

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/build/components/base/input/BaseInput.js +2 -2
  2. package/build/components/base/input/BaseInput.js.map +1 -1
  3. package/build/components/button/Button.js +21 -11
  4. package/build/components/button/Button.js.map +1 -1
  5. package/build/components/form/Form.d.ts +4 -2
  6. package/build/components/form/Form.js +36 -27
  7. package/build/components/form/Form.js.map +1 -1
  8. package/build/components/form/FormContext.d.ts +1 -14
  9. package/build/components/form/FormContext.js.map +1 -1
  10. package/build/components/form/FormValidationContext.d.ts +27 -0
  11. package/build/components/form/FormValidationContext.js +15 -0
  12. package/build/components/form/FormValidationContext.js.map +1 -0
  13. package/build/components/form/FormValidationSummary.d.ts +3 -3
  14. package/build/components/form/FormValidationSummary.js +3 -3
  15. package/build/components/form/FormValidationSummary.js.map +1 -1
  16. package/build/components/form/withFormContext.d.ts +1 -1
  17. package/build/components/form/withFormContext.js.map +1 -1
  18. package/build/components/form/withFormValidationContext.d.ts +3 -0
  19. package/build/components/form/withFormValidationContext.js +25 -0
  20. package/build/components/form/withFormValidationContext.js.map +1 -0
  21. package/build/components/submit/Submit.d.ts +3 -2
  22. package/build/components/submit/Submit.js +5 -5
  23. package/build/components/submit/Submit.js.map +1 -1
  24. package/build/dist/report.html +1 -1
  25. package/package.json +1 -1
  26. package/src/lib/components/base/input/BaseInput.tsx +1 -4
  27. package/src/lib/components/button/Button.tsx +16 -13
  28. package/src/lib/components/form/Form.tsx +48 -32
  29. package/src/lib/components/form/FormContext.ts +4 -18
  30. package/src/lib/components/form/FormValidationContext.ts +40 -0
  31. package/src/lib/components/form/FormValidationSummary.tsx +12 -7
  32. package/src/lib/components/form/withFormContext.tsx +14 -11
  33. package/src/lib/components/form/withFormValidationContext.tsx +29 -0
  34. package/src/lib/components/submit/Submit.tsx +9 -7
@@ -63,19 +63,22 @@ export interface ButtonState {
63
63
  }
64
64
 
65
65
  const DefaultButtonComponent: React.FC<ButtonComponentProps> = React.forwardRef(
66
- (props, ref) => (
67
- <button
68
- ref={ref}
69
- // tslint:disable-next-line:no-any
70
- {...((props.buttonProps ? props.buttonProps : {}) as any)}
71
- {...(props.id && { id: props.id })}
72
- className={props.className}
73
- onClick={props.onClick}
74
- style={props.style}
75
- >
76
- {props.children}
77
- </button>
78
- )
66
+ (props, ref) => {
67
+ const { disabled, ...rest } = props.buttonProps ?? {};
68
+ return (
69
+ <button
70
+ ref={ref}
71
+ // tslint:disable-next-line:no-any
72
+ {...(rest as any)}
73
+ {...(props.id && { id: props.id })}
74
+ className={props.className}
75
+ onClick={props.onClick}
76
+ style={props.style}
77
+ >
78
+ {props.children}
79
+ </button>
80
+ );
81
+ }
79
82
  );
80
83
 
81
84
  export class Button extends React.PureComponent<ButtonProps, ButtonState> {
@@ -4,13 +4,17 @@ import * as React from 'react';
4
4
  // Misc
5
5
  import {
6
6
  FormContextState,
7
- FormComponentContextState,
8
7
  FormContextProvider,
9
8
  ComponentsDict,
10
- FormComponentValidationContextStateBase,
11
9
  } from './FormContext';
12
10
  import { withThemeContext } from '../themeProvider/withThemeContext';
13
11
  import { ThemeContextProps } from '../themeProvider/ThemeContext';
12
+ import {
13
+ FormComponentContextState,
14
+ FormComponentValidationContextStateBase,
15
+ FormValidationContextProvider,
16
+ FormValidationContextState,
17
+ } from './FormValidationContext';
14
18
 
15
19
  export type FormProps = React.PropsWithChildren<
16
20
  ThemeContextProps & {
@@ -28,7 +32,8 @@ export type FormProps = React.PropsWithChildren<
28
32
  >;
29
33
 
30
34
  export interface FormState {
31
- contextState: FormContextState;
35
+ formContext: FormContextState;
36
+ formValidationContext: FormValidationContextState;
32
37
  }
33
38
 
34
39
  export class Form extends React.PureComponent<FormProps, FormState> {
@@ -46,13 +51,15 @@ export class Form extends React.PureComponent<FormProps, FormState> {
46
51
  this.disableComponents = this.disableComponents.bind(this);
47
52
  this.enableComponents = this.enableComponents.bind(this);
48
53
  this.state = {
49
- contextState: {
54
+ formContext: {
50
55
  subscribe: this.subscribe,
51
56
  unSubscribe: this.unSubscribe,
52
- isFormValid: true,
53
57
  updateCallback: this.updateCallback,
54
58
  disableComponents: this.disableComponents,
55
59
  enableComponents: this.enableComponents,
60
+ },
61
+ formValidationContext: {
62
+ isFormValid: true,
56
63
  components: {},
57
64
  },
58
65
  };
@@ -64,12 +71,12 @@ export class Form extends React.PureComponent<FormProps, FormState> {
64
71
  snapshot?: any
65
72
  ): void {
66
73
  if (
67
- this.state.contextState?.isFormValid !==
68
- prevState?.contextState?.isFormValid
74
+ this.state.formValidationContext?.isFormValid !==
75
+ prevState?.formValidationContext?.isFormValid
69
76
  ) {
70
- const componentsWithErrors = this.state.contextState
71
- ? Object.keys(this.state.contextState.components)
72
- .map((key) => this.state.contextState.components[key])
77
+ const componentsWithErrors = this.state.formContext
78
+ ? Object.keys(this.state.formValidationContext.components)
79
+ .map((key) => this.state.formValidationContext.components[key])
73
80
  .filter((component) => {
74
81
  if (!component.validation.isValid && !component.validation.name) {
75
82
  console.warn(
@@ -81,7 +88,7 @@ export class Form extends React.PureComponent<FormProps, FormState> {
81
88
  })
82
89
  : [];
83
90
  this.props.onValidChanged?.(
84
- this.state.contextState?.isFormValid,
91
+ this.state.formValidationContext?.isFormValid,
85
92
  componentsWithErrors
86
93
  );
87
94
  }
@@ -89,7 +96,7 @@ export class Form extends React.PureComponent<FormProps, FormState> {
89
96
 
90
97
  public disableComponents() {
91
98
  const components = {
92
- ...this.state.contextState.components,
99
+ ...this.state.formValidationContext.components,
93
100
  ...this.props.extraComponents,
94
101
  };
95
102
  Object.keys(components).forEach((key) => {
@@ -103,7 +110,7 @@ export class Form extends React.PureComponent<FormProps, FormState> {
103
110
 
104
111
  public enableComponents() {
105
112
  const components = {
106
- ...this.state.contextState.components,
113
+ ...this.state.formValidationContext.components,
107
114
  ...this.props.extraComponents,
108
115
  };
109
116
  Object.keys(components).forEach((key) => {
@@ -117,7 +124,7 @@ export class Form extends React.PureComponent<FormProps, FormState> {
117
124
 
118
125
  public touchAll() {
119
126
  const components = {
120
- ...this.state.contextState.components,
127
+ ...this.state.formValidationContext.components,
121
128
  ...this.props.extraComponents,
122
129
  };
123
130
  Object.keys(components).forEach((key) => {
@@ -131,7 +138,7 @@ export class Form extends React.PureComponent<FormProps, FormState> {
131
138
 
132
139
  public unTouchAll() {
133
140
  const components = {
134
- ...this.state.contextState.components,
141
+ ...this.state.formValidationContext.components,
135
142
  ...this.props.extraComponents,
136
143
  };
137
144
  Object.keys(components).forEach((key) => {
@@ -144,6 +151,7 @@ export class Form extends React.PureComponent<FormProps, FormState> {
144
151
  }
145
152
 
146
153
  public render() {
154
+ const validationContext = this.mergeContext();
147
155
  return (
148
156
  <this.props.component
149
157
  {...(this.props.id && { id: this.props.id })}
@@ -154,24 +162,26 @@ export class Form extends React.PureComponent<FormProps, FormState> {
154
162
  }`}
155
163
  onSubmit={this.onSubmit}
156
164
  >
157
- <FormContextProvider value={this.mergeContext()}>
158
- {this.props.children}
165
+ <FormContextProvider value={this.state.formContext}>
166
+ <FormValidationContextProvider value={validationContext}>
167
+ {this.props.children}
168
+ </FormValidationContextProvider>
159
169
  </FormContextProvider>
160
170
  </this.props.component>
161
171
  );
162
172
  }
163
173
 
164
- private mergeContext(): FormContextState {
174
+ private mergeContext(): FormValidationContextState {
165
175
  if (!this.props.extraComponents) {
166
- return this.state.contextState;
176
+ return this.state.formValidationContext;
167
177
  }
168
178
  const isFormValid =
169
- this.state.contextState.isFormValid &&
179
+ this.state.formValidationContext.isFormValid &&
170
180
  this.getIsFormValid(this.props.extraComponents);
171
181
  return {
172
- ...this.state.contextState,
182
+ ...this.state.formValidationContext,
173
183
  components: {
174
- ...this.state.contextState.components,
184
+ ...this.state.formValidationContext.components,
175
185
  ...this.props.extraComponents,
176
186
  },
177
187
  isFormValid,
@@ -191,18 +201,19 @@ export class Form extends React.PureComponent<FormProps, FormState> {
191
201
  this.setState((previousState) => {
192
202
  let components = Object.assign(
193
203
  {},
194
- previousState.contextState.components
204
+ previousState.formValidationContext.components
195
205
  );
196
206
  if (componentState) {
197
207
  components[componentId] = componentState;
198
208
  }
199
209
  let isFormValid = this.getIsFormValid(components);
200
210
  return {
201
- contextState: {
202
- ...previousState.contextState,
211
+ formValidationContext: {
212
+ ...previousState.formValidationContext,
203
213
  components,
204
214
  isFormValid,
205
215
  },
216
+ formContext: previousState.formContext,
206
217
  };
207
218
  });
208
219
  }
@@ -213,13 +224,14 @@ export class Form extends React.PureComponent<FormProps, FormState> {
213
224
  this.setState((previousState) => {
214
225
  let components = Object.assign(
215
226
  {},
216
- previousState.contextState.components
227
+ previousState.formValidationContext.components
217
228
  );
218
229
  delete components[componentId];
219
230
  let isFormValid = this.getIsFormValid(components);
220
231
  return {
221
- contextState: {
222
- ...previousState.contextState,
232
+ formContext: previousState.formContext,
233
+ formValidationContext: {
234
+ ...previousState.formValidationContext,
223
235
  components,
224
236
  isFormValid,
225
237
  },
@@ -229,7 +241,7 @@ export class Form extends React.PureComponent<FormProps, FormState> {
229
241
  }
230
242
 
231
243
  private getIsFormValid(
232
- components: ComponentsDict = this.state.contextState.components,
244
+ components: ComponentsDict = this.state.formValidationContext.components,
233
245
  initialValid: boolean = true
234
246
  ): boolean {
235
247
  let isFormValid = initialValid;
@@ -247,7 +259,10 @@ export class Form extends React.PureComponent<FormProps, FormState> {
247
259
  componentState: FormComponentValidationContextStateBase
248
260
  ) {
249
261
  this.setState((previousState) => {
250
- let components = Object.assign({}, previousState.contextState.components);
262
+ let components = Object.assign(
263
+ {},
264
+ previousState.formValidationContext.components
265
+ );
251
266
  const previousComponent = components[componentId];
252
267
  if (
253
268
  previousComponent &&
@@ -272,11 +287,12 @@ export class Form extends React.PureComponent<FormProps, FormState> {
272
287
  }
273
288
  let isFormValid = this.getIsFormValid(components);
274
289
  return {
275
- contextState: {
276
- ...this.state.contextState,
290
+ formValidationContext: {
291
+ ...this.state.formValidationContext,
277
292
  components,
278
293
  isFormValid,
279
294
  },
295
+ formContext: previousState.formContext,
280
296
  };
281
297
  });
282
298
  }
@@ -1,27 +1,15 @@
1
1
  import * as React from 'react';
2
- import { ValidationError, ComponentApi } from '../base/input';
3
2
  import { PartialBy } from '../utils/Typescript';
3
+ import {
4
+ FormComponentContextState,
5
+ FormComponentValidationContextStateBase,
6
+ } from './FormValidationContext';
4
7
 
5
8
  export type OmitFormContext<P extends FormContextProps> = PartialBy<
6
9
  P,
7
10
  'formContext'
8
11
  >;
9
12
 
10
- export interface FormComponentValidationContextStateBase {
11
- isValid: boolean;
12
- errors: ValidationError[];
13
- }
14
-
15
- export interface FormComponentValidationContextState
16
- extends FormComponentValidationContextStateBase {
17
- name: JSX.Element | string;
18
- }
19
-
20
- export interface FormComponentContextState {
21
- validation: FormComponentValidationContextState;
22
- componentApi: ComponentApi;
23
- }
24
-
25
13
  export interface FormContextProps {
26
14
  formContext?: FormContextState;
27
15
  }
@@ -36,14 +24,12 @@ export interface FormContextState {
36
24
  componentState: FormComponentContextState
37
25
  ) => void;
38
26
  unSubscribe: (componentId: string) => void;
39
- isFormValid: boolean;
40
27
  updateCallback: (
41
28
  componentId: string,
42
29
  newComponentState: FormComponentValidationContextStateBase
43
30
  ) => void;
44
31
  disableComponents: () => void;
45
32
  enableComponents: () => void;
46
- components: ComponentsDict;
47
33
  }
48
34
 
49
35
  const FormContext = React.createContext<FormContextState | undefined>(
@@ -0,0 +1,40 @@
1
+ import * as React from 'react';
2
+ import { ValidationError, ComponentApi } from '../base/input';
3
+ import { PartialBy } from '../utils/Typescript';
4
+
5
+ export type OmitFormValidationContext<P extends FormValidationContextProps> =
6
+ PartialBy<P, 'formValidationContext'>;
7
+
8
+ export interface FormComponentValidationContextStateBase {
9
+ isValid: boolean;
10
+ errors: ValidationError[];
11
+ }
12
+
13
+ export interface FormComponentValidationContextState
14
+ extends FormComponentValidationContextStateBase {
15
+ name: JSX.Element | string;
16
+ }
17
+
18
+ export interface FormComponentContextState {
19
+ validation: FormComponentValidationContextState;
20
+ componentApi: ComponentApi;
21
+ }
22
+
23
+ export interface FormValidationContextProps {
24
+ formValidationContext?: FormValidationContextState;
25
+ }
26
+
27
+ export type ComponentsDict = {
28
+ [componentId: string]: FormComponentContextState;
29
+ };
30
+
31
+ export interface FormValidationContextState {
32
+ isFormValid: boolean;
33
+ components: ComponentsDict;
34
+ }
35
+
36
+ const FormValidationContext = React.createContext<
37
+ FormValidationContextState | undefined
38
+ >(undefined);
39
+ export const FormValidationContextProvider = FormValidationContext.Provider;
40
+ export const FormValidationContextConsumer = FormValidationContext.Consumer;
@@ -1,11 +1,14 @@
1
1
  import * as React from 'react';
2
2
  import guid from '../utils/Guid';
3
- import { withFormContext } from './withFormContext';
4
- import { FormContextProps, FormComponentContextState } from './FormContext';
3
+ import {
4
+ FormValidationContextProps,
5
+ FormComponentContextState,
6
+ } from './FormValidationContext';
5
7
  import { Button } from './../button/Button';
6
8
  import classNames from 'classnames';
7
9
 
8
10
  import * as ArrowIcon from 'material-design-icons/maps/svg/production/ic_my_location_24px.svg';
11
+ import { withFormValidationContext } from './withFormValidationContext';
9
12
 
10
13
  export interface FormValidationSummaryComponentProps {
11
14
  componentsWithErrors: FormComponentContextState[];
@@ -20,7 +23,7 @@ export type FormValidationSummaryProps = {
20
23
  Component?:
21
24
  | React.ComponentType<FormValidationSummaryComponentProps>
22
25
  | React.FC<FormValidationSummaryComponentProps>;
23
- } & FormContextProps;
26
+ } & FormValidationContextProps;
24
27
 
25
28
  export interface FormValidationSummaryState {}
26
29
 
@@ -86,9 +89,9 @@ export class FormValidationSummaryRaw extends React.PureComponent<
86
89
  public componentId = guid();
87
90
 
88
91
  public render() {
89
- const componentsWithErrors = this.props.formContext
90
- ? Object.keys(this.props.formContext.components)
91
- .map((key) => this.props.formContext.components[key])
92
+ const componentsWithErrors = this.props.formValidationContext
93
+ ? Object.keys(this.props.formValidationContext.components)
94
+ .map((key) => this.props.formValidationContext.components[key])
92
95
  .filter((component) => {
93
96
  if (!component.validation.isValid && !component.validation.name) {
94
97
  console.warn(
@@ -119,4 +122,6 @@ export class FormValidationSummaryRaw extends React.PureComponent<
119
122
  }
120
123
 
121
124
  export const FormValidationSummary =
122
- withFormContext<FormValidationSummaryProps>(FormValidationSummaryRaw);
125
+ withFormValidationContext<FormValidationSummaryProps>(
126
+ FormValidationSummaryRaw
127
+ );
@@ -2,19 +2,22 @@ import * as React from 'react';
2
2
  import { FormContextConsumer, FormContextProps } from './FormContext';
3
3
 
4
4
  export function withFormContext<P extends FormContextProps>(
5
- Component: React.ComponentClass<P>
5
+ Component: React.ComponentClass<P> | React.ForwardRefExoticComponent<P>
6
6
  ): React.ForwardRefExoticComponent<
7
- React.RefAttributes<InstanceType<typeof Component>> &
7
+ React.RefAttributes<
8
+ React.ComponentClass<P> | React.ForwardRefExoticComponent<P>
9
+ > &
8
10
  React.PropsWithoutRef<React.PropsWithChildren<P>>
9
11
  > {
10
- const WithFormContext = React.forwardRef<InstanceType<typeof Component>, P>(
11
- (props, ref) => {
12
- return (
13
- <FormContextConsumer>
14
- {value => <Component ref={ref} {...props} formContext={value} />}
15
- </FormContextConsumer>
16
- );
17
- }
18
- );
12
+ const WithFormContext = React.forwardRef<
13
+ React.ComponentClass<P> | React.ForwardRefExoticComponent<P>,
14
+ P
15
+ >((props, ref) => {
16
+ return (
17
+ <FormContextConsumer>
18
+ {(value) => <Component ref={ref} {...props} formContext={value} />}
19
+ </FormContextConsumer>
20
+ );
21
+ });
19
22
  return WithFormContext;
20
23
  }
@@ -0,0 +1,29 @@
1
+ import * as React from 'react';
2
+ import {
3
+ FormValidationContextConsumer,
4
+ FormValidationContextProps,
5
+ } from './FormValidationContext';
6
+
7
+ export function withFormValidationContext<P extends FormValidationContextProps>(
8
+ Component: React.ComponentClass<P>
9
+ ): React.ForwardRefExoticComponent<
10
+ React.RefAttributes<InstanceType<typeof Component>> &
11
+ React.PropsWithoutRef<React.PropsWithChildren<P>>
12
+ > {
13
+ const WithFormValidationContext = React.forwardRef<
14
+ InstanceType<typeof Component>,
15
+ P
16
+ >((props, ref) => {
17
+ return (
18
+ <FormValidationContextConsumer>
19
+ {(value) => {
20
+ console.log('value', value);
21
+ return (
22
+ <Component ref={ref} {...props} formValidationContext={value} />
23
+ );
24
+ }}
25
+ </FormValidationContextConsumer>
26
+ );
27
+ });
28
+ return WithFormValidationContext;
29
+ }
@@ -7,10 +7,11 @@ import {
7
7
  defaultBaseTranslations,
8
8
  } from '../base/input/BaseInput';
9
9
  import { Button, ButtonProps } from '../button/Button';
10
- import { withFormContext } from '../form/withFormContext';
11
10
  import { FormValidationSummaryRaw } from './../form/FormValidationSummary';
12
11
  import SubmitValidationSummary from './subComponents/SubmitValidationSummary';
13
12
  import { withThemeContext } from '../themeProvider/withThemeContext';
13
+ import { withFormValidationContext } from '../form/withFormValidationContext';
14
+ import { FormValidationContextProps } from '../form/FormValidationContext';
14
15
 
15
16
  // Misc
16
17
  export const defaultSubmitTranslations = {
@@ -27,7 +28,7 @@ export type SubmitProps = BaseInputProps<never, SubmitTranslations> &
27
28
  validateForm?: boolean;
28
29
  disabledTitle?: string;
29
30
  showValidationSummaryTooltip?: boolean;
30
- };
31
+ } & FormValidationContextProps;
31
32
 
32
33
  export interface SubmitState extends BaseInputState {}
33
34
 
@@ -70,7 +71,8 @@ export class SubmitRaw extends BaseInput<
70
71
  this.props.tooltip
71
72
  ? this.props.tooltip
72
73
  : this.props.showValidationSummaryTooltip &&
73
- !this.props.formContext.isFormValid && (
74
+ this.props.formValidationContext &&
75
+ !this.props.formValidationContext.isFormValid && (
74
76
  <FormValidationSummaryRaw
75
77
  title={translations.hangOn}
76
78
  footer={translations.needsFixing}
@@ -78,7 +80,7 @@ export class SubmitRaw extends BaseInput<
78
80
  headerClassName="submitValidationSummary__header"
79
81
  footerClassName="submitValidationSummary__footer"
80
82
  Component={SubmitValidationSummary}
81
- formContext={this.props.formContext}
83
+ formValidationContext={this.props.formValidationContext}
82
84
  />
83
85
  )
84
86
  }
@@ -103,8 +105,8 @@ export class SubmitRaw extends BaseInput<
103
105
  const disabled = this.getDisabled();
104
106
  return disabled
105
107
  ? disabled
106
- : this.props.validateForm && this.props.formContext
107
- ? !this.props.formContext.isFormValid
108
+ : this.props.validateForm && this.props.formValidationContext
109
+ ? !this.props.formValidationContext.isFormValid
108
110
  : false;
109
111
  }
110
112
  }
@@ -112,6 +114,6 @@ export class SubmitRaw extends BaseInput<
112
114
  export const Submit = withThemeContext<
113
115
  SubmitProps,
114
116
  InstanceType<typeof SubmitRaw>
115
- >(withFormContext<SubmitProps>(SubmitRaw), 'submit');
117
+ >(withFormValidationContext<SubmitProps>(SubmitRaw), 'submit');
116
118
 
117
119
  export default Submit;