@startlibs/form 1.1.1 → 1.1.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.
@@ -40,7 +40,7 @@ const useDraftConfirmation = (submit, discard, meta) => {
40
40
  const Dialog = startlibs.dialogs[dialog.isOpen];
41
41
  if (process.env.NODE_ENV !== 'production') {
42
42
  if (dialog.isOpen && !Dialog) {
43
- console.log("Dialog [".concat(dialogType, "] not registered in startlibs.dialogs"));
43
+ console.log("Dialog [".concat(dialog.isOpen, "] not registered in startlibs.dialogs"));
44
44
  }
45
45
  }
46
46
  const feedback = dialog.isOpen ? /*#__PURE__*/React.createElement(Dialog, _extends({
@@ -1 +1 @@
1
- {"version":3,"file":"useDraftConfirmation.mjs","sources":["../src/useDraftConfirmation.jsx"],"sourcesContent":["import React, {useContext} from 'react'\nimport {useToggle, useRefToggle, StartlibsContext, useRefState} from '@startlibs/core'\nimport {callIfFunction} from '@startlibs/utils'\n\nexport const useDraftConfirmation = (submit, discard, meta) => {\n const loading = useToggle()\n const dialog = useToggle()\n const draft = useRefToggle()\n const promiseResolver = useRefState()\n const startlibs = useContext(StartlibsContext)\n\n const confirmation = (dialogType = 'submit-draft', callback) => {\n if (callIfFunction(draft.isOpen)) {\n dialog.openWith(dialogType)\n return new Promise((res) =>\n promiseResolver.set(() => (r) => {\n dialog.close()\n callIfFunction(callback,r)\n res(r)\n }))\n } else {\n return Promise.resolve()\n }\n }\n\n\n const submitAndResolve = async () => {\n loading.open()\n try {\n await submit()\n callIfFunction(promiseResolver.get())\n } catch (e) { } finally {\n dialog.close()\n loading.close()\n }\n }\n\n const discardAndResolve = () => {\n callIfFunction(discard)\n callIfFunction(promiseResolver.get())\n }\n\n const Dialog = startlibs.dialogs[dialog.isOpen]\n if (process.env.NODE_ENV !== 'production') {\n if (dialog.isOpen && !Dialog) { console.log(`Dialog [${dialogType}] not registered in startlibs.dialogs`) }\n }\n\n const feedback =\n dialog.isOpen\n ? <Dialog\n submit={submitAndResolve}\n discard={discardAndResolve}\n isLoading={loading.isOpen}\n cancel={dialog.close}\n {...meta}\n />\n : null\n\n return [feedback, draft, confirmation]\n}"],"names":["useDraftConfirmation","submit","discard","meta","loading","useToggle","dialog","draft","useRefToggle","promiseResolver","useRefState","startlibs","useContext","StartlibsContext","confirmation","dialogType","arguments","length","undefined","callback","callIfFunction","isOpen","openWith","Promise","res","set","r","close","resolve","submitAndResolve","open","get","e","discardAndResolve","Dialog","dialogs","process","env","NODE_ENV","console","log","concat","feedback","React","createElement","_extends","isLoading","cancel"],"mappings":";;;;;AAIO,MAAMA,oBAAoB,GAAGA,CAACC,MAAM,EAAEC,OAAO,EAAEC,IAAI,KAAK;AAC7D,EAAA,MAAMC,OAAO,GAAGC,SAAS,EAAE,CAAA;AAC3B,EAAA,MAAMC,MAAM,GAAGD,SAAS,EAAE,CAAA;AAC1B,EAAA,MAAME,KAAK,GAAGC,YAAY,EAAE,CAAA;AAC5B,EAAA,MAAMC,eAAe,GAAGC,WAAW,EAAE,CAAA;AACrC,EAAA,MAAMC,SAAS,GAAGC,UAAU,CAACC,gBAAgB,CAAC,CAAA;AAE9C,EAAA,MAAMC,YAAY,GAAG,YAA2C;AAAA,IAAA,IAA1CC,UAAU,GAAAC,SAAA,CAAAC,MAAA,GAAA,CAAA,IAAAD,SAAA,CAAA,CAAA,CAAA,KAAAE,SAAA,GAAAF,SAAA,CAAA,CAAA,CAAA,GAAG,cAAc,CAAA;IAAA,IAAEG,QAAQ,GAAAH,SAAA,CAAAC,MAAA,GAAAD,CAAAA,GAAAA,SAAA,MAAAE,SAAA,CAAA;AACzD,IAAA,IAAIE,cAAc,CAACb,KAAK,CAACc,MAAM,CAAC,EAAE;AAChCf,MAAAA,MAAM,CAACgB,QAAQ,CAACP,UAAU,CAAC,CAAA;MAC3B,OAAO,IAAIQ,OAAO,CAAEC,GAAG,IACrBf,eAAe,CAACgB,GAAG,CAAC,MAAOC,CAAC,IAAK;QAC/BpB,MAAM,CAAC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E+B,iBAAkB;IAC3Ba,SAAS,EAAE1C,OAAO,CAACiB,MAAO;IAC1B0B,MAAM,EAAEzC,MAAM,CAACqB,KAAAA;AAAM,GAAA,EACjBxB,IAAI,CACT,CAAC,GACA,IAAI,CAAA;AAER,EAAA,OAAO,CAACuC,QAAQ,EAAEnC,KAAK,EAAEO,YAAY,CAAC,CAAA;AACxC;;;;"}
1
+ {"version":3,"file":"useDraftConfirmation.mjs","sources":["../src/useDraftConfirmation.jsx"],"sourcesContent":["import React, {useContext} from 'react'\nimport {useToggle, useRefToggle, StartlibsContext, useRefState} from '@startlibs/core'\nimport {callIfFunction} from '@startlibs/utils'\n\nexport const useDraftConfirmation = (submit, discard, meta) => {\n const loading = useToggle()\n const dialog = useToggle()\n const draft = useRefToggle()\n const promiseResolver = useRefState()\n const startlibs = useContext(StartlibsContext)\n\n const confirmation = (dialogType = 'submit-draft', callback) => {\n if (callIfFunction(draft.isOpen)) {\n dialog.openWith(dialogType)\n return new Promise((res) =>\n promiseResolver.set(() => (r) => {\n dialog.close()\n callIfFunction(callback,r)\n res(r)\n }))\n } else {\n return Promise.resolve()\n }\n }\n\n\n const submitAndResolve = async () => {\n loading.open()\n try {\n await submit()\n callIfFunction(promiseResolver.get())\n } catch (e) { } finally {\n dialog.close()\n loading.close()\n }\n }\n\n const discardAndResolve = () => {\n callIfFunction(discard)\n callIfFunction(promiseResolver.get())\n }\n\n const Dialog = startlibs.dialogs[dialog.isOpen]\n if (process.env.NODE_ENV !== 'production') {\n if (dialog.isOpen && !Dialog) { console.log(`Dialog [${dialog.isOpen}] not registered in startlibs.dialogs`) }\n }\n\n const feedback =\n dialog.isOpen\n ? <Dialog\n submit={submitAndResolve}\n discard={discardAndResolve}\n isLoading={loading.isOpen}\n cancel={dialog.close}\n {...meta}\n />\n : null\n\n return [feedback, draft, confirmation]\n}"],"names":["useDraftConfirmation","submit","discard","meta","loading","useToggle","dialog","draft","useRefToggle","promiseResolver","useRefState","startlibs","useContext","StartlibsContext","confirmation","dialogType","arguments","length","undefined","callback","callIfFunction","isOpen","openWith","Promise","res","set","r","close","resolve","submitAndResolve","open","get","e","discardAndResolve","Dialog","dialogs","process","env","NODE_ENV","console","log","concat","feedback","React","createElement","_extends","isLoading","cancel"],"mappings":";;;;;AAIO,MAAMA,oBAAoB,GAAGA,CAACC,MAAM,EAAEC,OAAO,EAAEC,IAAI,KAAK;AAC7D,EAAA,MAAMC,OAAO,GAAGC,SAAS,EAAE,CAAA;AAC3B,EAAA,MAAMC,MAAM,GAAGD,SAAS,EAAE,CAAA;AAC1B,EAAA,MAAME,KAAK,GAAGC,YAAY,EAAE,CAAA;AAC5B,EAAA,MAAMC,eAAe,GAAGC,WAAW,EAAE,CAAA;AACrC,EAAA,MAAMC,SAAS,GAAGC,UAAU,CAACC,gBAAgB,CAAC,CAAA;AAE9C,EAAA,MAAMC,YAAY,GAAG,YAA2C;AAAA,IAAA,IAA1CC,UAAU,GAAAC,SAAA,CAAAC,MAAA,GAAA,CAAA,IAAAD,SAAA,CAAA,CAAA,CAAA,KAAAE,SAAA,GAAAF,SAAA,CAAA,CAAA,CAAA,GAAG,cAAc,CAAA;IAAA,IAAEG,QAAQ,GAAAH,SAAA,CAAAC,MAAA,GAAAD,CAAAA,GAAAA,SAAA,MAAAE,SAAA,CAAA;AACzD,IAAA,IAAIE,cAAc,CAACb,KAAK,CAACc,MAAM,CAAC,EAAE;AAChCf,MAAAA,MAAM,CAACgB,QAAQ,CAACP,UAAU,CAAC,CAAA;MAC3B,OAAO,IAAIQ,OAAO,CAAEC,GAAG,IACrBf,eAAe,CAACgB,GAAG,CAAC,MAAOC,CAAC,IAAK;QAC/BpB,MAAM,CAACqB,KAAK,EAAE,CAAA;AACdP,QAAAA,cAAc,CAACD,QAAQ,EAACO,CAAC,CAAC,CAAA;QAC1BF,GAAG,CAACE,CAAC,CAAC,CAAA;AACR,OAAC,CAAC,CAAC,CAAA;AACP,KAAC,MAAM;AACL,MAAA,OAAOH,OAAO,CAACK,OAA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C,QAAQ,EAAEnC,KAAK,EAAEO,YAAY,CAAC,CAAA;AACxC;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startlibs/form",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Startlibs Form",
5
5
  "main": "lib/index.cjs",
6
6
  "exports": {
@@ -1,5 +1,5 @@
1
1
  import React, {useEffect, useRef, useState} from 'react'
2
- import ContentEditable from 'react-contenteditable'
2
+ import ContentEditableModule from 'react-contenteditable'
3
3
  import {useRefState, useToggle, useEnsureRef} from '@startlibs/core'
4
4
  import {ContextMenu, Li, Loading, Icon} from '@startlibs/components'
5
5
  import {Errors, useProvideErrors} from './Errors'
@@ -10,6 +10,9 @@ import {callIfFunction, stopPropagation, constrainEvent, getColor, media, custom
10
10
  import styled, {css} from 'styled-components'
11
11
  import {darken} from 'polished'
12
12
 
13
+ // Node/esbuild interop em .mjs devolve module.exports; Rollup devolve o default. Cobre os dois.
14
+ const ContentEditable = ContentEditableModule.default || ContentEditableModule
15
+
13
16
  const ua = navigator.userAgent.toLowerCase();
14
17
  const isSafari = ua.indexOf('safari') >= 0 && ua.indexOf('chrome') < 0
15
18
 
@@ -608,15 +611,24 @@ export class AutoCompleteTextbox extends React.Component {
608
611
  return nextProps.value !== cleanText(this.el.innerText) || !!Object.keys(nextProps).find((k) => nextProps[k] !== this.props[k])
609
612
  }
610
613
 
611
- componentDidUpdate(oldProps) {
614
+ // Only a programmatic change (not the user typing) should move the caret to the end
615
+ getSnapshotBeforeUpdate() {
616
+ return this.props.value !== cleanText(this.el.innerText)
617
+ }
618
+
619
+ componentDidUpdate(oldProps, prevState, changedOutside) {
620
+ let rewritten = false
612
621
  if (this.props.value.slice(-1) === ' ' && /\S/.test(this.el.textContent.slice(-1))) { // does not have blank space as last
613
622
  this.el.innerHTML = this.props.value.replace(/\s/, '&nbsp;')
623
+ rewritten = true
614
624
  }
615
- if (this.props.value !== oldProps.value && document.activeElement === this.el) {
625
+ if (this.props.value !== oldProps.value && (changedOutside || rewritten) && document.activeElement === this.el) {
616
626
  setSelectionRange(this.el.firstChild || this.el, this.el.textContent.length + 1)
617
627
  }
618
628
  }
619
629
 
630
+ setEl = ref => this.el = ref
631
+
620
632
  onPaste = (ev) => {
621
633
  ev.preventDefault()
622
634
  const text = ev.clipboardData.getData('text')
@@ -628,7 +640,8 @@ export class AutoCompleteTextbox extends React.Component {
628
640
  const valueOrBlank = /*android fix, not needed? value || */ _.isString(value) && value ? value : '\u200b'
629
641
 
630
642
  const props = {
631
- innerRef: ref => this.el = ref,
643
+ // stable ref: a new function each render makes ContentEditable re-render and jump the caret to the end
644
+ innerRef: this.setEl,
632
645
  onKeyDown: this.onKeyDown,
633
646
  onPaste: this.onPaste,
634
647
  value: value,
package/src/RichText.jsx CHANGED
@@ -1,6 +1,6 @@
1
1
  import React, {useContext, useEffect, useRef, useState} from 'react'
2
2
  import styled, {css} from 'styled-components'
3
- import ContentEditable from 'react-contenteditable'
3
+ import ContentEditableModule from 'react-contenteditable'
4
4
  import {darken} from 'polished'
5
5
  import _ from 'lodash/fp'
6
6
  import {getColor, linkRegex, stopPropagation} from '@startlibs/utils'
@@ -12,6 +12,9 @@ import {TextInput} from './TextInput'
12
12
  import {useInput} from './enhanceInput'
13
13
  import {Button, Icon} from '@startlibs/components'
14
14
 
15
+ // Node/esbuild interop em .mjs devolve module.exports; Rollup devolve o default. Cobre os dois.
16
+ const ContentEditable = ContentEditableModule.default || ContentEditableModule
17
+
15
18
  const InputIcon = styled(Icon)`
16
19
  position: absolute;
17
20
  outline: none;
@@ -42,7 +42,7 @@ export const useDraftConfirmation = (submit, discard, meta) => {
42
42
 
43
43
  const Dialog = startlibs.dialogs[dialog.isOpen]
44
44
  if (process.env.NODE_ENV !== 'production') {
45
- if (dialog.isOpen && !Dialog) { console.log(`Dialog [${dialogType}] not registered in startlibs.dialogs`) }
45
+ if (dialog.isOpen && !Dialog) { console.log(`Dialog [${dialog.isOpen}] not registered in startlibs.dialogs`) }
46
46
  }
47
47
 
48
48
  const feedback =