-
Notifications
You must be signed in to change notification settings - Fork 646
SelectPanel2: Add story for "create new item" #4377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import React from 'react' | ||
| import {SelectPanel} from './SelectPanel' | ||
| import {ActionList, ActionMenu, Avatar, Box, Button, Text, Octicon, Flash} from '../../index' | ||
| import {ActionList, ActionMenu, Avatar, Box, Button, Text, Octicon, Flash, FormControl, TextInput} from '../../index' | ||
| import {Dialog} from '../../drafts' | ||
| import { | ||
| ArrowRightIcon, | ||
|
|
@@ -12,6 +12,7 @@ import { | |
| GitPullRequestIcon, | ||
| GitMergeIcon, | ||
| GitPullRequestDraftIcon, | ||
| PlusCircleIcon, | ||
| } from '@primer/octicons-react' | ||
| import data from './mock-story-data' | ||
|
|
||
|
|
@@ -846,6 +847,219 @@ export const NestedSelection = () => { | |
| ) | ||
| } | ||
|
|
||
| export const CreateNewRow = () => { | ||
| const initialSelectedLabels = data.issue.labelIds // mock initial state: has selected labels | ||
| const [selectedLabelIds, setSelectedLabelIds] = React.useState<string[]>(initialSelectedLabels) | ||
|
|
||
| /* Selection */ | ||
| const onLabelSelect = (labelId: string) => { | ||
| if (!selectedLabelIds.includes(labelId)) setSelectedLabelIds([...selectedLabelIds, labelId]) | ||
| else setSelectedLabelIds(selectedLabelIds.filter(id => id !== labelId)) | ||
| } | ||
| const onClearSelection = () => { | ||
| setSelectedLabelIds([]) | ||
| } | ||
|
|
||
| const onSubmit = () => { | ||
| data.issue.labelIds = selectedLabelIds // pretending to persist changes | ||
| } | ||
|
|
||
| /* Filtering */ | ||
| const [filteredLabels, setFilteredLabels] = React.useState(data.labels) | ||
| const [query, setQuery] = React.useState('') | ||
|
|
||
| const onSearchInputChange: React.ChangeEventHandler<HTMLInputElement> = event => { | ||
| const query = event.currentTarget.value | ||
| setQuery(query) | ||
|
|
||
| if (query === '') setFilteredLabels(data.labels) | ||
| else { | ||
| setFilteredLabels( | ||
| data.labels | ||
| .map(label => { | ||
| if (label.name.toLowerCase().startsWith(query)) return {priority: 1, label} | ||
| else if (label.name.toLowerCase().includes(query)) return {priority: 2, label} | ||
| else if (label.description?.toLowerCase().includes(query)) return {priority: 3, label} | ||
| else return {priority: -1, label} | ||
| }) | ||
| .filter(result => result.priority > 0) | ||
| .map(result => result.label), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| const sortingFn = (itemA: {id: string}, itemB: {id: string}) => { | ||
| const initialSelectedIds = data.issue.labelIds | ||
| if (initialSelectedIds.includes(itemA.id) && initialSelectedIds.includes(itemB.id)) return 1 | ||
| else if (initialSelectedIds.includes(itemA.id)) return -1 | ||
| else if (initialSelectedIds.includes(itemB.id)) return 1 | ||
| else return 1 | ||
| } | ||
|
|
||
| const itemsToShow = query ? filteredLabels : data.labels.sort(sortingFn) | ||
|
|
||
| /* | ||
| Controlled state + Create new label Dialog | ||
| We only have to do this until https://github.com/primer/react/pull/3840 is merged | ||
| */ | ||
| const [panelOpen, setPanelOpen] = React.useState(false) | ||
| const [newLabelDialogOpen, setNewLabelDialogOpen] = React.useState(false) | ||
|
|
||
| const openCreateLabelDialog = () => { | ||
| setPanelOpen(false) | ||
| setNewLabelDialogOpen(true) | ||
| } | ||
|
|
||
| const onNewLabelDialogSave = (id: string) => { | ||
| setNewLabelDialogOpen(false) | ||
|
|
||
| setQuery('') // clear search input | ||
| onLabelSelect(id) // select newly created label | ||
|
|
||
| setPanelOpen(true) | ||
|
|
||
| // focus newly created label once it renders | ||
| window.requestAnimationFrame(() => { | ||
| const newLabelElement = document.querySelector(`[data-id=${id}]`) as HTMLLIElement | ||
| newLabelElement.focus() | ||
| }) | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <h1>Create new item from panel</h1> | ||
|
|
||
| <SelectPanel | ||
| title="Select labels" | ||
| open={panelOpen} | ||
| onSubmit={onSubmit} | ||
| onCancel={() => setPanelOpen(false)} | ||
| onClearSelection={onClearSelection} | ||
| > | ||
| <SelectPanel.Button onClick={() => setPanelOpen(true)}>Assign label</SelectPanel.Button> | ||
|
|
||
| <SelectPanel.Header> | ||
| <SelectPanel.SearchInput value={query} onChange={onSearchInputChange} /> | ||
| </SelectPanel.Header> | ||
|
|
||
| {itemsToShow.length === 0 ? ( | ||
| <SelectPanel.Message variant="empty" title={`No labels found for "${query}"`}> | ||
| <Text>Select the button below to create this label</Text> | ||
| <Button onClick={openCreateLabelDialog}>Create "{query}"</Button> | ||
| </SelectPanel.Message> | ||
| ) : ( | ||
| <> | ||
| <ActionList> | ||
| {itemsToShow.map(label => ( | ||
| <ActionList.Item | ||
| key={label.id} | ||
| onSelect={() => onLabelSelect(label.id)} | ||
| selected={selectedLabelIds.includes(label.id)} | ||
| data-id={label.id} | ||
| > | ||
| <ActionList.LeadingVisual> | ||
| <Box | ||
| sx={{width: 14, height: 14, borderRadius: '100%'}} | ||
| style={{backgroundColor: `#${label.color}`}} | ||
| /> | ||
| </ActionList.LeadingVisual> | ||
| {label.name} | ||
| <ActionList.Description variant="block">{label.description}</ActionList.Description> | ||
| </ActionList.Item> | ||
| ))} | ||
| </ActionList> | ||
| {query && ( | ||
| <Box sx={{padding: 2, borderTop: '1px solid', borderColor: 'border.default', flexShrink: 0}}> | ||
| <Button | ||
| variant="invisible" | ||
| leadingVisual={PlusCircleIcon} | ||
| block | ||
| alignContent="start" | ||
| sx={{'[data-component=text]': {fontWeight: 'normal'}}} | ||
| onClick={openCreateLabelDialog} | ||
| > | ||
| Create new label "{query}"... | ||
| </Button> | ||
| </Box> | ||
| )} | ||
| </> | ||
| )} | ||
|
|
||
| <SelectPanel.Footer> | ||
| <SelectPanel.SecondaryAction variant="button">Edit labels</SelectPanel.SecondaryAction> | ||
| </SelectPanel.Footer> | ||
| </SelectPanel> | ||
|
|
||
| {newLabelDialogOpen && ( | ||
| <CreateNewLabelDialog | ||
| initialValue={query} | ||
| onSave={onNewLabelDialogSave} | ||
| onCancel={() => { | ||
| setNewLabelDialogOpen(false) | ||
| setPanelOpen(true) | ||
| }} | ||
| /> | ||
| )} | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| const CreateNewLabelDialog = ({ | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for reviewer: The code here isn't ideal, I tried to make it a little more accessible in the story but the underlying component needs work 😢 This isn't the focus of the PR/epic though, so I just added a |
||
| initialValue, | ||
| onSave, | ||
| onCancel, | ||
| }: { | ||
| initialValue: string | ||
| onSave: (id: string) => void | ||
| onCancel: () => void | ||
| }) => { | ||
| const formSubmitRef = React.useRef<HTMLButtonElement>(null) | ||
|
|
||
| const onSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
| event.preventDefault() | ||
|
|
||
| const formData = new FormData(event.target as HTMLFormElement) | ||
| const {name, color, description} = Object.fromEntries(formData) as Record<string, string> | ||
|
|
||
| // pretending to persist changes | ||
| const id = Math.random().toString(26).slice(6) | ||
| const createdAt = new Date().toISOString() | ||
| data.labels.unshift({id, name, color, description, createdAt}) | ||
| onSave(id) | ||
| } | ||
|
|
||
| return ( | ||
| <Dialog | ||
| title="Create new Label" | ||
| onClose={onCancel} | ||
| width="medium" | ||
| footerButtons={[ | ||
| {buttonType: 'default', content: 'Cancel', onClick: onCancel}, | ||
| {type: 'submit', buttonType: 'primary', content: 'Save', onClick: () => formSubmitRef.current?.click()}, | ||
| ]} | ||
| > | ||
| <Flash sx={{marginBottom: 2}} variant="warning"> | ||
| Note this Dialog is not accessible. Do not copy this. | ||
| </Flash> | ||
| <form onSubmit={onSubmit}> | ||
| <FormControl sx={{marginBottom: 2}}> | ||
| <FormControl.Label>Name</FormControl.Label> | ||
| <TextInput name="name" block defaultValue={initialValue} autoFocus /> | ||
| </FormControl> | ||
| <FormControl sx={{marginBottom: 2}}> | ||
| <FormControl.Label>Color</FormControl.Label> | ||
| <TextInput name="color" block defaultValue="fae17d" leadingVisual="#" /> | ||
| </FormControl> | ||
| <FormControl> | ||
| <FormControl.Label>Description</FormControl.Label> | ||
| <TextInput name="description" block placeholder="Good first issues" /> | ||
| </FormControl> | ||
| <button type="submit" hidden ref={formSubmitRef}></button> | ||
| </form> | ||
| </Dialog> | ||
| ) | ||
| } | ||
|
|
||
| // ----- Suspense implementation details ---- | ||
|
|
||
| const cache = new Map() | ||
|
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewer:
This is a bit unfortunate, you have to control the open state of SelectPanel because
SelectPaneluses<dialog>(top layer) butDialoguses portal not<dialog>so Dialog would render under the panel 🤦This would change once we pick up #3840 again