@startlibs/form 1.1.2 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startlibs/form",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Startlibs Form",
5
5
  "main": "lib/index.cjs",
6
6
  "exports": {
@@ -611,15 +611,24 @@ export class AutoCompleteTextbox extends React.Component {
611
611
  return nextProps.value !== cleanText(this.el.innerText) || !!Object.keys(nextProps).find((k) => nextProps[k] !== this.props[k])
612
612
  }
613
613
 
614
- 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
615
621
  if (this.props.value.slice(-1) === ' ' && /\S/.test(this.el.textContent.slice(-1))) { // does not have blank space as last
616
622
  this.el.innerHTML = this.props.value.replace(/\s/, ' ')
623
+ rewritten = true
617
624
  }
618
- if (this.props.value !== oldProps.value && document.activeElement === this.el) {
625
+ if (this.props.value !== oldProps.value && (changedOutside || rewritten) && document.activeElement === this.el) {
619
626
  setSelectionRange(this.el.firstChild || this.el, this.el.textContent.length + 1)
620
627
  }
621
628
  }
622
629
 
630
+ setEl = ref => this.el = ref
631
+
623
632
  onPaste = (ev) => {
624
633
  ev.preventDefault()
625
634
  const text = ev.clipboardData.getData('text')
@@ -631,7 +640,8 @@ export class AutoCompleteTextbox extends React.Component {
631
640
  const valueOrBlank = /*android fix, not needed? value || */ _.isString(value) && value ? value : '\u200b'
632
641
 
633
642
  const props = {
634
- 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,
635
645
  onKeyDown: this.onKeyDown,
636
646
  onPaste: this.onPaste,
637
647
  value: value,