Skip to content

Commit be7bd6f

Browse files
committed
move new label dialog into its own component
1 parent acf9957 commit be7bd6f

File tree

1 file changed

+67
-45
lines changed

1 file changed

+67
-45
lines changed

src/drafts/SelectPanel2/stories/SelectPanel.examples.stories.tsx

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -704,31 +704,21 @@ export const CreateNewRow = () => {
704704
We only have to do this until https://github.com/primer/react/pull/3840 is merged
705705
*/
706706
const [panelOpen, setPanelOpen] = React.useState(false)
707-
const [dialogOpen, setDialogOpen] = React.useState(false)
707+
const [newLabelDialogOpen, setNewLabelDialogOpen] = React.useState(false)
708708

709709
const openCreateLabelDialog = () => {
710710
setPanelOpen(false)
711-
setDialogOpen(true)
711+
setNewLabelDialogOpen(true)
712712
}
713713

714-
const onDialogSubmit = (event: FormEvent<HTMLFormElement>) => {
715-
event.preventDefault()
716-
717-
const formData = new FormData(event.target as HTMLFormElement)
718-
const {name, color, description} = Object.fromEntries(formData) as Record<string, string>
719-
720-
// pretending to persist changes
721-
const id = `new-${name}`
722-
data.labels.unshift({id, name, color, description})
723-
724-
setDialogOpen(false)
725-
setPanelOpen(true)
714+
const onNewLabelDialogSave = (id: string) => {
715+
setNewLabelDialogOpen(false)
726716

727717
setQuery('') // clear search input
728718
onLabelSelect(id) // select newly created label
729-
}
730719

731-
const formSubmitRef = React.useRef<HTMLButtonElement>(null)
720+
setPanelOpen(true)
721+
}
732722

733723
return (
734724
<>
@@ -798,40 +788,72 @@ export const CreateNewRow = () => {
798788
</SelectPanel.Footer>
799789
</SelectPanel>
800790

801-
{dialogOpen && (
802-
<Dialog
803-
title="Create new Label"
804-
onClose={() => setDialogOpen(false)}
805-
width="medium"
806-
footerButtons={[
807-
{buttonType: 'default', content: 'Cancel', onClick: () => setDialogOpen(false)},
808-
{type: 'submit', buttonType: 'primary', content: 'Save', onClick: () => formSubmitRef.current?.click()},
809-
]}
810-
>
811-
<Flash sx={{marginBottom: 2}} variant="warning">
812-
Note this Dialog is not accessible. Do not copy this.
813-
</Flash>
814-
<form onSubmit={onDialogSubmit}>
815-
<FormControl sx={{marginBottom: 2}}>
816-
<FormControl.Label>Name</FormControl.Label>
817-
<TextInput name="name" block defaultValue={query} autoFocus />
818-
</FormControl>
819-
<FormControl sx={{marginBottom: 2}}>
820-
<FormControl.Label>Color</FormControl.Label>
821-
<TextInput name="color" block defaultValue="fae17d" leadingVisual="#" />
822-
</FormControl>
823-
<FormControl>
824-
<FormControl.Label>Description</FormControl.Label>
825-
<TextInput name="description" block placeholder="Good first issues" />
826-
</FormControl>
827-
<button type="submit" hidden ref={formSubmitRef}></button>
828-
</form>
829-
</Dialog>
791+
{newLabelDialogOpen && (
792+
<CreateNewLabelDialog
793+
initialValue={query}
794+
onSave={onNewLabelDialogSave}
795+
onCancel={() => setNewLabelDialogOpen(false)}
796+
/>
830797
)}
831798
</>
832799
)
833800
}
834801

802+
const CreateNewLabelDialog = ({
803+
initialValue,
804+
onSave,
805+
onCancel,
806+
}: {
807+
initialValue: string
808+
onSave: (id: string) => void
809+
onCancel: () => void
810+
}) => {
811+
const formSubmitRef = React.useRef<HTMLButtonElement>(null)
812+
813+
const onSubmit = (event: FormEvent<HTMLFormElement>) => {
814+
event.preventDefault()
815+
816+
const formData = new FormData(event.target as HTMLFormElement)
817+
const {name, color, description} = Object.fromEntries(formData) as Record<string, string>
818+
819+
// pretending to persist changes
820+
const id = `new-${name}`
821+
data.labels.unshift({id, name, color, description})
822+
onSave(id)
823+
}
824+
825+
return (
826+
<Dialog
827+
title="Create new Label"
828+
onClose={onCancel}
829+
width="medium"
830+
footerButtons={[
831+
{buttonType: 'default', content: 'Cancel', onClick: () => setDialogOpen(false)},
832+
{type: 'submit', buttonType: 'primary', content: 'Save', onClick: () => formSubmitRef.current?.click()},
833+
]}
834+
>
835+
<Flash sx={{marginBottom: 2}} variant="warning">
836+
Note this Dialog is not accessible. Do not copy this.
837+
</Flash>
838+
<form onSubmit={onSubmit}>
839+
<FormControl sx={{marginBottom: 2}}>
840+
<FormControl.Label>Name</FormControl.Label>
841+
<TextInput name="name" block defaultValue={initialValue} autoFocus />
842+
</FormControl>
843+
<FormControl sx={{marginBottom: 2}}>
844+
<FormControl.Label>Color</FormControl.Label>
845+
<TextInput name="color" block defaultValue="fae17d" leadingVisual="#" />
846+
</FormControl>
847+
<FormControl>
848+
<FormControl.Label>Description</FormControl.Label>
849+
<TextInput name="description" block placeholder="Good first issues" />
850+
</FormControl>
851+
<button type="submit" hidden ref={formSubmitRef}></button>
852+
</form>
853+
</Dialog>
854+
)
855+
}
856+
835857
// ----- Suspense implementation details ----
836858

837859
const cache = new Map()

0 commit comments

Comments
 (0)