-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30ccd24
commit f6098a2
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { useCallback, useEffect, useReducer, useRef } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
import { KeyCode, useKeyboard } from '../hooks/useKeyboard'; | ||
import { useClickOutside } from '../hooks/useClickOutside'; | ||
import { nullable } from '../utils'; | ||
|
||
import { Form } from './Form'; | ||
|
||
interface RenderTriggerProps { | ||
onClick: () => void; | ||
} | ||
|
||
interface InlineFormProps { | ||
renderTrigger: (props: RenderTriggerProps) => React.ReactNode; | ||
onSubmit: () => Promise<void>; | ||
children: React.ReactNode; | ||
onReset: () => void; | ||
className?: string; | ||
} | ||
|
||
const StyledForm = styled(Form)` | ||
background-color: transparent; | ||
`; | ||
|
||
const StyledWrapper = styled.div` | ||
display: contents; | ||
} | ||
`; | ||
|
||
export const InlineForm: React.FC<InlineFormProps> = ({ renderTrigger, onSubmit, onReset, children, className }) => { | ||
const wrapperRef = useRef<HTMLDivElement>(null); | ||
const [visible, toggleVisible] = useReducer((state) => !state, !renderTrigger); | ||
|
||
const [onESC] = useKeyboard( | ||
[KeyCode.Escape], | ||
() => { | ||
if (visible) { | ||
toggleVisible(); | ||
} | ||
}, | ||
{ | ||
capture: true, | ||
}, | ||
); | ||
|
||
useClickOutside(wrapperRef, () => { | ||
if (visible) { | ||
toggleVisible(); | ||
} | ||
}); | ||
|
||
const handleSubmit = useCallback(async () => { | ||
await onSubmit(); | ||
toggleVisible(); | ||
onReset(); | ||
}, [onSubmit, onReset]); | ||
|
||
useEffect(() => { | ||
if (!visible) { | ||
onReset(); | ||
} | ||
}, [visible, onReset]); | ||
|
||
return ( | ||
<StyledWrapper ref={wrapperRef} className={className} {...onESC}> | ||
{nullable(!visible, () => renderTrigger({ onClick: toggleVisible }))} | ||
{nullable(visible, () => ( | ||
<StyledForm onSubmit={handleSubmit}>{children}</StyledForm> | ||
))} | ||
</StyledWrapper> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
import { action } from '@storybook/addon-actions'; | ||
import { Meta, StoryFn } from '@storybook/react'; | ||
import styled from 'styled-components'; | ||
import { IconQuestionCircleOutline } from '@taskany/icons'; | ||
|
||
import { InlineForm } from '../components/InlineForm'; | ||
import { Button } from '../components/Button'; | ||
import { Form } from '../components/Form'; | ||
import { Popup } from '../components/Popup'; | ||
import { FormInput } from '../components/FormInput'; | ||
|
||
export default { | ||
title: 'InlineForm', | ||
component: InlineForm, | ||
argTypes: { | ||
onSubmit: { action: 'form submitted' }, | ||
onClick: { action: 'clecked' }, | ||
onMouseOver: { action: 'onMouseOver' }, | ||
onMouseLeave: { action: 'onMouseLeave' }, | ||
onChange: { action: 'onChange' }, | ||
}, | ||
} as Meta<typeof InlineForm>; | ||
|
||
const StyledForm = styled(Form)` | ||
background-color: transparent; | ||
display: flex; | ||
align-items: center; | ||
`; | ||
|
||
const StyledQuestionIcon = styled.div` | ||
margin: 5px 0 0 4px; | ||
`; | ||
|
||
export const InlineFormStories: StoryFn = () => { | ||
return ( | ||
<> | ||
<StyledForm onSubmit={action('form submitted')}> | ||
<FormInput autoFocus brick="right" placeholder={'Enter criteria'} onChange={action('onChange')} /> | ||
<FormInput | ||
autoComplete="off" | ||
placeholder={'No more than 100'} | ||
brick="center" | ||
onChange={action('onChange')} | ||
/> | ||
<Button text={'Add'} brick="left" type="submit" view="primary" size="l" outline /> | ||
<StyledQuestionIcon> | ||
<a onMouseOver={action('onMouseOver')} onMouseLeave={action('onMouseLeave')}> | ||
<IconQuestionCircleOutline noWrap size="s" /> | ||
</a> | ||
</StyledQuestionIcon> | ||
<Popup tooltip placement="top-end"> | ||
Hint | ||
</Popup> | ||
</StyledForm> | ||
</> | ||
); | ||
}; |