-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
3,779 additions
and
3,501 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,73 +1,76 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import { Link, Route, Routes, useLocation } from 'react-router-dom'; | ||
import { Main } from './Main'; | ||
import { FirstSample } from './samples/first/FirstSample'; | ||
import { ValidationSample } from './samples/validation-types/ValidationSample'; | ||
import { AsyncValidationSample } from './samples/async/AsyncValidationSample'; | ||
import { Arrow } from './icons/Arrow'; | ||
import { useChangeEffect } from './hooks/useChangeEffect'; | ||
import { PasswordRepeatForm } from './samples/password-repeat/PasswordRepeatForm'; | ||
import React, { useCallback, useState } from "react"; | ||
import { Link, Route, Routes, useLocation } from "react-router-dom"; | ||
import { useChangeEffect } from "./hooks/useChangeEffect"; | ||
import { Arrow } from "./icons/Arrow"; | ||
import { Main } from "./Main"; | ||
import { AsyncValidationSample } from "./samples/async/AsyncValidationSample"; | ||
import { FirstSample } from "./samples/first/FirstSample"; | ||
import { PasswordRepeatForm } from "./samples/password-repeat/PasswordRepeatForm"; | ||
import { ValidationSample } from "./samples/validation-types/ValidationSample"; | ||
|
||
type Sample = { pathname: string; label: string }; | ||
const Samples: Sample[] = [ | ||
{ pathname: '/', label: 'First sample' }, | ||
{ pathname: '/validation/types', label: 'Validation types' }, | ||
{ pathname: '/validation/async', label: 'Async validation' }, | ||
{ pathname: '/validation/passwordRepeat', label: 'Password repetition' }, | ||
{ pathname: "/", label: "First sample" }, | ||
{ pathname: "/validation/types", label: "Validation types" }, | ||
{ pathname: "/validation/async", label: "Async validation" }, | ||
{ pathname: "/validation/passwordRepeat", label: "Password repetition" }, | ||
]; | ||
|
||
export type SidebarProps = { open?: boolean; toggle: () => void }; | ||
|
||
export const Sidebar: React.FC<SidebarProps> = ({ open, toggle }) => { | ||
const location = useLocation(); | ||
const location = useLocation(); | ||
|
||
useChangeEffect(() => { | ||
toggle(); | ||
}, [location.pathname, toggle]); | ||
useChangeEffect(() => { | ||
toggle(); | ||
}, [location.pathname, toggle]); | ||
|
||
let sideBarFadeClass = undefined; | ||
if (open !== undefined) { | ||
sideBarFadeClass = open ? 'fade-in' : 'fade-out'; | ||
} | ||
return ( | ||
<nav className={sideBarFadeClass}> | ||
<header> | ||
<strong>Navigation</strong> | ||
<div onClick={toggle}> | ||
<Arrow direction="LEFT" /> | ||
</div> | ||
</header> | ||
<ul> | ||
{Samples.map(({ pathname, label }: Sample) => { | ||
const activeClass = location.pathname === pathname ? ' active' : ''; | ||
return ( | ||
<li key={pathname}> | ||
<Link className={'nav-link' + activeClass} to={pathname}> | ||
{label} | ||
</Link> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
</nav> | ||
); | ||
let sideBarFadeClass = undefined; | ||
if (open !== undefined) { | ||
sideBarFadeClass = open ? "fade-in" : "fade-out"; | ||
} | ||
return ( | ||
<nav className={sideBarFadeClass}> | ||
<header> | ||
<strong>Navigation</strong> | ||
<div onClick={toggle}> | ||
<Arrow direction="LEFT" /> | ||
</div> | ||
</header> | ||
<ul> | ||
{Samples.map(({ pathname, label }: Sample) => { | ||
const activeClass = location.pathname === pathname ? " active" : ""; | ||
return ( | ||
<li key={pathname}> | ||
<Link className={"nav-link" + activeClass} to={pathname}> | ||
{label} | ||
</Link> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
</nav> | ||
); | ||
}; | ||
|
||
export const App: React.FC = () => { | ||
const [sideBarOpen, setSidebarOpen] = useState<boolean | undefined>(); | ||
const toggleSideBar = useCallback(() => setSidebarOpen((s) => !s), []); | ||
const [sideBarOpen, setSidebarOpen] = useState<boolean | undefined>(); | ||
const toggleSideBar = useCallback(() => setSidebarOpen((s) => !s), []); | ||
|
||
return ( | ||
<div className="app"> | ||
<Sidebar open={sideBarOpen} toggle={toggleSideBar} /> | ||
<Main toggleSideBar={toggleSideBar}> | ||
<Routes> | ||
<Route path="/validation/types" element={<ValidationSample />} /> | ||
<Route path="/validation/async" element={<AsyncValidationSample />} /> | ||
<Route path="/validation/passwordRepeat" element={<PasswordRepeatForm />} /> | ||
<Route path="*" element={<FirstSample />} /> | ||
</Routes> | ||
</Main> | ||
</div> | ||
); | ||
return ( | ||
<div className="app"> | ||
<Sidebar open={sideBarOpen} toggle={toggleSideBar} /> | ||
<Main toggleSideBar={toggleSideBar}> | ||
<Routes> | ||
<Route path="/validation/types" element={<ValidationSample />} /> | ||
<Route path="/validation/async" element={<AsyncValidationSample />} /> | ||
<Route | ||
path="/validation/passwordRepeat" | ||
element={<PasswordRepeatForm />} | ||
/> | ||
<Route path="*" element={<FirstSample />} /> | ||
</Routes> | ||
</Main> | ||
</div> | ||
); | ||
}; |
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,19 +1,29 @@ | ||
import React from 'react'; | ||
import React from "react"; | ||
|
||
type FormContainerProps = { children: React.ReactNode; toggleSideBar: () => void }; | ||
type FormContainerProps = { | ||
children: React.ReactNode; | ||
toggleSideBar: () => void; | ||
}; | ||
|
||
export const Main: React.FC<FormContainerProps> = ({ children, toggleSideBar }) => ( | ||
<main> | ||
<header> | ||
<img | ||
src={`${import.meta.env.BASE_URL}images/menu.svg`} | ||
className="sidebar-toggle" | ||
alt="menu" | ||
onClick={toggleSideBar} | ||
/> | ||
<img src={`${import.meta.env.BASE_URL}images/form-logo.svg`} className="logo" alt="logo" /> | ||
<h1>Welcome to morfi</h1> | ||
</header> | ||
<div className={'content'}>{children}</div> | ||
</main> | ||
export const Main: React.FC<FormContainerProps> = ({ | ||
children, | ||
toggleSideBar, | ||
}) => ( | ||
<main> | ||
<header> | ||
<img | ||
src={`${import.meta.env.BASE_URL}images/menu.svg`} | ||
className="sidebar-toggle" | ||
alt="menu" | ||
onClick={toggleSideBar} | ||
/> | ||
<img | ||
src={`${import.meta.env.BASE_URL}images/form-logo.svg`} | ||
className="logo" | ||
alt="logo" | ||
/> | ||
<h1>Welcome to morfi</h1> | ||
</header> | ||
<div className={"content"}>{children}</div> | ||
</main> | ||
); |
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,51 +1,60 @@ | ||
import React from 'react'; | ||
import type { ErrorMessage } from 'morfi'; | ||
import messages from '../../messages.json'; | ||
import { Spinner } from '../icons/Spinner'; | ||
import React from "react"; | ||
import type { ErrorMessage } from "morfi"; | ||
import messages from "../../messages.json"; | ||
import { Spinner } from "../icons/Spinner"; | ||
|
||
export const __ = (msgOrID: ErrorMessage): string => { | ||
const id = typeof msgOrID === 'string' ? msgOrID : msgOrID.id; | ||
const values = typeof msgOrID === 'string' ? undefined : msgOrID.values; | ||
let result = (messages as Record<string, undefined | string>)[id]; | ||
if (result && values) { | ||
result = Object.keys(values).reduce( | ||
(red: string, key: string) => red.replace(`{${key}}`, String(values[key])), | ||
result | ||
); | ||
} | ||
return result!; | ||
const id = typeof msgOrID === "string" ? msgOrID : msgOrID.id; | ||
const values = typeof msgOrID === "string" ? undefined : msgOrID.values; | ||
let result = (messages as Record<string, undefined | string>)[id]; | ||
if (result && values) { | ||
result = Object.keys(values).reduce( | ||
(red: string, key: string) => | ||
red.replace(`{${key}}`, String(values[key])), | ||
result, | ||
); | ||
} | ||
return result!; | ||
}; | ||
|
||
export const Label = ({ label, required = false }: { label: string; required?: boolean }) => ( | ||
<label className="control-label">{label + (required ? ' *' : '')}</label> | ||
); | ||
export const Label = ({ | ||
label, | ||
required = false, | ||
}: { | ||
label: string; | ||
required?: boolean; | ||
}) => <label className="control-label">{label + (required ? " *" : "")}</label>; | ||
|
||
export const DisplayError = ({ error }: { error: ErrorMessage }) => ( | ||
<span className="invalid-feedback">{__(error)}</span> | ||
<span className="invalid-feedback">{__(error)}</span> | ||
); | ||
|
||
export const onActionWrap = | ||
(cb?: (arg: any) => void, preventDefault = true): React.EventHandler<any> => | ||
(event) => { | ||
if (preventDefault) { | ||
event.preventDefault(); | ||
} | ||
if (cb) { | ||
cb(event.target.value); | ||
} | ||
}; | ||
(cb?: (arg: any) => void, preventDefault = true): React.EventHandler<any> => | ||
(event) => { | ||
if (preventDefault) { | ||
event.preventDefault(); | ||
} | ||
if (cb) { | ||
cb(event.target.value); | ||
} | ||
}; | ||
|
||
type ButtonProps = { | ||
children: React.ReactNode; | ||
loading?: boolean; | ||
disabled?: boolean; | ||
type?: 'submit' | 'button'; | ||
onClick?: () => void; | ||
children: React.ReactNode; | ||
loading?: boolean; | ||
disabled?: boolean; | ||
type?: "submit" | "button"; | ||
onClick?: () => void; | ||
}; | ||
|
||
export const Button: React.FC<ButtonProps> = ({ children, loading, ...rest }) => ( | ||
<button {...rest}> | ||
{loading && <Spinner />} | ||
{children} | ||
</button> | ||
export const Button: React.FC<ButtonProps> = ({ | ||
children, | ||
loading, | ||
...rest | ||
}) => ( | ||
<button {...rest}> | ||
{loading && <Spinner />} | ||
{children} | ||
</button> | ||
); |
Oops, something went wrong.