From 9d3dc72528e8994387c5d656450fc501de146041 Mon Sep 17 00:00:00 2001 From: Tanner Stirrat Date: Thu, 23 Jan 2025 10:41:24 -0700 Subject: [PATCH 1/3] Be explicit about available values --- src/playground-ui/ConfirmDialog.tsx | 26 +++++---- src/playground-ui/ConfirmDialogProvider.tsx | 59 ++++++++++++--------- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/playground-ui/ConfirmDialog.tsx b/src/playground-ui/ConfirmDialog.tsx index 5da9528..1057003 100644 --- a/src/playground-ui/ConfirmDialog.tsx +++ b/src/playground-ui/ConfirmDialog.tsx @@ -8,10 +8,12 @@ import DialogTitle from "@material-ui/core/DialogTitle"; import TextField from "@material-ui/core/TextField"; import React, { useState } from "react"; +export type ConfirmValue = "undefined" | "load" | "replace" + /** * ConfirmDialogButton is a button in the confirm dialog. */ -export interface ConfirmDialogButton { +export interface ConfirmDialogButton { /** * title is the title of the button. */ @@ -20,7 +22,7 @@ export interface ConfirmDialogButton { /** * value is the value to given to the callback if the button is clicked. */ - value: B + value?: ConfirmValue /** * color is the color of the button. Default is `default`. @@ -38,12 +40,12 @@ export interface ConfirmDialogButton { isEnabled?: (promptValue?: string) => boolean } -export type ConfirmCallback = (value: B, promptValue?: string) => void; +export type ConfirmCallback = (value: ConfirmValue, promptValue?: string) => void; /** * ConfirmDialogProps are the props for the confirm dialog. */ -export interface ConfirmDialogProps { +export interface ConfirmDialogProps { /** * isOpen indicates whether the ConfirmDialog is currently open. */ @@ -54,7 +56,7 @@ export interface ConfirmDialogProps { * returns the value clicked. If the [X] is clicked, the `value` will be * undefined. */ - handleComplete: ConfirmCallback + handleComplete: ConfirmCallback /** * title is the title of the confirm. @@ -69,7 +71,7 @@ export interface ConfirmDialogProps { /** * buttons are the buttons on the confirm dialog. */ - buttons: ConfirmDialogButton[] + buttons: ConfirmDialogButton[] /** @@ -97,13 +99,15 @@ const useStyles = makeStyles((theme: Theme) => * title="My confirm" content="Hi there!" * buttons={[{"title": "Close", "value": undefined}]}/> */ -export function ConfirmDialog(props: ConfirmDialogProps) { +export function ConfirmDialog(props: ConfirmDialogProps) { const [promptValue, setPromptValue] = useState(''); const classes = useStyles(); - const handleComplete = (value: any) => { - props.handleComplete(value as B, promptValue); - setPromptValue(''); + const handleComplete = (value: ConfirmValue | undefined) => { + if (value) { + props.handleComplete(value, promptValue); + setPromptValue(''); + } }; return (props: ConfirmDialogProps) { - {props.buttons.map((button: ConfirmDialogButton, index: number) => { + {props.buttons.map((button: ConfirmDialogButton, index: number) => { return