Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 15 additions & 9 deletions src/Forward/Forward.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import React, { FC, AnchorHTMLAttributes, MutableRefObject } from 'react';
import React, { FC, AnchorHTMLAttributes } from 'react';
import classNames from 'classnames';

export interface ForwardProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
/** Classi aggiuntive da usare per il componente Forward */
className?: string;
/** Riferimento al nodo a cui scorrere quando premuto */
scrollToRef: MutableRefObject<Element | null>;
// scrollToRef: MutableRefObject<Element | null>;
testId?: string;
}

export const Forward: FC<ForwardProps> = ({ className, scrollToRef, children, testId, ...attributes }) => {
export const Forward: FC<ForwardProps> = ({ className, children, testId, ...attributes }) => {
const classes = classNames(className, 'forward');
return (
<a
className={classes}
onClick={() =>
scrollToRef.current?.scrollIntoView({
behavior: 'smooth',
block: 'start'
})
}
onClick={(e) => {
e.preventDefault()
if (attributes.href) {
const scrollToRef = document.querySelector(attributes.href)
if (scrollToRef) {
scrollToRef.scrollIntoView({
behavior: 'smooth',
block: 'start'
})
}
}
}}
data-testid={testId}
{...attributes}
>
Expand Down
6 changes: 3 additions & 3 deletions stories/Components/Forward.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export default meta;
type Story = StoryObj<typeof Forward>;

const EsempiHooks = () => {
const ref = React.useRef(null);
// const ref = React.useRef(null);
return (
<div>
<Card>
<Forward scrollToRef={ref}>
<Forward href='#hereSection' aria-label='Vai a: Sezione di esempio'>
<Icon color='primary' icon='it-expand' />
</Forward>
</Card>
Expand Down Expand Up @@ -77,7 +77,7 @@ const EsempiHooks = () => {
commodo ea sit eu.
</p>
</div>
<div ref={ref}>Scroll to Here</div>
<div id="hereSection">Scroll to Here</div>
</div>
)

Expand Down
Loading