Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@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
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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.