-
Notifications
You must be signed in to change notification settings - Fork 1
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
fix(link-editor): update behaviour so that we can save on link collection items #863
Merged
+439
−251
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b200089
refactor: linkmodal
seaerchin b7a376a
chore: update props
seaerchin 6fa524b
fix: discard previous value if does not start with https
seaerchin 74ad870
fix: write to external store on change
seaerchin 889e80b
fix: consistent behaviour for toggling between radios
seaerchin f566f6b
fix: standardise handling of radio tabs
seaerchin 07c9b34
fix: overflow for links
seaerchin f871ce2
chore: remove unused files
seaerchin e005569
fix: remove extra imporst
seaerchin 8e36f83
chore: remove usequery
seaerchin bf87ddd
chore: add stories
seaerchin 77620f8
chore: add stories
seaerchin ccaa1ec
fix: remove errors
seaerchin 159e04d
fix: copy
seaerchin e27b0dd
chore: copy
seaerchin e9015f2
fix: update copy
seaerchin 099ec6c
Merge branch 'main' into fix/file-collections
seaerchin b5928d2
chore: add missing import
seaerchin 5c61988
chore: handle optional case
seaerchin bd10a6c
chore: fix types
seaerchin 9cced01
chore: fix copy
seaerchin c2e225c
chore: fix aria
seaerchin a5e2468
chore: use const
seaerchin 4c2dfbe
fix: use pick rather than respecifying
seaerchin a1245ad
Merge branch 'main' into fix/file-collections
seaerchin b52aab7
chore: rename
seaerchin 8a5114d
Merge branch 'main' of https://github.com/opengovsg/isomer into fix/f…
adriangohjw ddb6385
fix - remove additional story
adriangohjw 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 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
63 changes: 63 additions & 0 deletions
63
apps/studio/src/features/editing-experience/components/LinkEditor/LinkEditorContext.tsx
This file contains 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,63 @@ | ||
import type { PropsWithChildren } from "react" | ||
import { createContext, useContext, useState } from "react" | ||
|
||
import type { | ||
LinkTypeMapping, | ||
LinkTypes, | ||
} from "~/features/editing-experience/components/LinkEditor/constants" | ||
import { LINK_TYPES } from "~/features/editing-experience/components/LinkEditor/constants" | ||
|
||
export type LinkEditorContextReturn = ReturnType<typeof useLinkEditorContext> | ||
const LinkEditorContext = createContext<LinkEditorContextReturn | undefined>( | ||
undefined, | ||
) | ||
|
||
interface UseLinkEditorContextProps { | ||
linkHref: string | ||
linkTypes: Partial<LinkTypeMapping> | ||
error?: string | ||
onChange: (value: string) => void | ||
} | ||
const useLinkEditorContext = ({ | ||
linkHref, | ||
linkTypes, | ||
error, | ||
onChange, | ||
}: UseLinkEditorContextProps) => { | ||
const [curType, setCurType] = useState<LinkTypes>(LINK_TYPES.Page) | ||
const [curHref, setHref] = useState(linkHref) | ||
|
||
return { | ||
linkTypes, | ||
curHref, | ||
setHref: (value: string) => { | ||
onChange(value) | ||
setHref(value) | ||
}, | ||
error, | ||
curType, | ||
setCurType, | ||
} | ||
} | ||
|
||
export const LinkEditorContextProvider = ({ | ||
children, | ||
...passthroughProps | ||
}: PropsWithChildren<UseLinkEditorContextProps>) => { | ||
const values = useLinkEditorContext(passthroughProps) | ||
return ( | ||
<LinkEditorContext.Provider value={values}> | ||
{children} | ||
</LinkEditorContext.Provider> | ||
) | ||
} | ||
|
||
export const useLinkEditor = () => { | ||
const context = useContext(LinkEditorContext) | ||
if (!context) { | ||
throw new Error( | ||
`useLinkEditor must be used within a LinkEditorContextProvider component`, | ||
) | ||
} | ||
return context | ||
} |
90 changes: 90 additions & 0 deletions
90
apps/studio/src/features/editing-experience/components/LinkEditor/LinkEditorRadioGroup.tsx
This file contains 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,90 @@ | ||
import type { UseRadioProps } from "@chakra-ui/react" | ||
import type { PropsWithChildren } from "react" | ||
import { | ||
Box, | ||
HStack, | ||
Icon, | ||
Text, | ||
useRadio, | ||
useRadioGroup, | ||
} from "@chakra-ui/react" | ||
|
||
import type { LinkTypes } from "./constants" | ||
import { LINK_TYPES } from "./constants" | ||
import { useLinkEditor } from "./LinkEditorContext" | ||
|
||
const LinkTypeRadioCard = ({ | ||
children, | ||
...rest | ||
}: PropsWithChildren<UseRadioProps>) => { | ||
const { getInputProps, getRadioProps } = useRadio(rest) | ||
|
||
return ( | ||
<Box | ||
as="label" | ||
_first={{ | ||
"> div": { | ||
borderLeftRadius: "base", | ||
}, | ||
}} | ||
_last={{ | ||
"> div": { | ||
borderRightRadius: "base", | ||
}, | ||
}} | ||
> | ||
<input {...getInputProps()} /> | ||
|
||
<Box | ||
{...getRadioProps()} | ||
cursor="pointer" | ||
border="1px solid" | ||
borderColor="base.divider.strong" | ||
bgColor="utility.ui" | ||
px="1rem" | ||
py="0.5rem" | ||
mx={0} | ||
_checked={{ | ||
bgColor: "interaction.muted.main.active", | ||
color: "interaction.main.default", | ||
borderColor: "interaction.main.default", | ||
}} | ||
textTransform="none" | ||
fontWeight={500} | ||
lineHeight="1.25rem" | ||
> | ||
{children} | ||
</Box> | ||
</Box> | ||
) | ||
} | ||
|
||
export const LinkEditorRadioGroup = () => { | ||
const { linkTypes, setCurType } = useLinkEditor() | ||
const { getRootProps, getRadioProps } = useRadioGroup({ | ||
name: "link-type", | ||
defaultValue: LINK_TYPES.Page, | ||
// NOTE: This is a safe cast because we map over the `linkTypes` below | ||
// so each time we are using the `linkType` | ||
onChange: (value) => setCurType(value as LinkTypes), | ||
}) | ||
|
||
return ( | ||
<HStack {...getRootProps()} spacing={0}> | ||
{Object.entries(linkTypes).map(([key, props]) => { | ||
if (!props) return null | ||
const { icon, label } = props | ||
const radio = getRadioProps({ value: key }) | ||
|
||
return ( | ||
<LinkTypeRadioCard key={key} {...radio}> | ||
<HStack spacing={2}> | ||
<Icon as={icon} fontSize="1.25rem" /> | ||
<Text textStyle="subhead-2">{label}</Text> | ||
</HStack> | ||
</LinkTypeRadioCard> | ||
) | ||
})} | ||
</HStack> | ||
) | ||
} |
Oops, something went wrong.
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.
use
LinkTypeMapping
instead?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.
i actually tried this and it didn't quite work. this is because when we say
Record<LinkTypes, string>
, actually what we mean is a 1:1 mapping (ie, every type inLinkType
must have a corresponding string) which isn't quite what we want here. what we want is "some subset of LinkTypes to string" - i considered usingPartial<Record<LinkTypes, string>>
, but unfortunately that means that every type is now optional. hence, i just went iwth string for now