Skip to content

Commit 0ef9f1a

Browse files
committed
Add "setParameters" to stack routes.
1 parent b231a2e commit 0ef9f1a

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

react-native/components/createStackRoutingComponent/index.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ export const createStackRoutingComponent = <
129129
reset: (...replacementItems) => {
130130
props.setRouteState(replacementItems);
131131
},
132+
setParameters: (parameters) => {
133+
const routeStateCopy = [...props.routeState];
134+
135+
routeStateCopy.splice(index, 1, {
136+
...item,
137+
parameters,
138+
});
139+
140+
props.setRouteState(routeStateCopy);
141+
},
132142
bottom: index === 0,
133143
top: index === props.routeState.length - 1,
134144
...props,

react-native/components/createStackRoutingComponent/unit.tsx

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ test(`can render one item`, async () => {
120120
pop: expect.any(Function),
121121
replace: expect.any(Function),
122122
reset: expect.any(Function),
123+
setParameters: expect.any(Function),
123124
bottom: true,
124125
top: true,
125126
parameters: {
@@ -367,6 +368,7 @@ test(`can render two items`, async () => {
367368
pop: expect.any(Function),
368369
replace: expect.any(Function),
369370
reset: expect.any(Function),
371+
setParameters: expect.any(Function),
370372
bottom: true,
371373
top: false,
372374
parameters: {
@@ -404,6 +406,7 @@ test(`can render two items`, async () => {
404406
pop: expect.any(Function),
405407
replace: expect.any(Function),
406408
reset: expect.any(Function),
409+
setParameters: expect.any(Function),
407410
bottom: false,
408411
top: true,
409412
parameters: {
@@ -668,6 +671,7 @@ test(`can render three items`, async () => {
668671
pop: expect.any(Function),
669672
replace: expect.any(Function),
670673
reset: expect.any(Function),
674+
setParameters: expect.any(Function),
671675
bottom: true,
672676
top: false,
673677
parameters: {
@@ -705,6 +709,7 @@ test(`can render three items`, async () => {
705709
pop: expect.any(Function),
706710
replace: expect.any(Function),
707711
reset: expect.any(Function),
712+
setParameters: expect.any(Function),
708713
bottom: false,
709714
top: false,
710715
parameters: {
@@ -742,6 +747,7 @@ test(`can render three items`, async () => {
742747
pop: expect.any(Function),
743748
replace: expect.any(Function),
744749
reset: expect.any(Function),
750+
setParameters: expect.any(Function),
745751
bottom: false,
746752
top: true,
747753
parameters: {
@@ -2038,3 +2044,148 @@ test(`reset`, async () => {
20382044

20392045
renderer.unmount();
20402046
});
2047+
2048+
test(`setParameters`, async () => {
2049+
type ParametersA = {
2050+
readonly testRouteAParameterKey:
2051+
| `Test Route A Parameter Value A`
2052+
| `Test Route A Parameter Value B`
2053+
| `Test Route A Parameter Value C`;
2054+
};
2055+
2056+
type ParametersB = {
2057+
readonly testRouteBParameterKey:
2058+
| `Test Route B Parameter Value A`
2059+
| `Test Route B Parameter Value B`;
2060+
};
2061+
2062+
type ParametersC = {
2063+
readonly testRouteCParameterKey:
2064+
| `Test Route C Parameter Value A`
2065+
| `Test Route C Parameter Value B`;
2066+
};
2067+
2068+
type Parameters = {
2069+
testRouteAKey: ParametersA;
2070+
testRouteBKey: ParametersB;
2071+
testRouteCKey: ParametersC;
2072+
};
2073+
2074+
type OtherProps = {
2075+
exampleOtherPropKey: `Example Other Prop Value`;
2076+
};
2077+
2078+
const RouteA: StackRoute<Parameters, `testRouteAKey`, OtherProps> = ({
2079+
parameters: { testRouteAParameterKey },
2080+
exampleOtherPropKey,
2081+
}) => (
2082+
<Text>
2083+
Example Route A with parameter {testRouteAParameterKey}{" "}
2084+
{exampleOtherPropKey}
2085+
</Text>
2086+
);
2087+
2088+
const RouteB: StackRoute<Parameters, `testRouteBKey`, OtherProps> = ({
2089+
parameters: { testRouteBParameterKey },
2090+
exampleOtherPropKey,
2091+
}) => (
2092+
<Text>
2093+
Example Route B with parameter {testRouteBParameterKey}{" "}
2094+
{exampleOtherPropKey}
2095+
</Text>
2096+
);
2097+
2098+
const RouteC: StackRoute<Parameters, `testRouteCKey`, OtherProps> = ({
2099+
parameters: { testRouteCParameterKey },
2100+
exampleOtherPropKey,
2101+
}) => (
2102+
<Text>
2103+
Example Route C with parameter {testRouteCParameterKey}{" "}
2104+
{exampleOtherPropKey}
2105+
</Text>
2106+
);
2107+
2108+
const routeTable: StackRouteTable<Parameters, OtherProps> = {
2109+
testRouteAKey: RouteA,
2110+
testRouteBKey: RouteB,
2111+
testRouteCKey: RouteC,
2112+
};
2113+
2114+
const routeState: StackRouterState<Parameters> = [
2115+
{
2116+
uuid: `ec055b0f-0659-4e9a-a889-06a7586bb61a`,
2117+
key: `testRouteBKey`,
2118+
parameters: {
2119+
testRouteBParameterKey: `Test Route B Parameter Value A`,
2120+
},
2121+
},
2122+
{
2123+
uuid: `f36ce5e7-d37e-443a-8635-718118c27128`,
2124+
key: `testRouteAKey`,
2125+
parameters: {
2126+
testRouteAParameterKey: `Test Route A Parameter Value A`,
2127+
},
2128+
},
2129+
{
2130+
uuid: "345d1eff-3d1d-4d93-8136-e0c3ff0f7f7c",
2131+
key: `testRouteAKey`,
2132+
parameters: {
2133+
testRouteAParameterKey: `Test Route A Parameter Value B`,
2134+
},
2135+
},
2136+
];
2137+
2138+
const setRouteState = jest.fn();
2139+
const onBack = jest.fn();
2140+
2141+
const Component = createStackRoutingComponent(routeTable);
2142+
2143+
const renderer = TestRenderer.create(
2144+
<Component
2145+
routeState={routeState}
2146+
setRouteState={setRouteState}
2147+
exampleOtherPropKey="Example Other Prop Value"
2148+
onBack={onBack}
2149+
/>
2150+
);
2151+
2152+
await new Promise<void>((resolve) => {
2153+
setTimeout(resolve, 100);
2154+
});
2155+
2156+
(
2157+
renderer.toTree()
2158+
?.rendered as ReadonlyArray<TestRenderer.ReactTestRendererTree>
2159+
)[1]?.props[`children`].props.children.props.setParameters({
2160+
testRouteAParameterKey: `Test Route A Parameter Value C`,
2161+
});
2162+
2163+
expect(setRouteState).toBeCalledTimes(1);
2164+
expect(setRouteState).toHaveBeenCalledWith([
2165+
{
2166+
uuid: `ec055b0f-0659-4e9a-a889-06a7586bb61a`,
2167+
key: `testRouteBKey`,
2168+
parameters: {
2169+
testRouteBParameterKey: `Test Route B Parameter Value A`,
2170+
},
2171+
},
2172+
{
2173+
uuid: `f36ce5e7-d37e-443a-8635-718118c27128`,
2174+
key: `testRouteAKey`,
2175+
parameters: {
2176+
testRouteAParameterKey: `Test Route A Parameter Value C`,
2177+
},
2178+
},
2179+
{
2180+
uuid: "345d1eff-3d1d-4d93-8136-e0c3ff0f7f7c",
2181+
key: `testRouteAKey`,
2182+
parameters: {
2183+
testRouteAParameterKey: `Test Route A Parameter Value B`,
2184+
},
2185+
},
2186+
]);
2187+
2188+
expect(onBack).not.toHaveBeenCalled();
2189+
2190+
renderer.unmount();
2191+
});

react-native/types/StackRoute/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ export type StackRoute<
6666
*/
6767
reset(...replacementItems: ReadonlyArray<Item<TRouteParameters>>): void;
6868

69+
/**
70+
* Changes the parameters of this card.
71+
* @param parameters The replacement parameters.
72+
*/
73+
setParameters(parameters: TRouteParameters[TRouteKey]): void;
74+
6975
/**
7076
* The route parameters for this card.
7177
*/

0 commit comments

Comments
 (0)