Created
July 11, 2025 18:47
-
-
Save pkayokay/b4183a9aee67911067688a55705a416d to your computer and use it in GitHub Desktop.
Inertia / Phoenix lazy data and partial reload on select dropdown values
This file contains hidden or 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
import { useState } from "react"; | |
import { router } from "@inertiajs/react"; | |
interface selectOptionValuesType { | |
value: string; | |
label: string; | |
} | |
export default function Example({ selectOptionValues } : { selectOptionValues: selectOptionValuesType[] | null }) { | |
const [showDropdown, setShowDropdown] = useState(false); | |
const handleCheckboxChange = () => { | |
setShowDropdown(!showDropdown); | |
router.reload({ only: ['selectOptionValues'] }); | |
}; | |
console.log("selectOptionValues", selectOptionValues); | |
return ( | |
<> | |
<div> | |
<p>selectOptionValues prop value</p> | |
<code> | |
{JSON.stringify(selectOptionValues) || "No options available"} | |
</code> | |
</div> | |
<br /> | |
<div> | |
<input type="checkbox" onClick={handleCheckboxChange}/> | |
<label>Show select dropdown</label> | |
</div> | |
{showDropdown && ( | |
<div> | |
<select> | |
{(selectOptionValues || []).map((option: selectOptionValuesType, index: number) => ( | |
<option key={index} value={option.value}> | |
{option.label} | |
</option> | |
))} | |
</select> | |
</div> | |
)} | |
</> | |
); | |
} |
This file contains hidden or 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
defmodule ExampleWeb.PageController do | |
use ExampleWeb, :controller | |
def example(conn, _params) do | |
conn | |
|> assign(:page_title, "Example") | |
|> assign_prop( | |
:selectOptionValues, | |
inertia_optional(fn -> | |
[ | |
%{ | |
label: "Option 1", | |
value: "1" | |
}, | |
%{ | |
label: "Option 2", | |
value: "2" | |
}, | |
%{ | |
label: "Option 3", | |
value: "3" | |
} | |
] | |
end) | |
) | |
|> render_inertia("Example") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment