Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@cicara/better-modal
Advanced tools
A tool to help simplify antd modal state management
pnpm add @cicara/better-modal
Create an Antd Modal in the usual way, with the only difference that the state of the Modal is managed automatically using useModalContext
.
import { Input, Modal } from "antd";
import { useModalContext } from "@cicara/better-modal";
import { Form } from "antd";
import { useCallback, useEffect } from "react";
export type MyModalProps = {
value?: string;
};
export function MyModal(props: MyModalProps) {
const modal = useModalContext<string>(); // <- First use modal context
const [form] = Form.useForm();
const handleSubmit = useCallback(
(values: { name: string }) => {
modal.resolve(values.name); // <- Close the modal and return the data or message
},
[modal.resolve]
);
useEffect(() => {
if (!modal.visible) {
return;
}
form.setFieldsValue({ name: props.value });
}, [props.value]);
return (
<Modal
open={modal.visible}
title="What's your name?"
onOk={() => form.submit()}
onCancel={() => modal.reject(new Error("user cancel"))} // <- Close modal with reject and reason
afterClose={modal.destroy} // Optional, destroy the modal
>
<Form form={form} onFinish={handleSubmit} autoComplete="off">
<Form.Item name="name" rules={[{ required: true }]}>
<Input placeholder="your name..." />
</Form.Item>
</Form>
</Modal>
);
}
Use the useModal<string>(MyModal)
react hook to create the modal and insert {myModal.placeholder}
in the right place so that the modal can get the Context correctly.
import { useModal } from "@cicara/better-modal";
import { MyModal } from "./my-modal";
export function App() {
const myModal = useModal(MyModal);
return <div>{myModal.placeholder}</div>;
}
Use the myModal.show
function to display the modal and passing props, the show
function returns a Promise, which corresponds to the resolve
and reject
function in the useModalContext
.
import { Button } from "antd";
import { useModal } from "@cicara/better-modal";
import { MyModal } from "./my-modal";
import { useCallback } from "react";
export function App() {
const myModal = useModal(MyModal);
const handleOpenModal = useCallback(async () => {
try {
const result = await myModal.show<string>({ value: "hungtcs" });
alert(result);
} catch (err) {
alert(err);
}
}, []);
return (
<div>
<Button onClick={handleOpenModal}>Open Modal</Button>
{myModal.placeholder}
</div>
);
}
FAQs
a tool to help simplify antd modal state management
The npm package @cicara/better-modal receives a total of 0 weekly downloads. As such, @cicara/better-modal popularity was classified as not popular.
We found that @cicara/better-modal demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.