@neko-os/ui 0.5.2 → 0.5.3

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 CHANGED
@@ -1,3 +1,5 @@
1
1
  # Neko UI
2
2
 
3
3
  Universal React Native UI library (works on react-native, react-native-web and react).
4
+
5
+ https://nekoui.surge.sh/guides/install
@@ -62,6 +62,7 @@ export function FormItem(_ref)
62
62
 
63
63
  var handleBlur = function handleBlur(e, originalOnBlur) {
64
64
  if (originalOnBlur) originalOnBlur(e);
65
+ form.markTouched(listPath);
65
66
 
66
67
  if (shouldValidateOn('onBlur', rules, validateTrigger)) {
67
68
  form.validateField(listPath, 'onBlur');
@@ -1,4 +1,4 @@
1
- function asyncGeneratorStep(n, t, e, r, o, a, c) {try {var i = n[a](c),u = i.value;} catch (n) {return void e(n);}i.done ? t(u) : Promise.resolve(u).then(r, o);}function _asyncToGenerator(n) {return function () {var t = this,e = arguments;return new Promise(function (r, o) {var a = n.apply(t, e);function _next(n) {asyncGeneratorStep(a, r, o, _next, _throw, "next", n);}function _throw(n) {asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);}_next(void 0);});};}function _slicedToArray(r, e) {return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();}function _nonIterableRest() {throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");}function _unsupportedIterableToArray(r, a) {if (r) {if ("string" == typeof r) return _arrayLikeToArray(r, a);var t = {}.toString.call(r).slice(8, -1);return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;}}function _arrayLikeToArray(r, a) {(null == a || a > r.length) && (a = r.length);for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];return n;}function _iterableToArrayLimit(r, l) {var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];if (null != t) {var e,n,i,u,a = [],f = !0,o = !1;try {if (i = (t = t.call(r)).next, 0 === l) {if (Object(t) !== t) return;f = !1;} else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);} catch (r) {o = !0, n = r;} finally {try {if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;} finally {if (o) throw n;}}return a;}}function _arrayWithHoles(r) {if (Array.isArray(r)) return r;}import { assocPath, path } from 'ramda';
1
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {try {var i = n[a](c),u = i.value;} catch (n) {return void e(n);}i.done ? t(u) : Promise.resolve(u).then(r, o);}function _asyncToGenerator(n) {return function () {var t = this,e = arguments;return new Promise(function (r, o) {var a = n.apply(t, e);function _next(n) {asyncGeneratorStep(a, r, o, _next, _throw, "next", n);}function _throw(n) {asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);}_next(void 0);});};}function _slicedToArray(r, e) {return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();}function _nonIterableRest() {throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");}function _iterableToArrayLimit(r, l) {var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];if (null != t) {var e,n,i,u,a = [],f = !0,o = !1;try {if (i = (t = t.call(r)).next, 0 === l) {if (Object(t) !== t) return;f = !1;} else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);} catch (r) {o = !0, n = r;} finally {try {if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;} finally {if (o) throw n;}}return a;}}function _arrayWithHoles(r) {if (Array.isArray(r)) return r;}function _toConsumableArray(r) {return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();}function _nonIterableSpread() {throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");}function _unsupportedIterableToArray(r, a) {if (r) {if ("string" == typeof r) return _arrayLikeToArray(r, a);var t = {}.toString.call(r).slice(8, -1);return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;}}function _iterableToArray(r) {if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);}function _arrayWithoutHoles(r) {if (Array.isArray(r)) return _arrayLikeToArray(r);}function _arrayLikeToArray(r, a) {(null == a || a > r.length) && (a = r.length);for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];return n;}import { assocPath, path } from 'ramda';
2
2
  import React from 'react';
3
3
  import { validateRules, validateAllFields, normalizeRules } from "./validation";
4
4
 
@@ -6,6 +6,8 @@ export function useNewForm() {var _ref = arguments.length > 0 && arguments[0] !=
6
6
  var valuesRef = React.useRef(Object.assign({}, initialValues));
7
7
  var initialValuesRef = React.useRef(Object.assign({}, initialValues));
8
8
  var errorsRef = React.useRef({});
9
+ var touchedRef = React.useRef(new Set());
10
+ var dirtyRef = React.useRef(new Set());
9
11
  var listenersRef = React.useRef({});
10
12
  var errorListenersRef = React.useRef({});
11
13
  var rulesRegistryRef = React.useRef(new Map());
@@ -45,8 +47,37 @@ export function useNewForm() {var _ref = arguments.length > 0 && arguments[0] !=
45
47
  });
46
48
  };
47
49
 
50
+ var markTouched = function markTouched(name) {
51
+ touchedRef.current.add(toKey(name));
52
+ };
53
+
54
+ var updateDirty = function updateDirty(name) {
55
+ var key = toKey(name);
56
+ var current = path(toPath(name), valuesRef.current);
57
+ var initial = path(toPath(name), initialValuesRef.current);
58
+ if (current === initial) {
59
+ dirtyRef.current.delete(key);
60
+ } else {
61
+ dirtyRef.current.add(key);
62
+ }
63
+ };
64
+
65
+ var isTouched = function isTouched(name) {
66
+ if (!name) return touchedRef.current.size > 0;
67
+ return touchedRef.current.has(toKey(name));
68
+ };
69
+
70
+ var isDirty = function isDirty(name) {
71
+ if (!name) return dirtyRef.current.size > 0;
72
+ return dirtyRef.current.has(toKey(name));
73
+ };
74
+
75
+ var getTouchedFields = function getTouchedFields() {return _toConsumableArray(touchedRef.current);};
76
+ var getDirtyFields = function getDirtyFields() {return _toConsumableArray(dirtyRef.current);};
77
+
48
78
  var setFieldValue = function setFieldValue(name, value) {
49
79
  valuesRef.current = assocPath(toPath(name), value, valuesRef.current);
80
+ updateDirty(name);
50
81
  notify(name);
51
82
  var fn = callbacksRef.current.onValuesChange;
52
83
  if (fn) fn(name, valuesRef.current);
@@ -69,6 +100,8 @@ export function useNewForm() {var _ref = arguments.length > 0 && arguments[0] !=
69
100
  var resetFields = function resetFields() {
70
101
  valuesRef.current = Object.assign({}, initialValuesRef.current);
71
102
  errorsRef.current = {};
103
+ touchedRef.current.clear();
104
+ dirtyRef.current.clear();
72
105
  notifyAll();
73
106
  notifyAllErrors();
74
107
  };
@@ -196,6 +229,11 @@ export function useNewForm() {var _ref = arguments.length > 0 && arguments[0] !=
196
229
  getError: getError,
197
230
  setError: setError,
198
231
  clearErrors: clearErrors,
232
+ markTouched: markTouched,
233
+ isTouched: isTouched,
234
+ isDirty: isDirty,
235
+ getTouchedFields: getTouchedFields,
236
+ getDirtyFields: getDirtyFields,
199
237
  registerListener: registerListener,
200
238
  registerErrorListener: registerErrorListener,
201
239
  registerRules: registerRules,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neko-os/ui",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "author": "Christian Storch <ccstorch@gmail.com>",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -62,6 +62,7 @@ export function FormItem({
62
62
 
63
63
  const handleBlur = (e, originalOnBlur) => {
64
64
  if (originalOnBlur) originalOnBlur(e)
65
+ form.markTouched(listPath)
65
66
 
66
67
  if (shouldValidateOn('onBlur', rules, validateTrigger)) {
67
68
  form.validateField(listPath, 'onBlur')
@@ -6,6 +6,8 @@ export function useNewForm({ initialValues = {}, validate, onSubmit, onValuesCha
6
6
  const valuesRef = React.useRef({ ...initialValues })
7
7
  const initialValuesRef = React.useRef({ ...initialValues })
8
8
  const errorsRef = React.useRef({}) // Flat structure: { 'users': 'error', 'users.0.name': 'error' }
9
+ const touchedRef = React.useRef(new Set())
10
+ const dirtyRef = React.useRef(new Set())
9
11
  const listenersRef = React.useRef({})
10
12
  const errorListenersRef = React.useRef({})
11
13
  const rulesRegistryRef = React.useRef(new Map())
@@ -45,8 +47,37 @@ export function useNewForm({ initialValues = {}, validate, onSubmit, onValuesCha
45
47
  })
46
48
  }
47
49
 
50
+ const markTouched = (name) => {
51
+ touchedRef.current.add(toKey(name))
52
+ }
53
+
54
+ const updateDirty = (name) => {
55
+ const key = toKey(name)
56
+ const current = path(toPath(name), valuesRef.current)
57
+ const initial = path(toPath(name), initialValuesRef.current)
58
+ if (current === initial) {
59
+ dirtyRef.current.delete(key)
60
+ } else {
61
+ dirtyRef.current.add(key)
62
+ }
63
+ }
64
+
65
+ const isTouched = (name) => {
66
+ if (!name) return touchedRef.current.size > 0
67
+ return touchedRef.current.has(toKey(name))
68
+ }
69
+
70
+ const isDirty = (name) => {
71
+ if (!name) return dirtyRef.current.size > 0
72
+ return dirtyRef.current.has(toKey(name))
73
+ }
74
+
75
+ const getTouchedFields = () => [...touchedRef.current]
76
+ const getDirtyFields = () => [...dirtyRef.current]
77
+
48
78
  const setFieldValue = (name, value) => {
49
79
  valuesRef.current = assocPath(toPath(name), value, valuesRef.current)
80
+ updateDirty(name)
50
81
  notify(name)
51
82
  const fn = callbacksRef.current.onValuesChange
52
83
  if (fn) fn(name, valuesRef.current)
@@ -69,6 +100,8 @@ export function useNewForm({ initialValues = {}, validate, onSubmit, onValuesCha
69
100
  const resetFields = () => {
70
101
  valuesRef.current = { ...initialValuesRef.current }
71
102
  errorsRef.current = {}
103
+ touchedRef.current.clear()
104
+ dirtyRef.current.clear()
72
105
  notifyAll()
73
106
  notifyAllErrors()
74
107
  }
@@ -196,6 +229,11 @@ export function useNewForm({ initialValues = {}, validate, onSubmit, onValuesCha
196
229
  getError,
197
230
  setError,
198
231
  clearErrors,
232
+ markTouched,
233
+ isTouched,
234
+ isDirty,
235
+ getTouchedFields,
236
+ getDirtyFields,
199
237
  registerListener,
200
238
  registerErrorListener,
201
239
  registerRules,