forked from ing-bank/lion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(form-core): only preserve caret if value changed
- Loading branch information
1 parent
5e7e43d
commit e87b629
Showing
8 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@lion/form-core': patch | ||
'@lion/input': patch | ||
'@lion/textarea': patch | ||
--- | ||
|
||
only preserve caret if value changed, which fixes a safari bug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/form-core/test-suites/NativeTextFieldMixin.suite.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { LitElement } from '@lion/core'; | ||
import { getFormControlMembers } from '@lion/form-core/test-helpers'; | ||
import { defineCE, expect, fixture, html, triggerFocusFor, unsafeStatic } from '@open-wc/testing'; | ||
import { sendKeys } from '@web/test-runner-commands'; | ||
import { spy } from 'sinon'; | ||
import { NativeTextFieldMixin } from '../src/NativeTextFieldMixin.js'; | ||
|
||
/** | ||
* @typedef {import('../types/FormControlMixinTypes').FormControlHost} FormControlHost | ||
* @typedef {ArrayConstructor | ObjectConstructor | NumberConstructor | BooleanConstructor | StringConstructor | DateConstructor | 'iban' | 'email'} modelValueType | ||
*/ | ||
|
||
/** | ||
* @param {{tagString?: string, modelValueType?: modelValueType}} [customConfig] | ||
*/ | ||
export function runNativeTextFieldMixinSuite(customConfig) { | ||
const cfg = { | ||
tagString: null, | ||
...customConfig, | ||
}; | ||
|
||
describe('NativeTextFieldMixin', () => { | ||
class NativeTextFieldClass extends NativeTextFieldMixin(LitElement) { | ||
get slots() { | ||
return { | ||
// NativeTextFieldClass needs to have an _inputNode defined in order to work... | ||
input: () => document.createElement('input'), | ||
}; | ||
} | ||
} | ||
|
||
cfg.tagString = cfg.tagString ? cfg.tagString : defineCE(NativeTextFieldClass); | ||
const tag = unsafeStatic(cfg.tagString); | ||
|
||
it('preserves the caret position on value change', async () => { | ||
const el = /** @type {NativeTextFieldClass} */ (await fixture(html`<${tag}></${tag}>`)); | ||
// @ts-ignore [allow-protected] in test | ||
const setValueAndPreserveCaretSpy = spy(el, '_setValueAndPreserveCaret'); | ||
const { _inputNode } = getFormControlMembers(el); | ||
await triggerFocusFor(el); | ||
await el.updateComplete; | ||
_inputNode.value = 'hello world'; | ||
_inputNode.selectionStart = 2; | ||
_inputNode.selectionEnd = 2; | ||
el.value = 'hey there universe'; | ||
expect(setValueAndPreserveCaretSpy.calledOnce).to.be.true; | ||
expect(_inputNode.selectionStart).to.equal(2); | ||
expect(_inputNode.selectionEnd).to.equal(2); | ||
}); | ||
|
||
it('move focus to a next focusable element after writing some text', async () => { | ||
const el = /** @type {NativeTextFieldClass} */ (await fixture(html`<${tag}></${tag}>`)); | ||
// @ts-ignore [allow-protected] in test | ||
const setValueAndPreserveCaretSpy = spy(el, '_setValueAndPreserveCaret'); | ||
const { _inputNode } = getFormControlMembers(el); | ||
await triggerFocusFor(el); | ||
await el.updateComplete; | ||
expect(document.activeElement).to.equal(_inputNode); | ||
await sendKeys({ | ||
press: 'h', | ||
}); | ||
await sendKeys({ | ||
press: 'Tab', | ||
}); | ||
expect(document.activeElement).to.not.equal(_inputNode); | ||
expect(setValueAndPreserveCaretSpy.calledOnce).to.be.false; | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { runNativeTextFieldMixinSuite } from '../test-suites/NativeTextFieldMixin.suite.js'; | ||
|
||
runNativeTextFieldMixinSuite(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
import { runFormatMixinSuite } from '@lion/form-core/test-suites'; | ||
import { | ||
runInteractionStateMixinSuite, | ||
runFormatMixinSuite, | ||
runNativeTextFieldMixinSuite, | ||
} from '@lion/form-core/test-suites'; | ||
|
||
import '@lion/textarea/define'; | ||
|
||
const tagString = 'lion-textarea'; | ||
|
||
describe('<lion-textarea> integrations', () => { | ||
runInteractionStateMixinSuite({ | ||
tagString, | ||
}); | ||
|
||
runFormatMixinSuite({ | ||
tagString, | ||
}); | ||
|
||
runNativeTextFieldMixinSuite({ | ||
tagString, | ||
}); | ||
}); |