@wq/form-native 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.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ [![@wq/form][logo]][docs]
2
+
3
+ **@wq/form-native** provides [React Native] and [Expo] bindings for [@wq/form], via the [React Native Paper] library.
4
+
5
+ ### [Documentation][docs]
6
+
7
+ [logo]: https://wq.io/images/@wq/form.svg
8
+ [docs]: https://form.wq.io/@wq/form-native
9
+ [@wq/form]: https://form.wq.io/
10
+ [React Native]: https://reactnative.dev/
11
+ [Expo]: https://expo.dev/
12
+ [React Native Paper]: https://callstack.github.io/react-native-paper/
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ env: {
3
+ test: {
4
+ plugins: [
5
+ ["@babel/plugin-transform-private-methods", { loose: true }],
6
+ ],
7
+ presets: ["module:metro-react-native-babel-preset"],
8
+ },
9
+ },
10
+ };
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ export default {
2
+ preset: "jest-expo",
3
+ testMatch: ["**/__tests__/**/*.js?(x)"],
4
+ transformIgnorePatterns: [],
5
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@wq/form-native",
3
+ "version": "3.0.0-alpha.0",
4
+ "description": "React Native and Expo bindings for @wq/material",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "scripts": {
8
+ "test": "cd ../../ && npm run jest packages/form-native -- --config packages/form-native/jest.config.js --moduleDirectories node_modules/ packages/form-native/node_modules/",
9
+ "build": "npm run prettier",
10
+ "prettier": "cd ../../ && npm run prettier -- --write packages/form-native/",
11
+ "lint": "cd ../../ && npm run eslint packages/form-native/{,src/,src/*/,src/*/*/}*.js"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/wq/form.git",
16
+ "directory": "packages/form-native"
17
+ },
18
+ "keywords": [
19
+ "wq",
20
+ "react",
21
+ "material",
22
+ "material-ui"
23
+ ],
24
+ "author": "S. Andrew Sheppard",
25
+ "license": "MIT",
26
+ "bugs": {
27
+ "url": "https://github.com/wq/form/issues"
28
+ },
29
+ "homepage": "https://form.wq.io/@wq/form-native",
30
+ "dependencies": {
31
+ "@wq/form-common": "^3.0.0-alpha.0",
32
+ "@wq/react": "^3.0.0-alpha.0"
33
+ },
34
+ "peerDependencies": {
35
+ "expo-application": "*",
36
+ "expo-document-picker": "*",
37
+ "expo-image-picker": "*",
38
+ "react-native-modal-datetime-picker": "*",
39
+ "react-native-paper": "*",
40
+ "react-native-picker-select": "*"
41
+ },
42
+ "devDependencies": {
43
+ "expo-application": "^7.0.8",
44
+ "expo-document-picker": "^14.0.8",
45
+ "expo-image-picker": "^17.0.10",
46
+ "jest-expo": "^52.0.6",
47
+ "react-native-modal-datetime-picker": "^18.0.0",
48
+ "react-native-paper": "^5.14.5",
49
+ "react-native-picker-select": "^9.3.1"
50
+ }
51
+ }
@@ -0,0 +1,17 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { AutoFormBase, AutoInput, Form, CancelButton } from "@wq/form-common";
4
+ import * as components from "./components/index.js";
5
+
6
+ const AutoFormDefaults = {
7
+ components: { ...components, AutoForm, AutoInput, Form, CancelButton },
8
+ };
9
+
10
+ function AutoForm(props) {
11
+ if (!props.action && !props.onSubmit && !props.form) {
12
+ return props.children || null;
13
+ }
14
+ return <AutoFormBase {...props} />;
15
+ }
16
+
17
+ export default withWQ(AutoForm, { defaults: AutoFormDefaults });
@@ -0,0 +1,50 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { useField } from "formik";
4
+ import { Checkbox as PaperCheckbox, useTheme } from "react-native-paper";
5
+ import { Text, View } from "react-native";
6
+ import HelperText from "./HelperText.js";
7
+ import PropTypes from "prop-types";
8
+
9
+ function Checkbox({ name, label, hint }) {
10
+ const theme = useTheme(),
11
+ [, meta, helpers] = useField(name),
12
+ { value } = meta,
13
+ { setValue } = helpers;
14
+
15
+ function toggleChecked() {
16
+ setValue(!value);
17
+ }
18
+
19
+ return (
20
+ <View>
21
+ <View
22
+ style={{
23
+ flexDirection: "row",
24
+ alignItems: "center",
25
+ padding: 8,
26
+ }}
27
+ >
28
+ <PaperCheckbox.Android
29
+ status={value ? "checked" : "unchecked"}
30
+ onPress={toggleChecked}
31
+ />
32
+ <Text
33
+ style={{ color: theme.colors.text, fontSize: 16, flex: 1 }}
34
+ onPress={toggleChecked}
35
+ >
36
+ {label}
37
+ </Text>
38
+ </View>
39
+ <HelperText name={name} hint={hint} />
40
+ </View>
41
+ );
42
+ }
43
+
44
+ Checkbox.propTypes = {
45
+ name: PropTypes.string,
46
+ label: PropTypes.string,
47
+ hint: PropTypes.string,
48
+ };
49
+
50
+ export default withWQ(Checkbox);
@@ -0,0 +1,67 @@
1
+ import React, { useState } from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import DateTimePickerModal from "react-native-modal-datetime-picker";
4
+ import { Text, View } from "react-native";
5
+ import { TouchableRipple, TextInput } from "react-native-paper";
6
+ import { useField } from "formik";
7
+ import { format, parse } from "@wq/form-common";
8
+ import HelperText from "./HelperText.js";
9
+ import PropTypes from "prop-types";
10
+
11
+ const displayFormat = {
12
+ date: (value) => parse.date(value).toLocaleDateString(),
13
+ time: (value) => parse.time(value).toLocaleTimeString(),
14
+ datetime: (value) => parse.datetime(value).toLocaleString(),
15
+ };
16
+
17
+ function DateTime({ name, type, label, hint }) {
18
+ type = type.toLowerCase();
19
+
20
+ const [, meta, helpers] = useField(name),
21
+ { value } = meta,
22
+ { setValue } = helpers;
23
+
24
+ const [show, setShow] = useState(false);
25
+
26
+ function showPicker() {
27
+ setShow(true);
28
+ }
29
+ function hidePicker() {
30
+ setShow(false);
31
+ }
32
+ function onConfirm(val) {
33
+ hidePicker();
34
+ setValue(format[type](val));
35
+ }
36
+
37
+ return (
38
+ <View>
39
+ <TextInput
40
+ label={label}
41
+ value={value ? displayFormat[type](value) : ""}
42
+ render={({ style, value }) => (
43
+ <TouchableRipple onPress={showPicker}>
44
+ <Text style={style}>{value}</Text>
45
+ </TouchableRipple>
46
+ )}
47
+ />
48
+ <HelperText name={name} hint={hint} />
49
+ <DateTimePickerModal
50
+ isVisible={show}
51
+ mode={type}
52
+ date={parse[type](value) || new Date()}
53
+ onConfirm={onConfirm}
54
+ onCancel={hidePicker}
55
+ />
56
+ </View>
57
+ );
58
+ }
59
+
60
+ DateTime.propTypes = {
61
+ name: PropTypes.string,
62
+ type: PropTypes.string,
63
+ label: PropTypes.string,
64
+ hint: PropTypes.string,
65
+ };
66
+
67
+ export default withWQ(DateTime);
@@ -0,0 +1,85 @@
1
+ import React from "react";
2
+ import {
3
+ useComponents,
4
+ useMessage,
5
+ withWQ,
6
+ createFallbackComponents,
7
+ } from "@wq/react";
8
+ import { Form } from "@wq/form-common";
9
+ import SubmitButton from "./SubmitButton.js";
10
+ import Alert from "react-native";
11
+ import PropTypes from "prop-types";
12
+
13
+ const DeleteFormFallback = {
14
+ messages: {
15
+ CONFIRM_DELETE: "Are you sure you want to delete this record?",
16
+ CONFIRM_DELETE_TITLE: "Confirm Deletion",
17
+ CONFIRM_DELETE_OK: "Yes, Delete",
18
+ CONFIRM_DELETE_CANCEL: "Cancel",
19
+ },
20
+ components: {
21
+ Form,
22
+ SubmitButton,
23
+ ...createFallbackComponents(["View", "HorizontalView"], "@wq/material"),
24
+ },
25
+ };
26
+
27
+ function DeleteForm({ action }) {
28
+ const { Form, SubmitButton, View, HorizontalView } = useComponents(),
29
+ confirmDelete = useMessage("CONFIRM_DELETE"),
30
+ confirmDeleteTitle = useMessage("CONFIRM_DELETE_TITLE"),
31
+ confirmDeleteOk = useMessage("CONFIRM_DELETE_OK"),
32
+ confirmDeleteCancel = useMessage("CONFIRM_DELETE_CANCEL");
33
+
34
+ async function confirmSubmit() {
35
+ return new Promise((resolve) => {
36
+ Alert.alert(
37
+ confirmDeleteTitle,
38
+ confirmDelete,
39
+ [
40
+ {
41
+ text: confirmDeleteCancel,
42
+ onPress() {
43
+ resolve(false);
44
+ },
45
+ style: "cancel",
46
+ },
47
+ {
48
+ text: confirmDeleteOk,
49
+ onPress() {
50
+ resolve(true);
51
+ },
52
+ style: "destructive",
53
+ },
54
+ ],
55
+ {
56
+ onDismiss() {
57
+ resolve(false);
58
+ },
59
+ },
60
+ );
61
+ });
62
+ }
63
+
64
+ return (
65
+ <Form
66
+ action={action}
67
+ method="DELETE"
68
+ backgroundSync={false}
69
+ onSubmit={confirmSubmit}
70
+ >
71
+ <HorizontalView>
72
+ <View />
73
+ <SubmitButton icon="delete" variant="text" color="secondary">
74
+ Delete
75
+ </SubmitButton>
76
+ </HorizontalView>
77
+ </Form>
78
+ );
79
+ }
80
+
81
+ DeleteForm.propTypes = {
82
+ action: PropTypes.string,
83
+ };
84
+
85
+ export default withWQ(DeleteForm, { fallback: DeleteFormFallback });
@@ -0,0 +1,43 @@
1
+ import React, { useEffect } from "react";
2
+ import { createFallbackComponent, useComponents, withWQ } from "@wq/react";
3
+ import PropTypes from "prop-types";
4
+
5
+ const DrawFallback = {
6
+ components: {
7
+ Geojson: createFallbackComponent("Geojson", "@wq/form", "AutoForm"),
8
+ useMapInstance: createFallbackComponent(
9
+ "useMapInstance",
10
+ "@wq/form",
11
+ "AutoForm",
12
+ ),
13
+ },
14
+ };
15
+
16
+ function Draw({ name, data, setData }) {
17
+ const { Geojson, useMapInstance } = useComponents(),
18
+ map = useMapInstance(name);
19
+
20
+ useEffect(() => {
21
+ if (!map) {
22
+ return;
23
+ }
24
+ map.on("click", onClick);
25
+ function onClick(evt) {
26
+ setData(evt.geometry);
27
+ }
28
+ return () => map.off("click", onClick);
29
+ }, [map]);
30
+
31
+ if (!data) {
32
+ return null;
33
+ }
34
+ return <Geojson active data={data} />;
35
+ }
36
+
37
+ Draw.propTypes = {
38
+ name: PropTypes.string,
39
+ data: PropTypes.object,
40
+ setData: PropTypes.func,
41
+ };
42
+
43
+ export default withWQ(Draw, { fallback: DrawFallback });
@@ -0,0 +1,4 @@
1
+ import { Fragment } from "react";
2
+ import { withWQ } from "@wq/react";
3
+
4
+ export default withWQ(Fragment, "Fieldset");
@@ -0,0 +1,27 @@
1
+ import React from "react";
2
+ import { useComponents, withWQ, createFallbackComponents } from "@wq/react";
3
+ import PropTypes from "prop-types";
4
+
5
+ const FieldsetArrayFallback = {
6
+ components: createFallbackComponents(["View", "Button"], "@wq/material"),
7
+ };
8
+
9
+ function FieldsetArray({ label, children, addRow }) {
10
+ const { View, Button } = useComponents();
11
+ return (
12
+ <View>
13
+ {children}
14
+ {addRow && (
15
+ <Button onClick={() => addRow()}>{`Add ${label}`}</Button>
16
+ )}
17
+ </View>
18
+ );
19
+ }
20
+
21
+ FieldsetArray.propTypes = {
22
+ label: PropTypes.string,
23
+ children: PropTypes.node,
24
+ addRow: PropTypes.func,
25
+ };
26
+
27
+ export default withWQ(FieldsetArray, { fallback: FieldsetArrayFallback });
@@ -0,0 +1,182 @@
1
+ import React, { useState } from "react";
2
+ import { useField } from "formik";
3
+ import { withWQ, useComponents, createFallbackComponents } from "@wq/react";
4
+ import { View, Text } from "react-native";
5
+ import { Button, List, Menu } from "react-native-paper";
6
+ import * as Application from "expo-application";
7
+ import * as DocumentPicker from "expo-document-picker";
8
+ import * as ImagePicker from "expo-image-picker";
9
+ import { format } from "@wq/form-common";
10
+ import HelperText from "./HelperText.js";
11
+ import PropTypes from "prop-types";
12
+
13
+ const FileFallback = {
14
+ components: createFallbackComponents(["Img", "Popup"], "@wq/material"),
15
+ };
16
+
17
+ function File({ name, accept, capture, required, label, hint }) {
18
+ const [, { value }, { setValue }] = useField(name),
19
+ [imageOpen, setImageOpen] = useState(false),
20
+ [menuOpen, setMenuOpen] = useState(false),
21
+ { Img, Popup } = useComponents(),
22
+ acceptImage = accept && accept.startsWith("image/"),
23
+ showImage = () => setImageOpen(true),
24
+ hideImage = () => setImageOpen(false),
25
+ showMenu = () => setMenuOpen(true),
26
+ hideMenu = () => setMenuOpen(false);
27
+
28
+ async function handleFile(mode) {
29
+ const value = await mode(accept, capture);
30
+ if (value || mode === clear) {
31
+ setValue(value);
32
+ }
33
+ hideMenu();
34
+ }
35
+
36
+ const modes = capture
37
+ ? [takePhoto]
38
+ : acceptImage
39
+ ? [takePhoto, pickFile]
40
+ : [pickFile];
41
+
42
+ if (value && !required) {
43
+ modes.push(clear);
44
+ }
45
+
46
+ return (
47
+ <View>
48
+ {label && <List.Subheader>{label}</List.Subheader>}
49
+ <View
50
+ style={{
51
+ flexDirection: "row",
52
+ alignItems: "center",
53
+ padding: 8,
54
+ }}
55
+ >
56
+ {isImage(value) && (
57
+ <Img
58
+ onPress={showImage}
59
+ style={{ width: 64, height: 64 }}
60
+ src={typeof value === "string" ? value : value.uri}
61
+ />
62
+ )}
63
+ {value && !isImage(value) && <List.Icon icon="attachment" />}
64
+ <Text style={{ flex: 1, padding: 8 }}>
65
+ {value ? value.name : "No file selected"}
66
+ </Text>
67
+ <Menu
68
+ visible={menuOpen}
69
+ onDismiss={hideMenu}
70
+ anchor={
71
+ <Button
72
+ onPress={
73
+ modes.length > 1
74
+ ? showMenu
75
+ : () => handleFile(modes[0])
76
+ }
77
+ icon={value ? "pencil" : "plus"}
78
+ mode="contained-tonal"
79
+ >
80
+ {value
81
+ ? "Change"
82
+ : capture
83
+ ? "Add Photo"
84
+ : acceptImage
85
+ ? "Add Image"
86
+ : "Add File"}
87
+ </Button>
88
+ }
89
+ >
90
+ {modes.map((mode) => (
91
+ <Menu.Item
92
+ key={mode.icon}
93
+ onPress={() => handleFile(mode)}
94
+ leadingIcon={mode.icon}
95
+ title={mode.label}
96
+ />
97
+ ))}
98
+ </Menu>
99
+ </View>
100
+ <HelperText name={name} hint={hint} />
101
+ <Popup open={value && imageOpen} onClose={hideImage}>
102
+ <Img
103
+ onPress={hideImage}
104
+ resizeMode="contain"
105
+ style={{
106
+ width: "90%",
107
+ height: "90%",
108
+ margin: "5%",
109
+ }}
110
+ src={typeof value === "string" ? value : value && value.uri}
111
+ />
112
+ </Popup>
113
+ </View>
114
+ );
115
+ }
116
+ File.propTypes = {
117
+ name: PropTypes.string,
118
+ accept: PropTypes.string,
119
+ capture: PropTypes.string,
120
+ required: PropTypes.bool,
121
+ label: PropTypes.string,
122
+ hint: PropTypes.string,
123
+ };
124
+
125
+ export default withWQ(File, { fallback: FileFallback });
126
+
127
+ function isImage(value) {
128
+ if (typeof value === "string") {
129
+ return IMAGE_EXTENSIONS.some((ext) =>
130
+ value.toLowerCase().endsWith(ext),
131
+ );
132
+ } else {
133
+ return (
134
+ value && value.type && value.type.startsWith("image/") && value.uri
135
+ );
136
+ }
137
+ }
138
+
139
+ function generateName() {
140
+ const appName = Application.applicationName.replace(/ /, ""),
141
+ timestamp = new Date(),
142
+ date = format.date(timestamp).replace(/-/g, ""),
143
+ time = format.time(timestamp).replace(/:/g, "");
144
+ return `${appName}_${date}_${time}.jpg`;
145
+ }
146
+
147
+ async function pickFile(accept) {
148
+ const result = await DocumentPicker.getDocumentAsync({ type: accept });
149
+ if (result.assets && result.assets.length > 0) {
150
+ const { name, mimeType: type, uri } = result.assets[0];
151
+ return { name, type, uri };
152
+ }
153
+ }
154
+ pickFile.icon = "folder";
155
+ pickFile.label = "Pick File";
156
+
157
+ async function takePhoto(accept, capture) {
158
+ const options = {};
159
+ if (capture) {
160
+ options.cameraType =
161
+ capture === "user"
162
+ ? ImagePicker.CameraType.front
163
+ : ImagePicker.CameraType.back;
164
+ }
165
+ const result = await ImagePicker.launchCameraAsync(options);
166
+ if (result.assets && result.assets.length > 0) {
167
+ const name = generateName(),
168
+ type = "image/jpeg",
169
+ { uri } = result.assets[0];
170
+ return { name, type, uri };
171
+ }
172
+ }
173
+ takePhoto.icon = "camera";
174
+ takePhoto.label = "Take Photo";
175
+
176
+ function clear() {
177
+ return null;
178
+ }
179
+ clear.icon = "delete";
180
+ clear.label = "Remove";
181
+
182
+ const IMAGE_EXTENSIONS = ["jpg", "jpeg", "png", "gif"];
@@ -0,0 +1,15 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { List } from "react-native-paper";
4
+ import FieldsetArray from "./FieldsetArray.js";
5
+
6
+ function FileArray(props) {
7
+ return (
8
+ <>
9
+ {props.label && <List.Subheader>{props.label}</List.Subheader>}
10
+ <FieldsetArray {...props} />
11
+ </>
12
+ );
13
+ }
14
+
15
+ export default withWQ(FileArray);
@@ -0,0 +1,4 @@
1
+ import { Fragment } from "react";
2
+ import { withWQ } from "@wq/react";
3
+
4
+ export default withWQ(Fragment, "FlatFieldset");
@@ -0,0 +1,18 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { useField } from "formik";
4
+ import { HelperText } from "react-native-paper";
5
+
6
+ function FormError(props) {
7
+ const [, { error }] = useField("__other__");
8
+ if (!error) {
9
+ return null;
10
+ }
11
+ return (
12
+ <HelperText type="error" {...props}>
13
+ {error}
14
+ </HelperText>
15
+ );
16
+ }
17
+
18
+ export default withWQ(FormError);
@@ -0,0 +1,4 @@
1
+ import { Fragment } from "react";
2
+ import { withWQ } from "@wq/react";
3
+
4
+ export default withWQ(Fragment, "FormRoot");
@@ -0,0 +1,48 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import PropTypes from "prop-types";
4
+ import Icon from "react-native-paper/src/components/Icon";
5
+
6
+ function GeoHelpIcon({ name, type }) {
7
+ const iconClass = getIconClass(name, type);
8
+ if (!iconClass) {
9
+ return `{${name}}`;
10
+ }
11
+
12
+ return <Icon source={iconClass} />;
13
+ }
14
+
15
+ GeoHelpIcon.propTypes = {
16
+ name: PropTypes.string,
17
+ type: PropTypes.string,
18
+ };
19
+
20
+ const SHAPES = ["point", "line", "polygon"],
21
+ NAMES = SHAPES.map((shape) => `${shape.toUpperCase()}_ICON`),
22
+ ICONS = {
23
+ point: "map-marker",
24
+ line: "vector-polyline",
25
+ polygon: "vector-polygon",
26
+ };
27
+
28
+ function getIconClass(iconName, drawType) {
29
+ let shape = null;
30
+
31
+ if (iconName === "TOOL_ICON") {
32
+ if (drawType === "line_string") {
33
+ shape = "line";
34
+ } else if (SHAPES.includes(drawType)) {
35
+ shape = drawType;
36
+ }
37
+ } else if (NAMES.includes(iconName)) {
38
+ shape = SHAPES[NAMES.indexOf(iconName)];
39
+ }
40
+
41
+ if (shape) {
42
+ return ICONS[shape];
43
+ } else {
44
+ return null;
45
+ }
46
+ }
47
+
48
+ export default withWQ(GeoHelpIcon);
@@ -0,0 +1,32 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { useFormikContext, getIn } from "formik";
4
+ import { HelperText as FormHelperText } from "react-native-paper";
5
+ import PropTypes from "prop-types";
6
+
7
+ function HelperText({ name, hint }) {
8
+ const { errors, touched } = useFormikContext(),
9
+ error = getIn(errors, name),
10
+ showError = !!error && !!getIn(touched, name);
11
+
12
+ if (showError) {
13
+ hint = error;
14
+ }
15
+
16
+ if (!hint) {
17
+ return null;
18
+ }
19
+
20
+ return (
21
+ <FormHelperText type={showError ? "error" : "info"}>
22
+ {hint}
23
+ </FormHelperText>
24
+ );
25
+ }
26
+
27
+ HelperText.propTypes = {
28
+ name: PropTypes.string,
29
+ hint: PropTypes.string,
30
+ };
31
+
32
+ export default withWQ(HelperText);
@@ -0,0 +1,26 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { Field } from "formik";
4
+ import HelperText from "./HelperText.js";
5
+ import PropTypes from "prop-types";
6
+
7
+ function Empty() {
8
+ return null;
9
+ }
10
+
11
+ function Hidden(props) {
12
+ const { name, hint } = props;
13
+ return (
14
+ <>
15
+ <Field {...props} component={Empty} />
16
+ <HelperText name={name} hint={hint} />
17
+ </>
18
+ );
19
+ }
20
+
21
+ Hidden.propTypes = {
22
+ name: PropTypes.string,
23
+ hint: PropTypes.string,
24
+ };
25
+
26
+ export default withWQ(Hidden);
@@ -0,0 +1,20 @@
1
+ import React from "react";
2
+ import { useComponents, withWQ, createFallbackComponent } from "@wq/react";
3
+ import { useFormikContext } from "formik";
4
+
5
+ const IconSubmitButtonFallback = {
6
+ components: {
7
+ IconButton: createFallbackComponent("IconButton", "@wq/material"),
8
+ },
9
+ };
10
+
11
+ function IconSubmitButton(props) {
12
+ const { IconButton } = useComponents(),
13
+ { isSubmitting, submitForm } = useFormikContext();
14
+
15
+ return (
16
+ <IconButton disabled={isSubmitting} onPress={submitForm} {...props} />
17
+ );
18
+ }
19
+
20
+ export default withWQ(IconSubmitButton, { fallback: IconSubmitButtonFallback });
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import File from "./File.js";
4
+
5
+ function Image(props) {
6
+ return <File accept="image/*" {...props} />;
7
+ }
8
+
9
+ export default withWQ(Image);
@@ -0,0 +1,93 @@
1
+ import React, { useState } from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { View } from "react-native";
4
+ import { TextInput } from "react-native-paper";
5
+ import { useField } from "formik";
6
+ import { useHtmlInput } from "@wq/form-common";
7
+ import HelperText from "./HelperText.js";
8
+ import PropTypes from "prop-types";
9
+
10
+ const keyboards = {
11
+ int: "number-pad",
12
+ decimal: "decimal-pad",
13
+ tel: "phone-pad",
14
+ email: "email-address",
15
+ };
16
+
17
+ function Input(props) {
18
+ const { name, type, label, hint, style, min, max, step } = props,
19
+ { maxLength } = useHtmlInput(props),
20
+ [, meta, helpers] = useField(name),
21
+ { value } = meta,
22
+ [formatValue, setFormatValue] = useState(
23
+ type === "int" || type === "decimal"
24
+ ? typeof value === "number"
25
+ ? "" + value
26
+ : ""
27
+ : value,
28
+ ),
29
+ { setValue, setTouched } = helpers;
30
+
31
+ function handleChange(nextValue) {
32
+ let value = nextValue;
33
+ if (type === "int" || type === "decimal") {
34
+ if (type === "int") {
35
+ value = parseInt(value);
36
+ } else {
37
+ value = +value;
38
+ }
39
+ if (Number.isNaN(value)) {
40
+ setValue(null);
41
+ setFormatValue("");
42
+ } else {
43
+ if (typeof min === "number" && value < min) {
44
+ value = min;
45
+ }
46
+ if (typeof max === "number" && value > max) {
47
+ value = max;
48
+ }
49
+ if (step) {
50
+ value = +(Math.round(value / step) * step).toFixed(
51
+ step < 1 ? step.toString().split(".")[1].length : 0,
52
+ );
53
+ }
54
+ setValue(value);
55
+ setFormatValue(
56
+ +nextValue === value ? nextValue : value.toString(),
57
+ );
58
+ }
59
+ } else {
60
+ setValue(value);
61
+ setFormatValue(value);
62
+ }
63
+ }
64
+
65
+ return (
66
+ <View style={{ flex: (style || {}).flex }}>
67
+ <TextInput
68
+ label={label}
69
+ multiline={type === "text"}
70
+ keyboardType={keyboards[type] || "default"}
71
+ maxLength={maxLength}
72
+ onChangeText={handleChange}
73
+ onBlur={() => setTouched(true)}
74
+ value={formatValue}
75
+ style={style}
76
+ />
77
+ <HelperText name={name} hint={hint} />
78
+ </View>
79
+ );
80
+ }
81
+
82
+ Input.propTypes = {
83
+ name: PropTypes.string,
84
+ type: PropTypes.string,
85
+ label: PropTypes.string,
86
+ hint: PropTypes.string,
87
+ style: PropTypes.object,
88
+ min: PropTypes.number,
89
+ max: PropTypes.number,
90
+ step: PropTypes.number,
91
+ };
92
+
93
+ export default withWQ(Input);
@@ -0,0 +1,35 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { View } from "react-native";
4
+ import { RadioButton, List } from "react-native-paper";
5
+ import HelperText from "./HelperText.js";
6
+ import { useField } from "formik";
7
+ import PropTypes from "prop-types";
8
+
9
+ function Radio({ name, choices, label, hint }) {
10
+ const [, { value }, { setValue }] = useField(name);
11
+ return (
12
+ <View>
13
+ <List.Subheader>{label}</List.Subheader>
14
+ <RadioButton.Group onValueChange={setValue} value={value}>
15
+ {choices.map((choice) => (
16
+ <RadioButton.Item
17
+ key={choice.name}
18
+ value={choice.name}
19
+ label={choice.label}
20
+ />
21
+ ))}
22
+ </RadioButton.Group>
23
+ <HelperText name={name} hint={hint} />
24
+ </View>
25
+ );
26
+ }
27
+
28
+ Radio.propTypes = {
29
+ name: PropTypes.string,
30
+ label: PropTypes.string,
31
+ hint: PropTypes.string,
32
+ choices: PropTypes.arrayOf(PropTypes.object),
33
+ };
34
+
35
+ export default withWQ(Radio);
@@ -0,0 +1,61 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import RNPickerSelect from "react-native-picker-select";
4
+ import { View } from "react-native";
5
+ import { TextInput, useTheme } from "react-native-paper";
6
+ import { useField } from "formik";
7
+ import HelperText from "./HelperText.js";
8
+ import PropTypes from "prop-types";
9
+
10
+ function Select({ name, choices, label, hint }) {
11
+ const theme = useTheme(),
12
+ [, meta, helpers] = useField(name),
13
+ { value } = meta,
14
+ { setValue } = helpers;
15
+
16
+ const styles = {
17
+ inputIOS: {
18
+ color: theme.colors.text,
19
+ marginLeft: 12,
20
+ paddingTop: 32,
21
+ fontSize: 16,
22
+ },
23
+ inputAndroid: {
24
+ color: theme.colors.text,
25
+ marginLeft: 4,
26
+ paddingTop: 40,
27
+ paddingBottom: 40,
28
+ marginBottom: -40,
29
+ },
30
+ };
31
+
32
+ return (
33
+ <View>
34
+ <TextInput
35
+ label={label}
36
+ value={"-"}
37
+ render={() => (
38
+ <RNPickerSelect
39
+ value={value}
40
+ onValueChange={setValue}
41
+ items={choices.map(({ name, label }) => ({
42
+ value: name,
43
+ label,
44
+ }))}
45
+ style={styles}
46
+ />
47
+ )}
48
+ />
49
+ <HelperText name={name} hint={hint} />
50
+ </View>
51
+ );
52
+ }
53
+
54
+ Select.propTypes = {
55
+ name: PropTypes.string,
56
+ label: PropTypes.string,
57
+ hint: PropTypes.string,
58
+ choices: PropTypes.arrayOf(PropTypes.object),
59
+ };
60
+
61
+ export default withWQ(Select);
@@ -0,0 +1,25 @@
1
+ import React from "react";
2
+ import { withWQ, useComponents, createFallbackComponent } from "@wq/react";
3
+ import { useFormikContext } from "formik";
4
+
5
+ const SubmitButtonFallback = {
6
+ components: {
7
+ Button: createFallbackComponent("Button", "@wq/material"),
8
+ },
9
+ };
10
+
11
+ function SubmitButton(props) {
12
+ const { Button } = useComponents(),
13
+ { isSubmitting, submitForm } = useFormikContext();
14
+
15
+ return (
16
+ <Button
17
+ mode="contained"
18
+ disabled={isSubmitting}
19
+ onPress={submitForm}
20
+ {...props}
21
+ />
22
+ );
23
+ }
24
+
25
+ export default withWQ(SubmitButton, { fallback: SubmitButtonFallback });
@@ -0,0 +1,41 @@
1
+ import React from "react";
2
+ import { withWQ } from "@wq/react";
3
+ import { View, Text } from "react-native";
4
+ import { ToggleButton, List } from "react-native-paper";
5
+ import HelperText from "./HelperText.js";
6
+ import { useField } from "formik";
7
+ import PropTypes from "prop-types";
8
+
9
+ function Toggle({ name, choices, label, hint, style }) {
10
+ const [, { value }, { setValue }] = useField(name);
11
+ return (
12
+ <View>
13
+ <List.Subheader>{label}</List.Subheader>
14
+ <ToggleButton.Row
15
+ onValueChange={setValue}
16
+ value={value}
17
+ style={{ paddingLeft: 16, paddingBottom: 8, ...style }}
18
+ >
19
+ {choices.map((choice) => (
20
+ <ToggleButton
21
+ key={choice.name}
22
+ value={choice.name}
23
+ icon={() => <Text>{choice.label}</Text>}
24
+ style={{ width: undefined, padding: 8 }}
25
+ />
26
+ ))}
27
+ </ToggleButton.Row>
28
+ <HelperText name={name} hint={hint} />
29
+ </View>
30
+ );
31
+ }
32
+
33
+ Toggle.propTypes = {
34
+ name: PropTypes.string,
35
+ label: PropTypes.string,
36
+ hint: PropTypes.string,
37
+ choices: PropTypes.arrayOf(PropTypes.object),
38
+ style: PropTypes.object,
39
+ };
40
+
41
+ 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";