Skip to content
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

Add search params (query params) support #43

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ A higher order component that adds [`context.wizard`](#contextwizard) as a `wiza
* `step` (object): Describes the current step with structure: `{ id: string }`.
* `steps` (array): Array of `step` objects in the order they were declared within `<Steps>`.
* `history` (object): The backing [`history`](https://github.com/ReactTraining/history#properties) object.
* `preserveSearch` (bool): Determines if react-albus should preserve search params (query string params) when it handles history navigation
* `next()` (function): Moves to the next step in order.
* `previous()` (function): Moves to the previous step in order.
* `go(n)` (function): Moves `n` steps in history.
Expand Down
44 changes: 44 additions & 0 deletions __tests__/components/Wizard.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,48 @@ describe('Wizard', () => {
mounted.unmount();
});
});

describe('with existing history and preserving search params', () => {
const history = {
replace: () => null,
listen: () => () => null,
location: {
pathname: '/gryffindor',
search: '?foo=bar',
},
};

let wizard;
let mounted;
beforeEach(() => {
mounted = mount(
<Wizard history={history} preserveSearch={true}>
<WithWizard>
{prop => {
wizard = prop;
return null;
}}
</WithWizard>
<Steps>
<Step id="gryffindor">
<div />
</Step>
<Step id="slytherin">
<div />
</Step>
</Steps>
</Wizard>
);
});

it('call next and ensure url params are still in the url', () => {
wizard.history.push = jest.fn();
wizard.next();
expect(wizard.history.push).toBeCalledWith('/slytherin?foo=bar');
});

afterEach(() => {
mounted.unmount();
});
});
});
14 changes: 11 additions & 3 deletions src/components/Wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class Wizard extends Component {
return `${this.props.basename}/`;
}

get historySuffix() {
return this.props.preserveSearch ? this.history.location.search : '';
}

get ids() {
return this.state.steps.map(s => s.id);
}
Expand All @@ -82,13 +86,15 @@ class Wizard extends Component {
if (step.id) {
this.setState({ step });
} else {
this.history.replace(`${this.basename}${this.ids[0]}`);
this.history.replace(`${this.basename}${this.ids[0]}${this.historySuffix}`);
}
});
};

push = (step = this.nextStep) => this.history.push(`${this.basename}${step}`);
replace = (step = this.nextStep) => this.history.replace(`${this.basename}${step}`);
push = (step = this.nextStep) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

history.replace and history.push can accept a location-like object which will preserve query params. check out https://github.com/ReactTraining/history/tree/v4.6.0#navigation. Using this, you can remove historySuffix and do something like this:

replace = (step = this.nextStep) => {
  if (this.props.preserveSearch) {
    this.history.replace({
      ...this.history.location,
      pathname:`${this.basename}${step}`
    })
  } else {
    this.history.replace(`${this.basename}${step}`);
  }
}

this.history.push(`${this.basename}${step}${this.historySuffix}`);
replace = (step = this.nextStep) =>
this.history.replace(`${this.basename}${step}${this.historySuffix}`);

next = () => {
if (this.props.onNext) {
Expand All @@ -106,6 +112,7 @@ class Wizard extends Component {

Wizard.propTypes = {
basename: PropTypes.string,
preserveSearch: PropTypes.bool,
history: PropTypes.shape({
entries: PropTypes.array,
go: PropTypes.func,
Expand All @@ -120,6 +127,7 @@ Wizard.propTypes = {

Wizard.defaultProps = {
basename: '',
preserveSearch: false,
history: null,
onNext: null,
render: null,
Expand Down