-
Notifications
You must be signed in to change notification settings - Fork 0
Extract project components; add ConfirmDialog #149
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
Draft
microbit-matt-hillsdon
wants to merge
3
commits into
main
Choose a base branch
from
project-components
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 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
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
145 changes: 145 additions & 0 deletions
145
packages/ui-patterns/src/projects/NameProjectDialog.tsx
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 |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /** | ||
| * (c) 2026, Micro:bit Educational Foundation and contributors | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| import { | ||
| Button, | ||
| Modal, | ||
| ModalBody, | ||
| ModalCloseButton, | ||
| ModalFooter, | ||
| ModalHeader, | ||
| TextField, | ||
| } from "@microbit/ui"; | ||
| import { | ||
| FocusEvent, | ||
| FormEvent, | ||
| ReactNode, | ||
| RefObject, | ||
| useCallback, | ||
| useState, | ||
| } from "react"; | ||
| import { FormattedMessage } from "react-intl"; | ||
| import { uiMessage } from "@microbit/ui/messages"; | ||
| import { uiPatternsMessage } from "../messages"; | ||
|
|
||
| export interface NameProjectDialogProps { | ||
| isOpen: boolean; | ||
| onClose: () => void; | ||
| onSave: (name: string) => void; | ||
| /** The name to start from: the current one, or a default for a new project. */ | ||
| initialName: string; | ||
| /** Defaults to "Name your project". */ | ||
| heading?: ReactNode; | ||
| /** | ||
| * The verb for what saving does ("Create", "Rename", "Confirm and save"). | ||
| * Required: there is always a better label than "Confirm". | ||
| */ | ||
| confirmText: ReactNode; | ||
| /** Shown under the field, e.g. what the name is used for. */ | ||
| helperText?: ReactNode; | ||
| finalFocusRef?: RefObject<HTMLElement>; | ||
| onCloseComplete?: () => void; | ||
| } | ||
|
|
||
| export const isValidProjectName = (name: string): boolean => | ||
| name.trim().length > 0; | ||
|
|
||
| /** | ||
| * Asks for a project name. The field starts with the initial name selected | ||
| * so typing replaces it. Submit with Enter or the confirm button; both are | ||
| * disabled while the name is blank. | ||
| */ | ||
| export const NameProjectDialog = ({ | ||
| isOpen, | ||
| onClose, | ||
| onSave, | ||
| initialName, | ||
| heading, | ||
| confirmText, | ||
| helperText, | ||
| finalFocusRef, | ||
| onCloseComplete, | ||
| }: NameProjectDialogProps) => { | ||
| const [name, setName] = useState(initialName); | ||
| // Start afresh from the initial name each time the dialog opens. Adjusting | ||
| // state during render, as React recommends for state derived from a prop | ||
| // change, rather than an effect that would flash the stale name. | ||
| const [wasOpen, setWasOpen] = useState(isOpen); | ||
| if (isOpen !== wasOpen) { | ||
| setWasOpen(isOpen); | ||
| if (isOpen) { | ||
| setName(initialName); | ||
| } | ||
| } | ||
| const isValid = isValidProjectName(name); | ||
|
|
||
| const handleFocus = useCallback((event: FocusEvent<HTMLInputElement>) => { | ||
| event.target.setSelectionRange(0, event.target.value.length); | ||
| }, []); | ||
| const handleSave = useCallback(() => { | ||
| if (isValid) { | ||
| onSave(name.trim()); | ||
| } | ||
| }, [isValid, name, onSave]); | ||
| const handleSubmit = useCallback( | ||
| (e: FormEvent) => { | ||
| e.preventDefault(); | ||
| handleSave(); | ||
| }, | ||
| [handleSave], | ||
| ); | ||
|
|
||
| return ( | ||
| <Modal | ||
| isOpen={isOpen} | ||
| onClose={onClose} | ||
| size="md" | ||
| finalFocusRef={finalFocusRef} | ||
| onCloseComplete={onCloseComplete} | ||
| > | ||
| <ModalHeader> | ||
| {heading ?? ( | ||
| <FormattedMessage | ||
| {...uiPatternsMessage("ui-patterns.name-project-heading")} | ||
| /> | ||
| )} | ||
| </ModalHeader> | ||
| <ModalCloseButton /> | ||
| <ModalBody> | ||
| <form onSubmit={handleSubmit}> | ||
| <TextField | ||
| label={ | ||
| <FormattedMessage | ||
| {...uiPatternsMessage("ui-patterns.name-label")} | ||
| /> | ||
| } | ||
| value={name} | ||
| onChange={setName} | ||
| onFocus={handleFocus} | ||
| autoFocus | ||
| autoComplete="off" | ||
| isRequired | ||
| isInvalid={!isValid} | ||
| helperText={helperText} | ||
| helperTextCss={{ color: "gray.700" }} | ||
| errorMessage={ | ||
| <FormattedMessage | ||
| {...uiPatternsMessage("ui-patterns.project-name-not-empty")} | ||
| /> | ||
| } | ||
| /> | ||
| </form> | ||
| </ModalBody> | ||
| <ModalFooter css={{ gap: 3 }}> | ||
| <Button onPress={onClose}> | ||
| <FormattedMessage {...uiMessage("ui.cancel-action")} /> | ||
| </Button> | ||
| <Button variant="primary" onPress={handleSave} isDisabled={!isValid}> | ||
| {confirmText} | ||
| </Button> | ||
| </ModalFooter> | ||
| </Modal> | ||
| ); | ||
| }; |
Oops, something went wrong.
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.
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.
This is a bit weird but OK, we'll have to release ui-v0.5.0 first then ui-patterns-0.7.0.