Skip to content

Commit

Permalink
Email validator uses component function
Browse files Browse the repository at this point in the history
  • Loading branch information
paldepind committed Aug 21, 2019
1 parent db67f28 commit 588a098
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions examples/simple/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { map, Now, Behavior } from "@funkia/hareactive";
import { elements, modelView, runComponent } from "../../src";
import { elements, runComponent, component } from "../../src";
import { Behavior } from "@funkia/hareactive";
const { span, input, div } = elements;

const isValidEmail = (s: string) => /.+@.+\..+/i.test(s);

const model = ({ email }: { email: Behavior<string> }) => {
const isValid = email.map(isValidEmail);
return Now.of({ isValid });
};
type On = { email: Behavior<string> };

const view = ({ isValid }: { isValid: Behavior<boolean> }) => [
span("Please enter an email address: "),
input().output({ email: "value" }),
div(["The address is ", map((b) => (b ? "valid" : "invalid"), isValid)])
];
const app = component<On>((on) => {
const isValid = on.email.map(isValidEmail);
return [
span("Please enter an email address: "),
input().output({ email: "value" }),
div(["The address is ", isValid.map((b) => (b ? "valid" : "invalid"))])
];
});

const app = modelView(model, view);

runComponent("#mount", app());
runComponent("#mount", app);

0 comments on commit 588a098

Please sign in to comment.