-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve zone assignment label addition and editing functionality (
#3034) * feat: improve zone assignment label addition and editing functionality * refactor: display the add button when there are no assignment * refactor: truncate the link angle value to one decimal * refactor: apply 'bold' font weight to labels when the mouse is over the zone
- Loading branch information
1 parent
c02ed38
commit d626f5e
Showing
7 changed files
with
387 additions
and
37 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,88 @@ | ||
/** @jsxImportSource @emotion/react */ | ||
import { css } from '@emotion/react'; | ||
import { Popover, PopoverProps } from '@blueprintjs/core'; | ||
import { Formik } from 'formik'; | ||
import { ReactNode, useState } from 'react'; | ||
import * as Yup from 'yup'; | ||
|
||
import FormikInput from '../elements/formik/FormikInput'; | ||
|
||
type InputType = 'number' | 'text'; | ||
|
||
interface FieldEditionsProps { | ||
value: number | string; | ||
inputType?: InputType; | ||
onChange: (value: any) => void; | ||
children: ReactNode; | ||
PopoverProps?: PopoverProps; | ||
} | ||
const InputDimension = { height: 28, width: 100 }; | ||
|
||
const validationSchema = (inputType: 'number' | 'text') => | ||
Yup.object({ | ||
value: (inputType === 'number' ? Yup.number() : Yup.string()).required(), | ||
}); | ||
|
||
function stopPropagation(e) { | ||
e.stopPropagation(); | ||
} | ||
|
||
function keyDownCheck(event: React.KeyboardEvent<HTMLInputElement>) { | ||
if (event.key === 'Enter') { | ||
return true; | ||
} else if (event.key === 'Escape') { | ||
return false; | ||
} | ||
} | ||
|
||
export function FieldEdition(props: FieldEditionsProps) { | ||
const { value, inputType = 'text', onChange, children, PopoverProps } = props; | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
function handleOnSubmit({ value: newValue }) { | ||
onChange(newValue); | ||
setIsOpen(false); | ||
} | ||
|
||
return ( | ||
<Popover | ||
css={css` | ||
.field-edition-popover { | ||
border-radius: 5px; | ||
} | ||
`} | ||
popoverClassName="field-edition-popover" | ||
isOpen={isOpen} | ||
onClose={() => setIsOpen(false)} | ||
onInteraction={() => setIsOpen(true)} | ||
content={ | ||
<Formik | ||
initialValues={{ value }} | ||
onSubmit={handleOnSubmit} | ||
validationSchema={validationSchema(inputType)} | ||
> | ||
{({ submitForm }) => ( | ||
<FormikInput | ||
type={inputType} | ||
style={{ | ||
input: { | ||
height: `${InputDimension.height}px`, | ||
padding: '5px', | ||
outline: 'none', | ||
}, | ||
}} | ||
name="value" | ||
autoSelect | ||
onKeyDown={(e) => keyDownCheck(e) && submitForm()} | ||
onClick={stopPropagation} | ||
onMouseDown={stopPropagation} | ||
/> | ||
)} | ||
</Formik> | ||
} | ||
{...PopoverProps} | ||
> | ||
{children} | ||
</Popover> | ||
); | ||
} |
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
Oops, something went wrong.