Skip to content

Commit

Permalink
implement #214
Browse files Browse the repository at this point in the history
  • Loading branch information
nandorojo committed Feb 13, 2023
1 parent cfa0005 commit 1a3ce80
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/params/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,18 @@ type Returns<
: NonNullable<NullableUnparsedParsedType>
> = readonly [
state: ParsedType | InitialValue,
setState: (value: ParsedType) => void
setState: (value: ParsedType, options?: SetStateOptions) => void
]

type SetStateOptions = {
/**
* Override whether this function calls `Router.push` or `Router.replace`.
*
* By default, `Router.push` is called if the query parameter already exists in the URL.
*/
webBehavior?: 'push' | 'replace'
}

export function createParam<
Props extends Record<string, unknown> = Record<string, string>
>() {
Expand Down Expand Up @@ -173,7 +182,7 @@ export function createParam<
const hasSetState = useRef(false)

const setState = useCallback(
(value: ParsedType) => {
(value: ParsedType, options?: SetStateOptions) => {
hasSetState.current = true
const { pathname, query } = Router
const newQuery = { ...query }
Expand All @@ -192,7 +201,11 @@ export function createParam<
const willChangeExistingParam =
query[name as string] && newQuery[name as string]

const action = willChangeExistingParam ? Router.replace : Router.push
let action = willChangeExistingParam ? Router.replace : Router.push

if (options?.webBehavior) {
action = Router[options.webBehavior]
}

action(
{
Expand Down Expand Up @@ -284,7 +297,7 @@ export function createParam<

function useParams(): {
params: Props
setParams: (value: Partial<Props>) => void
setParams: (value: Partial<Props>, options?: SetStateOptions) => void
} {
if (Platform.OS !== 'web') {
const nativeRoute = useRoute()
Expand All @@ -302,7 +315,7 @@ export function createParam<

return {
params: nextRouter?.query as Props,
setParams: useCallback((params) => {
setParams: useCallback((params, options) => {
const { pathname, query } = Router
const newQuery = { ...query, ...params }
for (const key in params) {
Expand All @@ -311,7 +324,9 @@ export function createParam<
}
}

Router.push(
const action = Router[options?.webBehavior ?? 'push']

action(
{
pathname,
query: newQuery,
Expand Down

3 comments on commit 1a3ce80

@vercel
Copy link

@vercel vercel bot commented on 1a3ce80 Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

solito-app – ./example-monorepos/blank/apps/next

solito-app-git-master-fernandorojo.vercel.app
solito-app-fernandorojo.vercel.app
solito-app.vercel.app
example.solito.dev

@vercel
Copy link

@vercel vercel bot commented on 1a3ce80 Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

solito-s9oj – ./example-monorepos/with-tailwind/apps/next

solito-s9oj-git-master-beat-gig.vercel.app
solito-s9oj.vercel.app
solito-s9oj-beat-gig.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 1a3ce80 Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

with-custom-fonts – ./example-monorepos/with-custom-font/apps/next

with-custom-fonts.vercel.app
with-custom-fonts-git-master-fernandorojo.vercel.app
with-custom-fonts-fernandorojo.vercel.app
custom-font.example.solito.dev

Please sign in to comment.