forked from altalyst-solutions/hookify
-
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.
Merge pull request altalyst-solutions#13 from sleepinzombie/feat/add-…
…use-persisted-state Add usePersistedState hook
- Loading branch information
Showing
21 changed files
with
450 additions
and
79 deletions.
There are no files selected for viewing
File renamed without changes.
17 changes: 17 additions & 0 deletions
17
examples/basic/src/components/use-persisted-state/components/counter.tsx
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,17 @@ | ||
import { usePersistedState } from "@altalyst/hookify"; | ||
|
||
export const Counter = () => { | ||
const [count, setCount] = usePersistedState<number>("counter", 0); | ||
|
||
const increment = () => setCount((prev) => prev + 1); | ||
const decrement = () => setCount((prev) => prev - 1); | ||
|
||
return ( | ||
<div> | ||
<h2>Counter</h2> | ||
<p>Value: {count}</p> | ||
<button onClick={increment}>Increment</button> | ||
<button onClick={decrement}>Decrement</button> | ||
</div> | ||
); | ||
}; |
26 changes: 26 additions & 0 deletions
26
examples/basic/src/components/use-persisted-state/components/theme-switcher.tsx
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,26 @@ | ||
import { usePersistedState } from "@altalyst/hookify"; | ||
|
||
export const ThemeSwitcher = () => { | ||
const [theme, setTheme] = usePersistedState<"light" | "dark">( | ||
"theme", | ||
"light" | ||
); | ||
|
||
const toggleTheme = () => { | ||
setTheme((prev) => (prev === "light" ? "dark" : "light")); | ||
}; | ||
|
||
return ( | ||
<div | ||
style={{ | ||
background: theme === "dark" ? "#333" : "#fff", | ||
color: theme === "dark" ? "#fff" : "#000", | ||
padding: "1rem", | ||
}} | ||
> | ||
<h2>Theme Switcher</h2> | ||
<p>Current Theme: {theme}</p> | ||
<button onClick={toggleTheme}>Toggle Theme</button> | ||
</div> | ||
); | ||
}; |
33 changes: 33 additions & 0 deletions
33
examples/basic/src/components/use-persisted-state/components/todo-list.tsx
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,33 @@ | ||
import { usePersistedState } from "@altalyst/hookify"; | ||
import { useState } from "react"; | ||
|
||
export const TodoList = () => { | ||
const [todos, setTodos] = usePersistedState<string[]>("todos", []); | ||
const [input, setInput] = useState(""); | ||
|
||
const addTodo = () => { | ||
if (input.trim()) { | ||
setTodos((prev) => [...prev, input]); | ||
setInput(""); | ||
} | ||
}; | ||
|
||
const removeTodo = (index: number) => { | ||
setTodos((prev) => prev.filter((_, i) => i !== index)); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h2>To-Do List</h2> | ||
<input value={input} onChange={(e) => setInput(e.target.value)} /> | ||
<button onClick={addTodo}>Add</button> | ||
<ul> | ||
{todos.map((todo, index) => ( | ||
<li key={index}> | ||
{todo} <button onClick={() => removeTodo(index)}>Remove</button> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; |
33 changes: 33 additions & 0 deletions
33
examples/basic/src/components/use-persisted-state/components/user-form.tsx
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,33 @@ | ||
import { usePersistedState } from "@altalyst/hookify"; | ||
|
||
export const UserForm = () => { | ||
const [formData, setFormData] = usePersistedState<{ | ||
name: string; | ||
email: string; | ||
}>("userForm", { | ||
name: "", | ||
email: "", | ||
}); | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const { name, value } = e.target; | ||
setFormData((prev) => ({ ...prev, [name]: value })); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h2>User Form</h2> | ||
<form> | ||
<label> | ||
Name: | ||
<input name="name" value={formData.name} onChange={handleChange} /> | ||
</label> | ||
<br /> | ||
<label> | ||
Email: | ||
<input name="email" value={formData.email} onChange={handleChange} /> | ||
</label> | ||
</form> | ||
</div> | ||
); | ||
}; |
15 changes: 15 additions & 0 deletions
15
examples/basic/src/components/use-persisted-state/index.tsx
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,15 @@ | ||
import { Counter } from "./components/counter"; | ||
import { ThemeSwitcher } from "./components/theme-switcher"; | ||
import { TodoList } from "./components/todo-list"; | ||
import { UserForm } from "./components/user-form"; | ||
|
||
export const UsePersistedState = () => { | ||
return ( | ||
<> | ||
<Counter /> | ||
<ThemeSwitcher /> | ||
<TodoList /> | ||
<UserForm /> | ||
</> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
examples/basic/src/components/use-toggle-state/components/multi-toggle.tsx
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,18 @@ | ||
import { useToggleState } from "@altalyst/hookify"; | ||
|
||
export const MultiToggle = () => { | ||
const [isLightOn, toggleLight] = useToggleState(false); | ||
const [isFanOn, toggleFan] = useToggleState(false); | ||
|
||
return ( | ||
<div> | ||
<h2>Multi Toggle</h2> | ||
<button onClick={toggleLight}> | ||
{isLightOn ? "Turn Off Light" : "Turn On Light"} | ||
</button> | ||
<button onClick={toggleFan}> | ||
{isFanOn ? "Turn Off Fan" : "Turn On Fan"} | ||
</button> | ||
</div> | ||
); | ||
}; |
18 changes: 18 additions & 0 deletions
18
examples/basic/src/components/use-toggle-state/components/password-field.tsx
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,18 @@ | ||
import { useToggleState } from "@altalyst/hookify"; | ||
|
||
export const PasswordField = () => { | ||
const [isPasswordVisible, togglePasswordVisibility] = useToggleState(false); | ||
|
||
return ( | ||
<div> | ||
<h2>Password Field</h2> | ||
<input | ||
type={isPasswordVisible ? "text" : "password"} | ||
placeholder="Enter your password" | ||
/> | ||
<button onClick={togglePasswordVisibility}> | ||
{isPasswordVisible ? "Hide Password" : "Show Password"} | ||
</button> | ||
</div> | ||
); | ||
}; |
17 changes: 17 additions & 0 deletions
17
examples/basic/src/components/use-toggle-state/components/toggle-button.tsx
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,17 @@ | ||
import { useToggleState } from "@altalyst/hookify"; | ||
|
||
export const ToggleButton = () => { | ||
const [isActive, toggleActive] = useToggleState(false); | ||
|
||
return ( | ||
<div> | ||
<h2>Controlled Component: Toggle Button</h2> | ||
<button | ||
onClick={toggleActive} | ||
style={{ background: isActive ? "green" : "red" }} | ||
> | ||
{isActive ? "Active" : "Inactive"} | ||
</button> | ||
</div> | ||
); | ||
}; |
15 changes: 15 additions & 0 deletions
15
examples/basic/src/components/use-toggle-state/components/toggle-visibility.tsx
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,15 @@ | ||
import { useToggleState } from "@altalyst/hookify"; | ||
|
||
export const ToggleVisibility = () => { | ||
const [isVisible, toggleVisibility] = useToggleState(false); | ||
|
||
return ( | ||
<div> | ||
<h2>Toggle Visibility</h2> | ||
<button onClick={toggleVisibility}> | ||
{isVisible ? "Hide" : "Show"} Content | ||
</button> | ||
{isVisible && <p>This is some toggleable content!</p>} | ||
</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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { MultiToggle } from "./components/multi-toggle"; | ||
import { PasswordField } from "./components/password-field"; | ||
import { ToggleButton } from "./components/toggle-button"; | ||
import { ToggleVisibility } from "./components/toggle-visibility"; | ||
|
||
export const UseToggleState = () => { | ||
return ( | ||
<> | ||
<MultiToggle /> | ||
<PasswordField /> | ||
<ToggleButton /> | ||
<ToggleVisibility /> | ||
</> | ||
); | ||
}; |
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
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,10 @@ | ||
import { UsePersistedState } from "../../components/use-persisted-state"; | ||
|
||
export const UsePersistedStatePage = () => { | ||
return ( | ||
<> | ||
<h1>Demo for: usePersistedState hook</h1> | ||
<UsePersistedState /> | ||
</> | ||
); | ||
}; |
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,4 +1,5 @@ | ||
export * from "./use-api"; | ||
export * from "./use-effect-after-mount"; | ||
export * from "./use-outside-click"; | ||
export * from "./use-persisted-state"; | ||
export * from "./use-toggle-state"; |
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
Oops, something went wrong.