-
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.
Merge pull request #9 from PaulZhemanov/editable_titles
Editable titles
- Loading branch information
Showing
18 changed files
with
588 additions
and
226 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,92 @@ | ||
import styled from "@emotion/styled" | ||
import React, { ChangeEvent, useEffect, useRef, useState } from "react" | ||
import {IEditableInputProps, StyledInput, StyledText} from "@components/StyledInput"; | ||
|
||
interface IProps extends IEditableInputProps{ | ||
onChange?: (str: string) => void | ||
title?: string | ||
} | ||
|
||
const Root = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
width: auto; | ||
height: 27px; | ||
` | ||
|
||
const EditableTitle: React.FC<IProps> = ({ | ||
fontSize, | ||
color, | ||
fontWeight, | ||
textTransform, | ||
showUnderline = false, | ||
opacity, | ||
title = "", | ||
inputLength, | ||
...rest | ||
}) => { | ||
// const [title, setTitle] = useState<string>(startTitle) | ||
// const inputStore = new InputStore() | ||
const [editing, setEditing] = useState<boolean>(false) | ||
const inputRef = useRef<HTMLInputElement>(null) | ||
|
||
const handleTitleClick = () => { | ||
setEditing(true) | ||
} | ||
|
||
const handleTitleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
rest.onChange && rest.onChange(event?.target.value ?? "") | ||
} | ||
|
||
const handleTitleFix = () => { | ||
if (title.trim() !== "") { | ||
setEditing(false) | ||
} | ||
} | ||
|
||
const handleTitleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (event.key === "Enter" || event.key === "Escape") { | ||
handleTitleFix() | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (editing && inputRef.current) { | ||
inputRef.current.focus() | ||
const length = title.length | ||
inputRef.current.setSelectionRange(length, length) | ||
} | ||
}, [editing, title]) | ||
return ( | ||
<Root> | ||
{editing ? ( | ||
<StyledInput | ||
ref={inputRef} | ||
value={title} | ||
onChange={handleTitleChange} | ||
onBlur={handleTitleFix} | ||
onKeyDown={handleTitleKeyDown} | ||
fontSize={fontSize} | ||
color={color} | ||
fontWeight={fontWeight} | ||
textTransform={textTransform} | ||
showUnderline={showUnderline} | ||
opacity={opacity} | ||
maxLength={inputLength} | ||
/> | ||
) : ( | ||
<StyledText | ||
onDoubleClick={handleTitleClick} | ||
fontSize={fontSize} | ||
textTransform={textTransform} | ||
opacity={opacity} | ||
fontWeight={fontWeight} | ||
color={color} | ||
> | ||
{title} | ||
</StyledText> | ||
)} | ||
</Root> | ||
) | ||
} | ||
export default EditableTitle |
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,39 @@ | ||
import {Text} from "@components/Text"; | ||
import styled from "@emotion/styled"; | ||
|
||
export interface IEditableInputProps { | ||
fontSize?: string | ||
color?: string | ||
fontWeight?: string | ||
textTransform?: string | ||
showUnderline?: boolean | ||
opacity?: string | ||
startTitle?: string | ||
inputLength?: number | undefined | ||
} | ||
|
||
export const StyledInput = styled.input<IEditableInputProps>` | ||
border: none; | ||
outline: none; | ||
background: transparent; | ||
font-size: ${(props) => (props.fontSize ? props.fontSize : "inherit")}; | ||
opacity: ${(props) => (props.opacity ? props.opacity : "inherit")}; | ||
color: ${(props) => (props.color ? props.color : "inherit")}; | ||
font-weight: ${(props) => (props.fontWeight ? props.fontWeight : "inherit")}; | ||
text-transform: ${(props) => | ||
props.textTransform ? props.textTransform : "inherit"}; | ||
border-bottom: ${(props) => | ||
props.showUnderline ? "2px solid #000" : "none"}; | ||
maxlength: ${(props) => (props.inputLength ? props.inputLength : "inherit")}; | ||
` | ||
|
||
|
||
export const StyledText = styled(Text)<IEditableInputProps>` | ||
font-size: ${(props) => (props.fontSize ? props.fontSize : "inherit")}; | ||
text-transform: ${(props) => | ||
props.textTransform ? props.textTransform : "inherit"}; | ||
opacity: ${(props) => (props.opacity ? props.opacity : "inherit")}; | ||
font-weight: ${(props) => (props.fontWeight ? props.fontWeight : "inherit")}; | ||
color: ${(props) => (props.color ? props.color : "inherit")}; | ||
` |
Oops, something went wrong.