@wq/form-web 3.0.0-alpha.0

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 (50) hide show
  1. package/AutoForm.js +24 -0
  2. package/README.md +10 -0
  3. package/components/Checkbox.js +30 -0
  4. package/components/DateTime.js +14 -0
  5. package/components/DeleteForm.js +56 -0
  6. package/components/Draw.js +78 -0
  7. package/components/Fieldset.js +32 -0
  8. package/components/FieldsetArray.js +30 -0
  9. package/components/File.js +77 -0
  10. package/components/FileArray.js +132 -0
  11. package/components/FlatFieldset.js +30 -0
  12. package/components/FormError.js +19 -0
  13. package/components/FormRoot.js +32 -0
  14. package/components/GeoHelpIcon.js +42 -0
  15. package/components/HelperText.js +28 -0
  16. package/components/Hidden.js +24 -0
  17. package/components/IconSubmitButton.js +20 -0
  18. package/components/Image.js +10 -0
  19. package/components/Input.js +28 -0
  20. package/components/Radio.js +53 -0
  21. package/components/Select.js +191 -0
  22. package/components/SubmitButton.js +36 -0
  23. package/components/Toggle.js +52 -0
  24. package/components/index.js +50 -0
  25. package/index.js +3 -0
  26. package/package.json +47 -0
  27. package/src/AutoForm.js +17 -0
  28. package/src/components/Checkbox.js +28 -0
  29. package/src/components/DateTime.js +13 -0
  30. package/src/components/DeleteForm.js +52 -0
  31. package/src/components/Draw.js +82 -0
  32. package/src/components/Fieldset.js +24 -0
  33. package/src/components/FieldsetArray.js +27 -0
  34. package/src/components/File.js +73 -0
  35. package/src/components/FileArray.js +129 -0
  36. package/src/components/FlatFieldset.js +26 -0
  37. package/src/components/FormError.js +18 -0
  38. package/src/components/FormRoot.js +27 -0
  39. package/src/components/GeoHelpIcon.js +52 -0
  40. package/src/components/HelperText.js +28 -0
  41. package/src/components/Hidden.js +21 -0
  42. package/src/components/IconSubmitButton.js +18 -0
  43. package/src/components/Image.js +9 -0
  44. package/src/components/Input.js +30 -0
  45. package/src/components/Radio.js +38 -0
  46. package/src/components/Select.js +158 -0
  47. package/src/components/SubmitButton.js +38 -0
  48. package/src/components/Toggle.js +35 -0
  49. package/src/components/index.js +52 -0
  50. package/src/index.js +3 -0
@@ -0,0 +1,158 @@
1
+ import React, { useMemo } from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { Field, getIn } from "formik";
4
+ import { Select as FMuiSelect } from "formik-mui";
5
+ import { MenuItem, Checkbox, ListItemText, ListSubheader } from "@mui/material";
6
+ import PropTypes from "prop-types";
7
+ import { useFormikContext } from "formik";
8
+
9
+ function ContextCheckbox({ value, field }) {
10
+ const { values } = useFormikContext();
11
+ const checked = (getIn(values, field) || []).some((val) => val === value);
12
+ return <Checkbox checked={checked} />;
13
+ }
14
+
15
+ ContextCheckbox.propTypes = {
16
+ value: PropTypes.string,
17
+ field: PropTypes.string,
18
+ };
19
+
20
+ function Select({
21
+ choices,
22
+ label,
23
+ required,
24
+ native,
25
+ placeholder,
26
+ renderValue,
27
+ InputLabelProps,
28
+ ...rest
29
+ }) {
30
+ const { name: fieldName, type, hint } = rest,
31
+ { errors, touched } = useFormikContext(),
32
+ showError = !!getIn(errors, fieldName) && getIn(touched, fieldName),
33
+ multiple = type === "select";
34
+
35
+ if (multiple && !renderValue) {
36
+ renderValue = (sel) => sel.map(getLabel).join(", ");
37
+ }
38
+ if (placeholder && !renderValue) {
39
+ renderValue = (sel) => getLabel(sel) || sel || placeholder;
40
+ }
41
+
42
+ const getLabel = useMemo(() => {
43
+ const labels = {};
44
+ choices.forEach(({ name, label }) => {
45
+ labels[name] = label;
46
+ });
47
+ return (name) => labels[name];
48
+ }, [choices]);
49
+
50
+ const choiceGroups = useMemo(() => {
51
+ const choiceGroups = {};
52
+ choices.forEach((choice) => {
53
+ const group = choice.group || "";
54
+ if (!choiceGroups[group]) {
55
+ choiceGroups[group] = [];
56
+ }
57
+ choiceGroups[group].push(choice);
58
+ });
59
+ return choiceGroups;
60
+ }, [choices]);
61
+
62
+ const Option = useMemo(
63
+ () =>
64
+ native
65
+ ? ({ value, children }) => (
66
+ <option value={value}>{children}</option>
67
+ )
68
+ : ({ children, ...rest }) => (
69
+ <MenuItem {...rest}>
70
+ {multiple && (
71
+ <ContextCheckbox
72
+ value={rest["data-value"]}
73
+ field={fieldName}
74
+ />
75
+ )}
76
+ <ListItemText
77
+ primary={children}
78
+ data-value={rest["data-value"]}
79
+ primaryTypographyProps={{
80
+ "data-value": rest["data-value"],
81
+ }}
82
+ />
83
+ </MenuItem>
84
+ ),
85
+ [native, multiple, fieldName],
86
+ );
87
+
88
+ const renderChoices = (choices) =>
89
+ choices.map(({ name, label }) => (
90
+ <Option key={name} value={name}>
91
+ {label}
92
+ </Option>
93
+ ));
94
+
95
+ const renderGroups = native
96
+ ? (choiceGroups) =>
97
+ Object.entries(choiceGroups).map(([group, choices]) =>
98
+ group ? (
99
+ <optgroup key={group} label={group}>
100
+ {renderChoices(choices)}
101
+ </optgroup>
102
+ ) : (
103
+ <>{renderChoices(choices)}</>
104
+ ),
105
+ )
106
+ : (choiceGroups) => {
107
+ const flattened = [];
108
+ Object.entries(choiceGroups).forEach(([group, choices]) => {
109
+ if (group) {
110
+ flattened.push(
111
+ <ListSubheader style={{ backgroundColor: "white" }}>
112
+ {group}
113
+ </ListSubheader>,
114
+ );
115
+ }
116
+ flattened.push(...renderChoices(choices));
117
+ });
118
+ return flattened;
119
+ };
120
+
121
+ return (
122
+ <Field
123
+ component={FMuiSelect}
124
+ formControl={{ fullWidth: true, margin: "dense" }}
125
+ inputLabel={{ required, error: showError, ...InputLabelProps }}
126
+ formHelperText={{ children: hint }}
127
+ multiple={multiple}
128
+ required={required}
129
+ native={native}
130
+ label={label}
131
+ renderValue={renderValue}
132
+ displayEmpty={!!placeholder}
133
+ {...(InputLabelProps && InputLabelProps.shrink
134
+ ? { notched: true }
135
+ : {})}
136
+ {...rest}
137
+ >
138
+ {!multiple && (
139
+ <Option value={""} disabled={!!required}>
140
+ {required ? "Select one..." : "(No Selection)"}
141
+ </Option>
142
+ )}
143
+ {renderGroups(choiceGroups)}
144
+ </Field>
145
+ );
146
+ }
147
+
148
+ Select.propTypes = {
149
+ choices: PropTypes.arrayOf(PropTypes.object),
150
+ label: PropTypes.string,
151
+ placeholder: PropTypes.string,
152
+ required: PropTypes.bool,
153
+ native: PropTypes.bool,
154
+ renderValue: PropTypes.func,
155
+ InputLabelProps: PropTypes.object,
156
+ };
157
+
158
+ export default withWQ(Select);
@@ -0,0 +1,38 @@
1
+ import React from "react";
2
+ import {
3
+ useMessage,
4
+ useComponents,
5
+ withWQ,
6
+ createFallbackComponent,
7
+ } from "@wq/react";
8
+ import { useFormikContext } from "formik";
9
+
10
+ const SubmitButtonFallback = {
11
+ messages: {
12
+ SUBMIT: "Submit",
13
+ },
14
+ components: {
15
+ Button: createFallbackComponent("Button", "@wq/material"),
16
+ },
17
+ };
18
+
19
+ function SubmitButton(props) {
20
+ const { Button } = useComponents(),
21
+ defaultLabel = useMessage("SUBMIT"),
22
+ label = props.children || defaultLabel,
23
+ { isSubmitting } = useFormikContext();
24
+
25
+ return (
26
+ <Button
27
+ type="submit"
28
+ color="primary"
29
+ variant="contained"
30
+ disabled={isSubmitting}
31
+ {...props}
32
+ >
33
+ {label}
34
+ </Button>
35
+ );
36
+ }
37
+
38
+ export default withWQ(SubmitButton, { fallback: SubmitButtonFallback });
@@ -0,0 +1,35 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { Field } from "formik";
4
+ import { ToggleButton, FormControl, FormLabel } from "@mui/material";
5
+ import { ToggleButtonGroup } from "formik-mui";
6
+ import HelperText from "./HelperText.js";
7
+ import PropTypes from "prop-types";
8
+
9
+ function Toggle({ choices, label, ...rest }) {
10
+ return (
11
+ <FormControl component="fieldset" fullWidth margin="dense">
12
+ <FormLabel component="legend">{label}</FormLabel>
13
+ <Field
14
+ component={ToggleButtonGroup}
15
+ exclusive
16
+ {...rest}
17
+ type="checkbox"
18
+ >
19
+ {choices.map(({ name, label }) => (
20
+ <ToggleButton key={name} value={name}>
21
+ {label}
22
+ </ToggleButton>
23
+ ))}
24
+ </Field>
25
+ <HelperText name={rest.name} hint={rest.hint} />
26
+ </FormControl>
27
+ );
28
+ }
29
+
30
+ Toggle.propTypes = {
31
+ choices: PropTypes.arrayOf(PropTypes.object),
32
+ label: PropTypes.string,
33
+ };
34
+
35
+ export default withWQ(Toggle);
@@ -0,0 +1,52 @@
1
+ import FormRoot from "./FormRoot.js";
2
+ import FormError from "./FormError.js";
3
+ import Input from "./Input.js";
4
+ import Checkbox from "./Checkbox.js";
5
+ import DateTime from "./DateTime.js";
6
+ import File from "./File.js";
7
+ import Image from "./Image.js";
8
+ import Select from "./Select.js";
9
+ import Radio from "./Radio.js";
10
+ import Toggle from "./Toggle.js";
11
+ import Hidden from "./Hidden.js";
12
+ import HelperText from "./HelperText.js";
13
+ import Fieldset from "./Fieldset.js";
14
+ import FlatFieldset from "./FlatFieldset.js";
15
+ import FieldsetArray from "./FieldsetArray.js";
16
+ import FileArray from "./FileArray.js";
17
+ import SubmitButton from "./SubmitButton.js";
18
+ import IconSubmitButton from "./IconSubmitButton.js";
19
+ import DeleteForm from "./DeleteForm.js";
20
+ import Draw from "./Draw.js";
21
+ import GeoHelpIcon from "./GeoHelpIcon.js";
22
+
23
+ const Date = DateTime;
24
+ const Time = DateTime;
25
+ const dateTime = DateTime;
26
+
27
+ export {
28
+ FormRoot,
29
+ FormError,
30
+ Input,
31
+ Checkbox,
32
+ DateTime,
33
+ Date,
34
+ Time,
35
+ dateTime,
36
+ File,
37
+ Image,
38
+ Select,
39
+ Radio,
40
+ Toggle,
41
+ Hidden,
42
+ HelperText,
43
+ Fieldset,
44
+ FlatFieldset,
45
+ FieldsetArray,
46
+ FileArray,
47
+ SubmitButton,
48
+ IconSubmitButton,
49
+ DeleteForm,
50
+ Draw,
51
+ GeoHelpIcon,
52
+ };
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import AutoForm from "./AutoForm.js";
2
+ export { AutoForm };
3
+ export * from "./components/index.js";