Skip to content

Commit

Permalink
feat(select): add useSelectWithLoading preset (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
reme3d2y committed Jun 17, 2021
1 parent 6c2f0e0 commit 12d542f
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@alfalab/core-components-form-control": "^6.1.1",
"@alfalab/core-components-input": "^6.1.3",
"@alfalab/core-components-popover": "^4.0.0",
"@alfalab/core-components-skeleton": "^1.3.6",
"@alfalab/hooks": "^1.0.0",
"classnames": "^2.2.6",
"downshift": "5.4.7",
Expand Down
46 changes: 46 additions & 0 deletions packages/select/src/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,49 @@ const groups = [{
label='preventFlip'
/>
</Preview>


## Пресеты

### Селект со скелетной загрузкой

```tsx live
const options = [
{ key: '1', content: 'Neptunium' },
{ key: '2', content: 'Plutonium' },
{ key: '3', content: 'Americium' },
{ key: '4', content: 'Curium' },
{ key: '5', content: 'Berkelium' },
{ key: '6', content: 'Californium' },
{ key: '7', content: 'Einsteinium' },
];

function Example() {
const [loading, setLoading] = React.useState(true);

const loadingProps = useSelectWithLoading({
visibleOptions: 6,
loading,
});

const handleOpen = () => {
setLoading(true);
setTimeout(() => setLoading(false), 2000);
};

return (
<Select
options={options}
label='select with loading'
size='l'
block={true}
onOpen={handleOpen}
{...loadingProps}
/>
);
}

render(
<Example />
)
```
1 change: 1 addition & 0 deletions packages/select/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './Component';
export * from './components';
export * from './presets';
export * from './typings';
export * from './utils';
1 change: 1 addition & 0 deletions packages/select/src/presets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useSelectWithLoading/hook';
38 changes: 38 additions & 0 deletions packages/select/src/presets/useSelectWithLoading/hook.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { useCallback } from 'react';
import { Skeleton } from '@alfalab/core-components-skeleton';
import { Option } from '../../components/option';
import { BaseSelectProps, OptionShape } from '../../typings';

import styles from './index.module.css';

type useSelectWithLoadingProps = {
loading?: boolean;
visibleOptions?: BaseSelectProps['visibleOptions'];
};

export function useSelectWithLoading({
loading = false,
visibleOptions = 6,
}: useSelectWithLoadingProps) {
const renderOption = useCallback(
props => (
<Option {...props} Checkmark={null} highlighted={loading ? false : props.highlighted} />
),
[loading],
);

const options: OptionShape[] = Array(visibleOptions)
.fill(0)
.map((_, key) => ({
key: `loading-${key}`,
disabled: true,
content: <Skeleton className={styles.skeleton} visible={true} />,
}));

if (!loading) return null;

return {
Option: renderOption,
options,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.skeleton {
position: absolute;
top: 50%;
transform: translateY(-50%);
height: 16px;
width: 50%;
}
9 changes: 7 additions & 2 deletions packages/select/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
"rootDirs": ["src"],
"baseUrl": ".",
"paths": {
"@alfalab/core-components-*": ["../*/src"],
"@alfalab/core-components-*": ["../*/src"]
}
},
"references": [{ "path": "../form-control" }, { "path": "../popover" }, { "path": "../input" }]
"references": [
{ "path": "../form-control" },
{ "path": "../input" },
{ "path": "../popover" },
{ "path": "../skeleton" }
]
}

0 comments on commit 12d542f

Please sign in to comment.