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

Hookify more components #2083

Merged
merged 2 commits into from
Sep 11, 2019
Merged
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 UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f
### Code quality

- Migrated `ContextualSaveBar` to use hooks instead of `withAppProvider` ([#2091](https://github.com/Shopify/polaris-react/pull/2091))
- Migrated `RangeSlider`, `ScrollLock` and `TopBar.SearchField` to use hooks instead of withAppProvider ([#2083](https://github.com/Shopify/polaris-react/pull/2083))

### Deprecations
61 changes: 23 additions & 38 deletions src/components/RangeSlider/RangeSlider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import React from 'react';
import {createUniqueIDFactory} from '@shopify/javascript-utilities/other';
import {
withAppProvider,
WithAppProviderProps,
} from '../../utilities/with-app-provider';
import {useUniqueId} from '../../utilities/unique-id';
import {RangeSliderProps, RangeSliderValue, DualValue} from './types';
import {RangeSliderDefault} from './utilities';

Expand All @@ -14,42 +10,31 @@ import {SingleThumb, DualThumb} from './components';
// ensures that the Props Explorer table is generated correctly, instead of
// crashing if we write `RangeSlider extends React.Component<RangeSliderProps>`
interface Props extends RangeSliderProps {}
type CombinedProps = Props & WithAppProviderProps;

const getUniqueID = createUniqueIDFactory('RangeSlider');

class RangeSlider extends React.Component<CombinedProps> {
private id = getUniqueID();

render() {
const {
min = RangeSliderDefault.Min,
max = RangeSliderDefault.Max,
step = RangeSliderDefault.Step,
value,
...rest
} = this.props;

const sharedProps = {
id: this.id,
min,
max,
step,
...rest,
};

return isDualThumb(value) ? (
<DualThumb value={value} {...sharedProps} />
) : (
<SingleThumb value={value} {...sharedProps} />
);
}
export function RangeSlider({
min = RangeSliderDefault.Min,
max = RangeSliderDefault.Max,
step = RangeSliderDefault.Step,
value,
...rest
}: Props) {
const id = useUniqueId('RangeSlider');

const sharedProps = {
id,
min,
max,
step,
...rest,
};

return isDualThumb(value) ? (
<DualThumb value={value} {...sharedProps} />
) : (
<SingleThumb value={value} {...sharedProps} />
);
}

function isDualThumb(value: RangeSliderValue): value is DualValue {
return Array.isArray(value);
}

// Use named export once withAppProvider is refactored away
// eslint-disable-next-line import/no-default-export
export default withAppProvider<Props>()(RangeSlider);
4 changes: 1 addition & 3 deletions src/components/RangeSlider/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import RangeSlider from './RangeSlider';

export {RangeSlider};
export {RangeSlider} from './RangeSlider';
export {RangeSliderProps, RangeSliderValue, DualValue} from './types';
2 changes: 1 addition & 1 deletion src/components/RangeSlider/tests/RangeSlider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import {mountWithAppProvider} from 'test-utilities/legacy';
import RangeSlider from '../RangeSlider';
import {RangeSlider} from '../RangeSlider';
import {DualThumb, SingleThumb} from '../components';
import {RangeSliderDefault} from '../utilities';

Expand Down
21 changes: 21 additions & 0 deletions src/components/ScrollLock/ScrollLock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {useEffect} from 'react';
import {useScrollLockManager} from '../../utilities/scroll-lock-manager';
import './ScrollLock.scss';

interface ScrollLockProps {}
BPScott marked this conversation as resolved.
Show resolved Hide resolved

// Even though this has no args, reference ScrollLockProps so the prop explorer
Copy link
Member

Choose a reason for hiding this comment

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

Will this show up in the style guide without a Props interface?

Copy link
Member

Choose a reason for hiding this comment

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

On second thought, looks like we don't have a readme and this is just an internal component.

Copy link
Member Author

@BPScott BPScott Sep 11, 2019

Choose a reason for hiding this comment

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

Per my proposal in #1959 we now name Props after the component - see ScrollLockProps above this. This stops us having to rename the interface when reexporting it in src/components/index.ts and src/components/ScrollLock/index.ts

The styleguide was updated to handle this in https://github.com/Shopify/polaris-styleguide/pull/2932

// in the styleguide works without warnings about unfound props
export function ScrollLock(_: ScrollLockProps) {
BPScott marked this conversation as resolved.
Show resolved Hide resolved
const scrollLockManager = useScrollLockManager();

useEffect(() => {
scrollLockManager.registerScrollLock();

return () => {
scrollLockManager.unregisterScrollLock();
};
}, [scrollLockManager]);

return null;
}
29 changes: 0 additions & 29 deletions src/components/ScrollLock/ScrollLock.tsx

This file was deleted.

4 changes: 1 addition & 3 deletions src/components/ScrollLock/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import ScrollLock from './ScrollLock';

export {ScrollLock};
export {ScrollLock} from './ScrollLock';
2 changes: 1 addition & 1 deletion src/components/ScrollLock/tests/ScrollLock.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import {mountWithAppProvider} from 'test-utilities/legacy';
import {SCROLL_LOCKING_ATTRIBUTE} from '../../../utilities/scroll-lock-manager';
import ScrollLock from '../ScrollLock';
import {ScrollLock} from '../ScrollLock';

describe('ScrollLock', () => {
let scrollSpy: jest.SpyInstance;
Expand Down
Loading