@ilo-org/react 0.7.8 → 0.8.1

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 (174) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/lib/cjs/components/Checkbox/Checkbox.js +28 -16
  3. package/lib/cjs/components/Checkbox/index.js +25 -3
  4. package/lib/cjs/components/DatePicker/DatePicker.js +21 -16
  5. package/lib/cjs/components/DatePicker/index.js +4 -4
  6. package/lib/cjs/components/Dropdown/Dropdown.js +19 -15
  7. package/lib/cjs/components/Dropdown/index.js +3 -3
  8. package/lib/cjs/components/Fieldset/Fieldset.js +33 -9
  9. package/lib/cjs/components/Fieldset/index.js +2 -2
  10. package/lib/cjs/components/FileUpload/FileUpload.js +29 -15
  11. package/lib/cjs/components/FileUpload/index.js +3 -3
  12. package/lib/cjs/components/Form/Form.js +8 -55
  13. package/lib/cjs/components/Form/index.js +10 -20
  14. package/lib/cjs/components/FormControl/FormControl.js +79 -0
  15. package/lib/cjs/components/FormControl/index.js +16 -0
  16. package/lib/cjs/components/Input/Input.js +1 -1
  17. package/lib/cjs/components/NumberPicker/NumberPicker.js +21 -16
  18. package/lib/cjs/components/NumberPicker/index.js +4 -4
  19. package/lib/cjs/components/Radio/Radio.js +34 -17
  20. package/lib/cjs/components/Radio/index.js +19 -4
  21. package/lib/cjs/components/TextInput/TextInput.js +30 -0
  22. package/lib/cjs/components/TextInput/index.js +52 -0
  23. package/lib/cjs/components/Textarea/Textarea.js +19 -19
  24. package/lib/cjs/components/Textarea/index.js +26 -5
  25. package/lib/cjs/components/Toggle/Toggle.js +33 -0
  26. package/lib/cjs/components/Toggle/index.js +51 -0
  27. package/lib/cjs/components/Tooltip/Tooltip.js +2 -2
  28. package/lib/cjs/components/index.js +10 -11
  29. package/lib/cjs/hooks/usePrevious.js +20 -0
  30. package/lib/cjs/index.js +10 -11
  31. package/lib/esm/components/Checkbox/Checkbox.js +31 -19
  32. package/lib/esm/components/Checkbox/index.js +27 -2
  33. package/lib/esm/components/DatePicker/DatePicker.js +22 -17
  34. package/lib/esm/components/DatePicker/index.js +4 -4
  35. package/lib/esm/components/Dropdown/Dropdown.js +20 -16
  36. package/lib/esm/components/Dropdown/index.js +3 -3
  37. package/lib/esm/components/Fieldset/Fieldset.js +31 -11
  38. package/lib/esm/components/Fieldset/index.js +1 -1
  39. package/lib/esm/components/FileUpload/FileUpload.js +31 -17
  40. package/lib/esm/components/FileUpload/index.js +3 -3
  41. package/lib/esm/components/Form/Form.js +10 -57
  42. package/lib/esm/components/Form/index.js +13 -20
  43. package/lib/esm/components/FormControl/FormControl.js +73 -0
  44. package/lib/esm/components/{ChoiceGroup → FormControl}/index.js +3 -6
  45. package/lib/esm/components/NumberPicker/NumberPicker.js +21 -16
  46. package/lib/esm/components/NumberPicker/index.js +4 -4
  47. package/lib/esm/components/Radio/Radio.js +34 -17
  48. package/lib/esm/components/Radio/index.js +21 -3
  49. package/lib/esm/components/TextInput/TextInput.js +28 -0
  50. package/lib/esm/components/TextInput/index.js +49 -0
  51. package/lib/esm/components/Textarea/Textarea.js +19 -19
  52. package/lib/esm/components/Textarea/index.js +28 -4
  53. package/lib/esm/components/Toggle/Toggle.js +31 -0
  54. package/lib/esm/components/Toggle/index.js +48 -0
  55. package/lib/esm/components/Tooltip/Tooltip.js +2 -2
  56. package/lib/esm/components/index.js +8 -5
  57. package/lib/esm/hooks/usePrevious.js +15 -0
  58. package/lib/esm/index.js +8 -5
  59. package/lib/types/react/src/components/Checkbox/Checkbox.args.d.ts +52 -0
  60. package/lib/types/react/src/components/Checkbox/Checkbox.d.ts +5 -3
  61. package/lib/types/react/src/components/Checkbox/Checkbox.props.d.ts +4 -13
  62. package/lib/types/react/src/components/Checkbox/index.d.ts +1 -0
  63. package/lib/types/react/src/components/DatePicker/DatePicker.d.ts +4 -3
  64. package/lib/types/react/src/components/DatePicker/DatePicker.props.d.ts +10 -43
  65. package/lib/types/react/src/components/Dropdown/Dropdown.d.ts +5 -3
  66. package/lib/types/react/src/components/Dropdown/Dropdown.props.d.ts +16 -42
  67. package/lib/types/react/src/components/Fieldset/Fieldset.d.ts +6 -0
  68. package/lib/types/react/src/components/Fieldset/Fieldset.props.d.ts +14 -21
  69. package/lib/types/react/src/components/FileUpload/FileUpload.d.ts +5 -3
  70. package/lib/types/react/src/components/FileUpload/FileUpload.props.d.ts +7 -44
  71. package/lib/types/react/src/components/Form/Form.args.d.ts +2 -0
  72. package/lib/types/react/src/components/Form/Form.d.ts +1 -2
  73. package/lib/types/react/src/components/Form/Form.props.d.ts +4 -111
  74. package/lib/types/react/src/components/Form/index.d.ts +1 -0
  75. package/lib/types/react/src/components/FormControl/FormControl.d.ts +9 -0
  76. package/lib/types/react/src/components/FormControl/FormControl.props.d.ts +55 -0
  77. package/lib/types/react/src/components/FormControl/index.d.ts +2 -0
  78. package/lib/types/react/src/components/FormElement/FormElement.props.d.ts +2 -2
  79. package/lib/types/react/src/components/Input/Input.props.d.ts +2 -2
  80. package/lib/types/react/src/components/NumberPicker/NumberPicker.d.ts +5 -3
  81. package/lib/types/react/src/components/NumberPicker/NumberPicker.props.d.ts +5 -42
  82. package/lib/types/react/src/components/Radio/Radio.args.d.ts +8 -0
  83. package/lib/types/react/src/components/Radio/Radio.d.ts +5 -3
  84. package/lib/types/react/src/components/Radio/Radio.props.d.ts +4 -14
  85. package/lib/types/react/src/components/Radio/index.d.ts +1 -0
  86. package/lib/types/react/src/components/TextInput/TextInput.args.d.ts +10 -0
  87. package/lib/types/react/src/components/TextInput/TextInput.d.ts +6 -0
  88. package/lib/types/react/src/components/TextInput/TextInput.props.d.ts +17 -0
  89. package/lib/types/react/src/components/TextInput/index.d.ts +2 -0
  90. package/lib/types/react/src/components/Textarea/Textarea.args.d.ts +10 -0
  91. package/lib/types/react/src/components/Textarea/Textarea.d.ts +5 -3
  92. package/lib/types/react/src/components/Textarea/Textarea.props.d.ts +17 -39
  93. package/lib/types/react/src/components/Textarea/index.d.ts +2 -0
  94. package/lib/types/react/src/components/Toggle/Toggle.args.d.ts +12 -0
  95. package/lib/types/react/src/components/Toggle/Toggle.d.ts +5 -0
  96. package/lib/types/react/src/components/Toggle/Toggle.props.d.ts +22 -0
  97. package/lib/types/react/src/components/Toggle/index.d.ts +3 -0
  98. package/lib/types/react/src/components/Tooltip/Tooltip.props.d.ts +4 -0
  99. package/lib/types/react/src/components/index.d.ts +1 -5
  100. package/lib/types/react/src/hooks/usePrevious.d.ts +2 -0
  101. package/lib/types/react/src/types/index.d.ts +57 -1
  102. package/package.json +20 -19
  103. package/src/components/Checkbox/Checkbox.args.ts +31 -9
  104. package/src/components/Checkbox/Checkbox.props.ts +5 -13
  105. package/src/components/Checkbox/Checkbox.tsx +78 -76
  106. package/src/components/Checkbox/index.ts +1 -0
  107. package/src/components/DatePicker/DatePicker.args.ts +6 -32
  108. package/src/components/DatePicker/DatePicker.props.ts +11 -51
  109. package/src/components/DatePicker/DatePicker.tsx +71 -110
  110. package/src/components/Dropdown/Dropdown.args.ts +42 -192
  111. package/src/components/Dropdown/Dropdown.props.ts +18 -49
  112. package/src/components/Dropdown/Dropdown.tsx +73 -62
  113. package/src/components/Fieldset/Fieldset.props.ts +14 -23
  114. package/src/components/Fieldset/Fieldset.tsx +75 -69
  115. package/src/components/FileUpload/FileUpload.args.ts +25 -27
  116. package/src/components/FileUpload/FileUpload.props.ts +8 -53
  117. package/src/components/FileUpload/FileUpload.tsx +103 -72
  118. package/src/components/Form/Form.args.ts +2 -184
  119. package/src/components/Form/Form.props.ts +4 -133
  120. package/src/components/Form/Form.tsx +17 -77
  121. package/src/components/Form/index.ts +1 -0
  122. package/src/components/FormControl/FormControl.props.ts +72 -0
  123. package/src/components/FormControl/FormControl.tsx +169 -0
  124. package/src/components/FormControl/index.ts +2 -0
  125. package/src/components/FormElement/FormElement.props.ts +2 -2
  126. package/src/components/Input/Input.props.ts +2 -2
  127. package/src/components/Navigation/Navigation.args.ts +2 -1
  128. package/src/components/NumberPicker/NumberPicker.args.ts +18 -26
  129. package/src/components/NumberPicker/NumberPicker.props.ts +7 -52
  130. package/src/components/NumberPicker/NumberPicker.tsx +73 -56
  131. package/src/components/Radio/Radio.args.ts +1 -7
  132. package/src/components/Radio/Radio.props.ts +6 -14
  133. package/src/components/Radio/Radio.tsx +90 -52
  134. package/src/components/Radio/index.ts +1 -0
  135. package/src/components/TextInput/TextInput.args.ts +75 -0
  136. package/src/components/TextInput/TextInput.props.ts +20 -0
  137. package/src/components/TextInput/TextInput.tsx +71 -0
  138. package/src/components/TextInput/index.ts +2 -0
  139. package/src/components/Textarea/Textarea.args.ts +19 -37
  140. package/src/components/Textarea/Textarea.props.ts +18 -46
  141. package/src/components/Textarea/Textarea.tsx +44 -59
  142. package/src/components/Textarea/index.ts +2 -0
  143. package/src/components/Toggle/Toggle.args.ts +62 -0
  144. package/src/components/Toggle/Toggle.props.ts +27 -0
  145. package/src/components/Toggle/Toggle.tsx +85 -0
  146. package/src/components/Toggle/index.ts +3 -0
  147. package/src/components/Tooltip/Tooltip.props.ts +5 -0
  148. package/src/components/Tooltip/Tooltip.tsx +2 -0
  149. package/src/components/index.ts +1 -5
  150. package/src/hooks/usePrevious.ts +15 -0
  151. package/src/types/forms.args.ts +288 -0
  152. package/src/types/index.ts +69 -1
  153. package/tsconfig.json +2 -1
  154. package/lib/cjs/components/ChoiceGroup/ChoiceGroup.js +0 -34
  155. package/lib/cjs/components/ChoiceGroup/index.js +0 -19
  156. package/lib/cjs/components/FormGroup/FormGroup.js +0 -58
  157. package/lib/cjs/components/FormGroup/index.js +0 -26
  158. package/lib/esm/components/ChoiceGroup/ChoiceGroup.js +0 -32
  159. package/lib/esm/components/FormGroup/FormGroup.js +0 -56
  160. package/lib/esm/components/FormGroup/index.js +0 -20
  161. package/lib/types/react/src/components/ChoiceGroup/ChoiceGroup.d.ts +0 -4
  162. package/lib/types/react/src/components/ChoiceGroup/ChoiceGroup.props.d.ts +0 -20
  163. package/lib/types/react/src/components/ChoiceGroup/index.d.ts +0 -1
  164. package/lib/types/react/src/components/FormGroup/FormGroup.d.ts +0 -4
  165. package/lib/types/react/src/components/FormGroup/FormGroup.props.d.ts +0 -81
  166. package/lib/types/react/src/components/FormGroup/index.d.ts +0 -1
  167. package/src/components/ChoiceGroup/ChoiceGroup.args.ts +0 -95
  168. package/src/components/ChoiceGroup/ChoiceGroup.props.ts +0 -25
  169. package/src/components/ChoiceGroup/ChoiceGroup.tsx +0 -54
  170. package/src/components/ChoiceGroup/index.ts +0 -1
  171. package/src/components/FormGroup/FormGroup.args.ts +0 -85
  172. package/src/components/FormGroup/FormGroup.props.ts +0 -102
  173. package/src/components/FormGroup/FormGroup.tsx +0 -73
  174. package/src/components/FormGroup/index.ts +0 -1
@@ -1,88 +1,94 @@
1
- import { Children, FC } from "react";
1
+ import { FC, createContext, useContext, useState } from "react";
2
2
  import classNames from "classnames";
3
3
  import useGlobalSettings from "../../hooks/useGlobalSettings";
4
- import { FieldsetChild, FieldsetProps } from "./Fieldset.props";
4
+ import { FieldsetProps } from "./Fieldset.props";
5
5
  import { Tooltip } from "../Tooltip";
6
6
 
7
+ type FieldsetContextType = {
8
+ hasError: boolean;
9
+ setHasError: (value: boolean) => void;
10
+ };
11
+
12
+ // Create a new context for the error state
13
+ export const FieldsetErrorContext = createContext<FieldsetContextType>({
14
+ hasError: false,
15
+ setHasError: (value) => value,
16
+ });
17
+
18
+ // Custom hook to access the error state from the context
19
+ export const useFieldsetError = () => useContext(FieldsetErrorContext);
20
+
7
21
  const Fieldset: FC<FieldsetProps> = ({
8
22
  children,
9
23
  className,
10
- fieldsetid,
11
- grouperror,
12
- grouphelper,
13
- grouptooltip,
14
24
  legend,
25
+ helper,
26
+ tooltip,
27
+ errorMessage,
28
+ disabled = false,
29
+ wrap = "nowrap",
30
+ direction = "column",
31
+ ...props
15
32
  }) => {
16
33
  const { prefix } = useGlobalSettings();
34
+
35
+ const [hasError, setHasError] = useState(false);
36
+
17
37
  const baseClass = `${prefix}--fieldset`;
18
- const fieldsetClasses = classNames(className, {
19
- [baseClass]: true,
38
+ const wrapClass = `${baseClass}--wrap__${wrap}`;
39
+ const directionClass = `${baseClass}--direction__${direction}`;
40
+ const helperClass = `${baseClass}--helper`;
41
+ const errorClass = `${baseClass}__error`;
42
+ const disabledClass = `${baseClass}__disabled`;
43
+
44
+ const fieldsetClasses = classNames(
45
+ className,
46
+ baseClass,
47
+ wrapClass,
48
+ directionClass,
49
+ {
50
+ [disabledClass]: disabled,
51
+ }
52
+ );
53
+
54
+ const helperClasses = classNames(helperClass, {
55
+ [errorClass]: hasError,
20
56
  });
21
57
 
58
+ const contextValue = {
59
+ hasError,
60
+ setHasError,
61
+ };
62
+
63
+ const showHelper = !!helper || hasError;
64
+
65
+ const helperMessage = hasError ? errorMessage : helper;
66
+
22
67
  return (
23
- <fieldset
24
- className={fieldsetClasses}
25
- id={fieldsetid ? fieldsetid : undefined}
26
- >
27
- {legend && (
28
- <legend className={`${baseClass}--legend`}>
29
- {legend}
30
- {grouptooltip && (
31
- <Tooltip
32
- className={`${baseClass}--legend--tooltip`}
33
- icon={true}
34
- label={grouptooltip}
35
- theme={"dark"}
36
- ></Tooltip>
37
- )}
38
- </legend>
39
- )}
40
- {grouphelper && !grouperror && (
41
- <span className={`${baseClass}--helper`}>{grouphelper}</span>
42
- )}
43
- {grouperror && (
44
- <span className={`${baseClass}--error`}>{grouperror}</span>
45
- )}
46
- {Children.map(children, (child: FieldsetChild, i) => (
47
- <>
48
- {child && child.props && (
49
- <div
50
- key={`${baseClass}--input--${i}`}
51
- className={`${baseClass}--input${
52
- child.props?.type ? "--" + child.props.type : ""
53
- } ${child.props.error ? "error" : ""}`}
54
- >
55
- {child.props.label && (
56
- <label
57
- className={`${baseClass}--label`}
58
- htmlFor={child.props.id ? child.props.id : child.props.name}
59
- >
60
- {child.props.label}
61
- {child.props.tooltip && (
62
- <Tooltip
63
- className={`${baseClass}--label--tooltip`}
64
- icon={true}
65
- label={child.props.tooltip}
66
- theme={"dark"}
67
- ></Tooltip>
68
- )}
69
- </label>
70
- )}
71
- {child}
72
- {child.props.helper && !child.props.error && (
73
- <span className={`${baseClass}--helper`}>
74
- {child.props.helper}
75
- </span>
76
- )}
77
- {child.props.error && (
78
- <span className={`${baseClass}--error`}>
79
- {child.props.error}
80
- </span>
68
+ <fieldset className={fieldsetClasses} {...props}>
69
+ {legend || helper ? (
70
+ <div className={`${baseClass}--legend-wrapper`}>
71
+ {legend && (
72
+ <legend className={`${baseClass}--legend`}>
73
+ {legend}
74
+ {tooltip && (
75
+ <Tooltip
76
+ className={`${baseClass}--legend--tooltip`}
77
+ icon={true}
78
+ label={tooltip}
79
+ theme={"dark"}
80
+ ></Tooltip>
81
81
  )}
82
- </div>
82
+ </legend>
83
83
  )}
84
- </>
85
- ))}
84
+ {showHelper && <span className={helperClasses}>{helperMessage}</span>}
85
+ </div>
86
+ ) : null}
87
+ <div className={`${baseClass}--elements`}>
88
+ <FieldsetErrorContext.Provider value={contextValue}>
89
+ {children}
90
+ </FieldsetErrorContext.Provider>
91
+ </div>
86
92
  </fieldset>
87
93
  );
88
94
  };
@@ -1,54 +1,51 @@
1
- import { FileUploadProps } from "./FileUpload.props";
1
+ import { FileUploadProps, LabelledFileUploadProps } from "./FileUpload.props";
2
2
 
3
- const basic: FileUploadProps = {
4
- callback: () => {},
3
+ const basic: LabelledFileUploadProps = {
4
+ label: "Upload your file",
5
+ id: "fileupload",
5
6
  disabled: false,
6
7
  error: false,
7
- helper: false,
8
- label: "File Upload Field Label",
9
- multiple: false,
8
+ multiple: true,
10
9
  name: "fileupload",
11
- placeholder: "File Upload Field",
10
+ placeholder: "Select files",
12
11
  required: false,
13
12
  };
14
13
 
15
- const hashelper: FileUploadProps = {
16
- callback: () => {},
14
+ const hashelper: LabelledFileUploadProps = {
15
+ label: "Upload your file",
16
+ id: "fileupload",
17
17
  disabled: false,
18
18
  error: false,
19
- helper: "The file upload's helper text",
20
- label: "File Upload Field Label",
21
- multiple: false,
19
+ multiple: true,
22
20
  name: "fileupload",
23
- placeholder: "File Upload Field",
21
+ placeholder: "Select files",
24
22
  required: false,
25
23
  };
26
24
 
27
- const haserror: FileUploadProps = {
28
- callback: () => {},
25
+ const haserror: LabelledFileUploadProps = {
26
+ label: "Upload your file",
27
+ id: "fileupload",
29
28
  disabled: false,
30
- error: "this one has an error",
31
- helper: false,
32
- label: "File Upload Field Label",
33
- multiple: false,
29
+ error: true,
30
+ multiple: true,
34
31
  name: "fileupload",
35
- placeholder: "File Upload Field",
32
+ placeholder: "Select files",
36
33
  required: false,
37
34
  };
38
35
 
39
- const hastooltip: FileUploadProps = {
40
- callback: () => {},
36
+ const hastooltip: LabelledFileUploadProps = {
37
+ label: "Upload your file",
38
+ id: "fileupload",
41
39
  disabled: false,
42
40
  error: false,
43
- helper: false,
44
- label: "File Upload Field Label",
45
- multiple: false,
41
+ multiple: true,
46
42
  name: "fileupload",
47
- placeholder: "File Upload Field",
43
+ placeholder: "Select files",
48
44
  required: false,
49
- tooltip: "here is the Tooltip",
50
45
  };
51
46
 
47
+ const disabled: FileUploadProps = { ...basic, disabled: true };
48
+
52
49
  /**
53
50
  * Sample prop definitions FileUpload's enumerable properties (imported in stories and test)
54
51
  */
@@ -57,6 +54,7 @@ const FileUploadArgs = {
57
54
  hashelper,
58
55
  haserror,
59
56
  hastooltip,
57
+ disabled,
60
58
  };
61
59
 
62
60
  export default FileUploadArgs;
@@ -1,61 +1,16 @@
1
- export interface FileUploadProps {
2
- /**
3
- * The FileUpload's onChange callback.
4
- */
5
- callback?: (e: React.ChangeEvent<HTMLInputElement>) => any;
6
-
7
- /**
8
- * Specify an optional className to be added to your FileUpload.
9
- */
10
- className?: string;
11
-
12
- /**
13
- * Is the FileUpload disabled?
14
- */
15
- disabled?: boolean;
16
-
17
- /**
18
- * Does the FileUpload have an error?
19
- */
20
- error?: string | false;
21
-
22
- /**
23
- * The FileUpload's helper text
24
- */
25
- helper: string | false;
26
-
27
- /**
28
- * The FileUpload's id attribute
29
- */
30
- id?: string;
31
-
32
- /**
33
- * Does this FileUpload have a label?
34
- */
35
- label: string;
36
-
37
- /**
38
- * Should the FileUpload allow multiple uploads?
39
- */
40
- multiple?: boolean;
1
+ import { FormFieldProps } from "../../types";
2
+ import { LabelledFormFieldProps } from "../../types";
41
3
 
4
+ export interface FileUploadProps extends FormFieldProps<HTMLInputElement> {
42
5
  /**
43
- * The FileUpload's name attribute
44
- */
45
- name?: Required<string>;
46
-
47
- /**
48
- * Specify an optional className to be added to your FileUpload component.
6
+ * The placeholder for the input
49
7
  */
50
8
  placeholder?: string;
51
9
 
52
10
  /**
53
- * Specify whether this FileUpload is required
54
- */
55
- required?: boolean;
56
-
57
- /**
58
- * Does this FileUpload have a tooltip?
11
+ * Can the user upload more than one file?
59
12
  */
60
- tooltip?: string;
13
+ multiple?: boolean;
61
14
  }
15
+
16
+ export type LabelledFileUploadProps = LabelledFormFieldProps<FileUploadProps>;
@@ -1,82 +1,113 @@
1
- import { FC, useState } from "react";
1
+ import React, { useState } from "react";
2
2
  import classNames from "classnames";
3
3
  import useGlobalSettings from "../../hooks/useGlobalSettings";
4
- import { FileUploadProps } from "./FileUpload.props";
5
- import { Fieldset } from "../Fieldset";
6
- import { FormElement } from "../FormElement";
4
+ import FormControl, { useFormControl } from "../FormControl/FormControl";
5
+ import { FileUploadProps, LabelledFileUploadProps } from "./FileUpload.props";
7
6
 
8
- const FileUpload: FC<FileUploadProps> = ({
9
- callback,
10
- disabled = false,
11
- error,
12
- helper,
13
- id,
14
- label,
15
- multiple,
16
- name,
17
- placeholder,
18
- required,
19
- tooltip,
20
- }) => {
21
- const { prefix } = useGlobalSettings();
22
- const baseClass = `${prefix}--file-upload`;
23
- const FileUploadClasses = classNames("", {
24
- [baseClass]: true,
25
- [`error`]: error,
26
- });
7
+ function formatBytes(bytes: number, decimals = 2) {
8
+ if (!+bytes) return "0 Bytes";
27
9
 
28
- const [uploadfiles, setUploadFiles] = useState([]);
10
+ const k = 1024;
11
+ const dm = decimals < 0 ? 0 : decimals;
12
+ const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
29
13
 
30
- /**
31
- * On change, add file to listed files, if there is a callback, call it
32
- */
33
- const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
34
- const files = e.target.files;
35
- setUploadFiles(files as any);
14
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
36
15
 
37
- if (callback) {
38
- callback(e);
39
- }
40
- };
16
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
17
+ }
18
+ const FileUpload = React.forwardRef<HTMLInputElement, FileUploadProps>(
19
+ (
20
+ {
21
+ onChange,
22
+ onBlur,
23
+ disabled = false,
24
+ error,
25
+ id,
26
+ multiple,
27
+ name,
28
+ placeholder,
29
+ required,
30
+ },
31
+ ref
32
+ ) => {
33
+ const { prefix } = useGlobalSettings();
34
+ const formControlCtx = useFormControl();
35
+ const { ariaDescribedBy } = formControlCtx;
36
+
37
+ const baseClass = `${prefix}--file-upload`;
38
+ const fileUploadClasses = classNames(baseClass, {
39
+ [`error`]: error,
40
+ });
41
+ const inputClass = `${baseClass}--input`;
42
+
43
+ const [uploadfiles, setUploadFiles] = useState<null | FileList>(null);
44
+
45
+ const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
46
+ const files = e.target.files;
47
+ setUploadFiles(files);
48
+
49
+ if (onChange) {
50
+ onChange(e);
51
+ }
52
+ };
53
+
54
+ const inputId = id ? id : name;
55
+
56
+ return (
57
+ <>
58
+ <div className={fileUploadClasses}>
59
+ <label className={inputClass}>
60
+ {placeholder}
61
+ <input
62
+ ref={ref}
63
+ id={inputId}
64
+ name={name}
65
+ onChange={handleChange}
66
+ onBlur={onBlur}
67
+ disabled={disabled}
68
+ multiple={multiple}
69
+ placeholder={placeholder}
70
+ required={required as any}
71
+ type="file"
72
+ data-label={placeholder}
73
+ aria-describedby={ariaDescribedBy}
74
+ />
75
+ </label>
76
+ </div>
77
+ {uploadfiles && uploadfiles.length > 0 && (
78
+ <ul className={`${baseClass}--list`}>
79
+ {[...uploadfiles].map((file, i) => (
80
+ <li
81
+ className={`${baseClass}--list-item`}
82
+ key={`${baseClass}--list-item-${i}`}
83
+ >
84
+ {`${file.name} (${formatBytes(file.size)})`}
85
+ </li>
86
+ ))}
87
+ </ul>
88
+ )}
89
+ </>
90
+ );
91
+ }
92
+ );
93
+
94
+ const LabelledFileUpload = React.forwardRef<
95
+ HTMLInputElement,
96
+ LabelledFileUploadProps
97
+ >((props, ref) => {
98
+ const { style, inputStyle, className, ...rest } = props;
99
+ const fieldId = props.id ? props.id : props.name;
41
100
 
42
101
  return (
43
- <Fieldset className={"file-upload"}>
44
- <FormElement
45
- elemid={name as any}
46
- label={label}
47
- helper={helper as any}
48
- error={error as any}
49
- required={required as any}
50
- tooltip={tooltip}
51
- type={"file"}
52
- >
53
- <input
54
- id={id ? id : name}
55
- name={name}
56
- onChange={handleChange}
57
- disabled={disabled}
58
- multiple={multiple}
59
- placeholder={placeholder}
60
- required={required as any}
61
- type={"file"}
62
- className={FileUploadClasses}
63
- data-label={placeholder}
64
- />
65
- </FormElement>
66
- {uploadfiles.length > 0 && (
67
- <ul className={`${baseClass}--list`}>
68
- {[...uploadfiles].map((file: any, i: any) => (
69
- <li
70
- className={`${baseClass}--list-item`}
71
- key={`${baseClass}--list-item-${i}`}
72
- >
73
- {file.name}
74
- </li>
75
- ))}
76
- </ul>
77
- )}
78
- </Fieldset>
102
+ <FormControl
103
+ fieldId={fieldId}
104
+ style={style}
105
+ className={className}
106
+ {...rest}
107
+ >
108
+ <FileUpload ref={ref} style={inputStyle} {...rest} />
109
+ </FormControl>
79
110
  );
80
- };
111
+ });
81
112
 
82
- export default FileUpload;
113
+ export default LabelledFileUpload;
@@ -1,187 +1,5 @@
1
- import CheckboxArgs from "../Checkbox/Checkbox.args";
2
- import DatePickerArgs from "../DatePicker/DatePicker.args";
3
- import DropdownArgs from "../Dropdown/Dropdown.args";
4
- import FileUploadArgs from "../FileUpload/FileUpload.args";
5
- import InputArgs from "../Input/Input.args";
6
- import NumberPickerArgs from "../NumberPicker/NumberPicker.args";
7
- import TextareaArgs from "../Textarea/Textarea.args";
8
1
  import { FormProps } from "./Form.props";
9
2
 
10
- const basic: FormProps = {
11
- action: "https://my.action.url",
12
- formid: "exampleform",
13
- items: [
14
- {
15
- field: InputArgs.basic,
16
- type: "input",
17
- },
18
- {
19
- field: DropdownArgs.basic,
20
- type: "dropdown",
21
- },
22
- {
23
- field: CheckboxArgs.basic,
24
- type: "checkbox",
25
- choicegroupid: "",
26
- },
27
- {
28
- field: FileUploadArgs.basic,
29
- type: "file",
30
- },
31
- {
32
- field: DatePickerArgs.hasrange,
33
- type: "date",
34
- },
35
- {
36
- field: NumberPickerArgs.basic,
37
- type: "number",
38
- },
39
- {
40
- field: TextareaArgs.basic,
41
- type: "textarea",
42
- },
43
- ],
44
- submitlabel: "Submit",
3
+ export const basic: FormProps = {
4
+ theme: "light",
45
5
  };
46
-
47
- const hashelper: FormProps = {
48
- action: "https://my.action.url",
49
- formid: "exampleform",
50
- items: [
51
- {
52
- field: InputArgs.hashelper,
53
- type: "input",
54
- },
55
- {
56
- field: DropdownArgs.hashelper,
57
- type: "dropdown",
58
- },
59
- {
60
- field: FileUploadArgs.hashelper,
61
- type: "file",
62
- },
63
- {
64
- field: DatePickerArgs.hashelper,
65
- type: "date",
66
- },
67
- {
68
- field: NumberPickerArgs.hashelper,
69
- type: "number",
70
- },
71
- {
72
- field: TextareaArgs.hashelper,
73
- type: "textarea",
74
- },
75
- ],
76
- submitlabel: "Submit",
77
- };
78
-
79
- const haserror: FormProps = {
80
- action: "https://my.action.url",
81
- formid: "exampleform",
82
- items: [
83
- {
84
- field: InputArgs.haserror,
85
- type: "input",
86
- },
87
- {
88
- field: DropdownArgs.haserror,
89
- type: "dropdown",
90
- },
91
- {
92
- field: FileUploadArgs.haserror,
93
- type: "file",
94
- },
95
- {
96
- field: DatePickerArgs.haserror,
97
- type: "date",
98
- },
99
- {
100
- field: NumberPickerArgs.haserror,
101
- type: "number",
102
- },
103
- {
104
- field: TextareaArgs.haserror,
105
- type: "textarea",
106
- },
107
- ],
108
- submitlabel: "Submit",
109
- };
110
-
111
- const hastooltip: FormProps = {
112
- action: "https://my.action.url",
113
- formid: "exampleform",
114
- items: [
115
- {
116
- field: InputArgs.hastooltip,
117
- type: "input",
118
- },
119
- {
120
- field: DropdownArgs.hastooltip,
121
- type: "dropdown",
122
- },
123
-
124
- {
125
- field: FileUploadArgs.hastooltip,
126
- type: "file",
127
- },
128
- {
129
- field: DatePickerArgs.hastooltip,
130
- type: "date",
131
- },
132
- {
133
- field: NumberPickerArgs.hastooltip,
134
- type: "number",
135
- },
136
- {
137
- field: TextareaArgs.hastooltip,
138
- type: "textarea",
139
- },
140
- ],
141
- submitlabel: "Submit",
142
- };
143
-
144
- const hasdisabled: FormProps = {
145
- action: "https://my.action.url",
146
- formid: "exampleform",
147
- items: [
148
- {
149
- field: InputArgs.isdisabled,
150
- type: "input",
151
- },
152
- {
153
- field: DropdownArgs.basic,
154
- type: "dropdown",
155
- },
156
- {
157
- field: FileUploadArgs.basic,
158
- type: "file",
159
- },
160
- {
161
- field: DatePickerArgs.hasrange,
162
- type: "date",
163
- },
164
- {
165
- field: NumberPickerArgs.basic,
166
- type: "number",
167
- },
168
- {
169
- field: TextareaArgs.basic,
170
- type: "textarea",
171
- },
172
- ],
173
- submitlabel: "Submit",
174
- };
175
-
176
- /**
177
- * Sample prop definitions FormGroup's enumerable properties (imported in stories and test)
178
- */
179
- const FormArgs = {
180
- basic,
181
- hasdisabled,
182
- hashelper,
183
- haserror,
184
- hastooltip,
185
- };
186
-
187
- export default FormArgs;