Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix number detection #8

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/model/form.event.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class FormEventListener extends EventListener {
data[key] = true
} else {
const value = parseFloat(data[key])
if(!isNaN(value)) {
if(!isNaN(value) && isFinite(data[key])) {
data[key] = value
}
}
Expand Down
39 changes: 34 additions & 5 deletions test/form.binding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ const RootModel = FormModel({
},
]
},
{
tagName: "div",
children: [
{
tagName: "input",
value: "1",
identifier: "number1"
},
{
tagName: "input",
value: "1.1",
identifier: "number2"
},
{
tagName: "input",
value: "1.1.1",
identifier: "number3"
},
]
},
{
tagName: "input",
type: "file",
Expand Down Expand Up @@ -102,12 +122,12 @@ describe("form.binding", () => {
const form = new Form()
const binding = new FormBinding({ form })
rootBinding.run(RootModel, { binding })
assert.strictEqual(Object.keys(binding.keys).length, 9)
assert.strictEqual(Object.keys(binding.keys).length, 12)
assert.strictEqual(binding.root.tagName, "FORM")
assert.strictEqual(binding.root.className, "form")
assert.deepEqual(Object.keys(binding.identifier), binding.keys)
assert.strictEqual(binding.identifier.name, binding.identifier[binding.keys[0]])
assert.strictEqual(binding.identifier.file, binding.identifier[binding.keys[8]])
assert.strictEqual(binding.identifier.file, binding.identifier[binding.keys[11]])
})

it("focus", () => {
Expand All @@ -131,7 +151,10 @@ describe("form.binding", () => {
date2: "1990-02-20",
gender: "female",
married: true,
kids: false
kids: false,
number1: 1,
number2: 1.1,
number3: "1.1.1",
})
assert.strictEqual(binding.identifier.name.value, "Yasuhito")
assert.strictEqual(binding.identifier.choices.value, "b")
Expand All @@ -140,7 +163,10 @@ describe("form.binding", () => {
assert.strictEqual(binding.identifier.date2.value, "1990-02-20")
assert.strictEqual(binding.root.querySelector("input[name=gender]:checked").value, "female")
assert.strictEqual(binding.identifier.married.checked, true)
assert.strictEqual(binding.identifier.kids.checked, false)
assert.strictEqual(binding.identifier.kids.checked, false)
assert.strictEqual(binding.identifier.number1.value, "1")
assert.strictEqual(binding.identifier.number2.value, "1.1")
assert.strictEqual(binding.identifier.number3.value, "1.1.1")
})

it("clear", () => {
Expand All @@ -158,7 +184,7 @@ describe("form.binding", () => {
const binding = new FormBinding({ form })
rootBinding.run(RootModel, { binding })
form.listen("submitted", data => {
assert.deepEqual(Object.keys(data), ["name", "body", "choices", "date", "date2", "gender", "married", "kids", "file"])
assert.deepEqual(Object.keys(data), ["name", "body", "choices", "date", "date2", "gender", "married", "kids", "number1", "number2", "number3", "file"])
assert.strictEqual(data.name, "")
assert.strictEqual(data.choices, "a")
assert.strictEqual(data.body, "")
Expand All @@ -167,6 +193,9 @@ describe("form.binding", () => {
assert.strictEqual(data.gender, null)
assert.strictEqual(data.married, false)
assert.strictEqual(data.kids, false)
assert.strictEqual(data.number1, 1)
assert.strictEqual(data.number2, 1.1)
assert.strictEqual(data.number3, "1.1.1")
assert.ok(data.file instanceof window.FileList)
assert.strictEqual(data.file.length, 0)
})
Expand Down
Loading