Skip to content

Commit

Permalink
feat: 优化picker组件
Browse files Browse the repository at this point in the history
  • Loading branch information
busy-mango committed Oct 7, 2024
1 parent 010e872 commit ec06f87
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 42 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"change-case": "^5.4.4",
"classnames": "^2.5.1",
"dayjs": "^1.11.13",
"framer-motion": "^11.5.4",
"framer-motion": "^11.11.1",
"i18next": "^23.15.1",
"immer": "^10.1.1",
"js-cookie": "^3.0.5",
Expand All @@ -84,7 +84,6 @@
"rc-cascader": "^3.27.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-geiger": "^1.2.0",
"react-i18next": "^15.0.0",
"react-router-dom": "^6.26.2",
"smooth-scroll-into-view-if-needed": "^2.0.2",
Expand Down
22 changes: 5 additions & 17 deletions pnpm-lock.yaml

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

12 changes: 11 additions & 1 deletion src/components/widgets/wheel/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNumber, isString } from '@busymango/is-esm';
import { compact } from '@busymango/utils';
import { and, compact } from '@busymango/utils';

import type { ReactTargetType } from '@/models';
import { iFindElement } from '@/utils';
Expand Down Expand Up @@ -47,3 +47,13 @@ export const iScrollIntoView = (target?: ReactTargetType) => {
inline: 'center',
});
};

export const isSupportSnape = () =>
and(
[
['scroll-snap-type', 'y mandatory'],
['scroll-snap-stop', 'normal'],
['scroll-snap-align', 'center'],
] satisfies [string, string][],
(args) => CSS.supports(...args)
);
14 changes: 9 additions & 5 deletions src/components/widgets/wheel/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
align-items: center;
justify-content: center;

// scroll-snap-stop: normal;
// scroll-snap-align: center;

// transform-style: preserve-3d;
}

.is-scroll-snap {
scroll-snap-type: y mandatory;

& > .option {
scroll-snap-stop: normal;
scroll-snap-align: center;
}
}

.wheel {
width: 100%;
height: 100%;
Expand All @@ -35,8 +41,6 @@
transform-style: preserve-3d;
scroll-behavior: smooth;

// scroll-snap-type: y mandatory;

-webkit-overflow-scrolling: touch;

&::-webkit-scrollbar {
Expand Down
29 changes: 12 additions & 17 deletions src/components/widgets/wheel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useControlState } from '../control';
import {
identified,
iScrollIntoView,
isSupportSnape,
iTarget,
WHEEL_ITEM_ID_NAME,
} from './helpers';
Expand Down Expand Up @@ -62,7 +63,7 @@ const IWheelOption: ReactCFC<IWheelOptionProps> = (props) => {
};

export const IWheel: React.FC<IWheelProps> = (props) => {
const { options } = props;
const { options, isScrollSnape = isSupportSnape() } = props;

const view = useRef<string>();

Expand All @@ -81,19 +82,19 @@ export const IWheel: React.FC<IWheelProps> = (props) => {
iScrollIntoView(element);
}
}
}, 10 * FRAME2MS);
}, 0);

useEffect(() => {
const { current } = container;
if (current) {
if (current && !isScrollSnape) {
const { starer, cancel } = iEventListener;
current?.addEventListener('scrollend', starer);
return () => {
cancel();
current?.removeEventListener('scrollend', starer);
};
}
}, [container, iEventListener]);
}, [container, iEventListener, isScrollSnape]);

const iScroll = useDebounceFunc(() => {
const index = options?.findIndex((option, index) => {
Expand Down Expand Up @@ -124,7 +125,13 @@ export const IWheel: React.FC<IWheelProps> = (props) => {
);

return (
<div ref={container} className={styles.wheel} onScroll={iScroll.starer}>
<div
ref={container}
className={classNames(styles.wheel, {
[styles.isScrollSnap]: isScrollSnape,
})}
onScroll={iScroll.starer}
>
<IWheelOption key={-3} container={container} />
<IWheelOption key={-2} container={container} />
<IWheelOption key={-1} container={container} />
Expand All @@ -147,15 +154,3 @@ export const IWheel: React.FC<IWheelProps> = (props) => {
</div>
);
};

// useEffect(() => {
// const { current } = refs.floating;
// if (open && current) {
// value?.forEach((e, i) => {
// const id = identified(e, i);
// const selector = `[${DATA_ID_NAME}="${id}"]`;
// const element = current.querySelector(selector);
// isHTMLElement(element) && iScrollIntoView(element);
// });
// }
// }, [open, value, refs.floating]);
1 change: 1 addition & 0 deletions src/components/widgets/wheel/models/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface IWheelOptionProps {

export interface IWheelProps {
options?: ControlOption[];
isScrollSnape?: boolean;
value?: ControlOption['value'];
onChange?: (value?: ControlOption['value']) => void;
}

0 comments on commit ec06f87

Please sign in to comment.