Skip to content

Commit

Permalink
allow hyphens in polyvariant path params
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Aug 19, 2024
1 parent 06b041a commit 41607a6
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-parrots-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rescript-relay-router": patch
---

Allow hyphens in polyvariant match branches.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ let renderer = Routes.Root.Todos.ByStatus.Route.makeRenderer(
~prepare=_props => {
()
},
~render=_props => {
React.null
~render=props => {
<>
{switch props.byStatus {
| #completed => React.string("YES")
| #"not-completed" => React.string("NOT COMPLETED")
}}
{React.string((props.byStatus :> string))}
</>
},
)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/client-rendering/src/routes/todoRoutes.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Todos",
"children": [
{
"path": ":byStatus(completed|notCompleted)",
"path": ":byStatus(completed|not-completed)",
"name": "ByStatus",
"children": []
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,12 @@ module Path = {
)
}
| _ =>
switch %re(`/[A-Za-z0-9_]/`)->RegExp.test(char) {
switch %re(`/[A-Za-z0-9_\-]/`)->RegExp.test(char) {
| true => ()
| false =>
ctx.addDecodeError(
~loc=charLoc,
~message=`"${char}" is not a valid character in a path match branch. Path match branches can contain letters, digits, and underscores.`,
~message=`"${char}" is not a valid character in a path match branch. Path match branches can contain letters, digits, underscores and hyphens.`,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ let printablePathParamToTypeStr = p =>
| PrintableRegularPathParam({?pathToCustomModuleWithTypeT}) =>
pathToCustomModuleWithTypeT->Option.getOr("string")
| PrintablePathParamWithMatchBranches({matchArms}) =>
`[${matchArms->Array.map(b => `#${b}`)->Array.join(" | ")}]`
`[${matchArms->Array.map(b => `#"${b}"`)->Array.join(" | ")}]`
}

let printablePathParamToParamName = p =>
Expand Down
2 changes: 1 addition & 1 deletion packages/rescript-relay-router/src/vendor/react-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function compilePath(path, caseSensitive = false, end = true) {
.replace(/\/*\*?$/, "") // Ignore trailing / and /*, we'll handle it below
.replace(/^\/*/, "/") // Make sure it has a leading /
.replace(/[\\.*+^$?{}[\]]/g, "\\$&") // Escape special regex chars
.replace(/:([\w(|)]+)/g, (_, paramName) => {
.replace(/:([\w(|)-]+)/g, (_, paramName) => {
// Check if this is a regexp param. If so, special treatment.
if (paramName.endsWith(")")) {
let [pname, regexp] = paramName.slice(0, -1).split("(");
Expand Down

0 comments on commit 41607a6

Please sign in to comment.