-
Notifications
You must be signed in to change notification settings - Fork 4
[WIP] replace editable title with borderless textfield #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
096c06c
18fa839
06d765e
ff66d96
b927ac0
281324a
34d9d8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import React, { useState, useEffect } from 'react' | ||
| import React, { useState, useEffect, useRef } from 'react' | ||
|
|
||
| import { Icons, TextField, makeStyles, getTailwindConfigColor } from '@eqworks/lumen-labs' | ||
| import { TextField, makeStyles, getTailwindConfigColor } from '@eqworks/lumen-labs' | ||
|
|
||
| import { useStoreState, useStoreActions } from '../../store' | ||
| import CustomButton from '../../components/custom-button' | ||
| import modes from '../../constants/modes' | ||
| import { getTextSize } from '../../util/string-manipulation' | ||
|
|
||
|
|
||
| const commonClasses = { | ||
|
|
@@ -13,6 +13,24 @@ const commonClasses = { | |
| alignItems: 'center', | ||
| margin: '0 0.6rem', | ||
| display: 'flex', | ||
|
|
||
| '& .textfield-container': { | ||
| backgroundColor: getTailwindConfigColor('secondary-100'), | ||
|
|
||
| '& .textfield-root': { | ||
| borderTop: 0, | ||
|
|
||
| '& .textfield-input': { | ||
| fontFamily: 'Open Sans', | ||
| outlineWidth: 0, | ||
| outlineColor: 'transparent', | ||
| }, | ||
| }, | ||
|
|
||
| '& .textfield-root:focus-within': { | ||
| borderColor: getTailwindConfigColor('primary-500'), | ||
| }, | ||
| }, | ||
| }, | ||
| button: { | ||
| marginLeft: '0.4rem', | ||
|
|
@@ -29,100 +47,102 @@ const commonClasses = { | |
| }, | ||
| } | ||
|
|
||
| const useStyles = (mode) => makeStyles( | ||
| mode === modes.EDITOR | ||
| ? { | ||
| title: { | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| color: getTailwindConfigColor('secondary-600'), | ||
| fontWeight: 700, | ||
| background: getTailwindConfigColor('secondary-100'), | ||
| fontSize: '0.875rem', | ||
| padding: '0.2rem 0.4rem', | ||
| paddingLeft: '0.6rem', | ||
| cursor: 'default', | ||
| }, | ||
| ...commonClasses, | ||
| } | ||
| : { | ||
| title: { | ||
| color: getTailwindConfigColor('primary-500'), | ||
| fontSize: '1.125rem', | ||
| fontWeight: 700, | ||
| }, | ||
| ...commonClasses, | ||
| } | ||
| const useStyles = () => makeStyles( | ||
| { | ||
| title: { | ||
| color: getTailwindConfigColor('primary-500'), | ||
| fontSize: '1.125rem', | ||
| fontWeight: 700, | ||
| }, | ||
| ...commonClasses, | ||
| }, | ||
| ) | ||
|
|
||
| const textFieldClasses = Object.freeze({ | ||
| container: 'textfield-container', | ||
| root: 'textfield-root', | ||
| input: 'textfield-input mb-0 text-sm focus:text-secondary-900', | ||
| }) | ||
|
|
||
| const EditableTitle = () => { | ||
| const userUpdate = useStoreActions((actions) => actions.userUpdate) | ||
| const isLoading = useStoreState((state) => state.isLoading) | ||
| const title = useStoreState((state) => state.title) | ||
| const mode = useStoreState((state) => state.ui.mode) | ||
|
|
||
| const classes = useStyles(mode) | ||
| const classes = useStyles() | ||
|
|
||
| const [editing, setEditing] = useState(false) | ||
| const [tentativeTitle, setTentativeTitle] = useState(title) | ||
|
|
||
| const textfieldRef = useRef(null) | ||
|
|
||
| useEffect(() => { | ||
| setTentativeTitle(title) | ||
| }, [title]) | ||
|
|
||
| const renderEditButton = ( | ||
| <CustomButton | ||
| horizontalMargin | ||
| className={classes.button} | ||
| type='secondary' | ||
| onClick={() => setEditing(true)} | ||
| endIcon={<Icons.Edit size="md" />} | ||
| /> | ||
| ) | ||
|
|
||
| const renderTitle = ( | ||
| <div className={classes.title} > | ||
| <div className={`edit-title-class ${classes.title}`} > | ||
| {isLoading ? '...' : title} | ||
| { | ||
| (mode === modes.QL || mode === modes.EDITOR) && | ||
| renderEditButton | ||
| } | ||
| </div > | ||
| ) | ||
|
|
||
| const renderTextfield = () => { | ||
| if (textfieldRef.current) { | ||
| let el = textfieldRef.current.childNodes[0][0] | ||
| const lengthSize = el.value.length > 12 ? 14 : 16 | ||
| el.style.width = `${getTextSize(el.value, 700, lengthSize, 'Open Sans').width}px` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kc-leung could you determine the font family from the 'el' component and use it instead of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we make the font family for the title Open Sans or PT Sans? sally's figma link is using Open Sans
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@kc-leung Sorry, just saw this. I don't think you should set it to any font family. I believe the Widget Studio will just use whatever font family is in use in the application is plugged in. The only place we setup the font family is for the storybook.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kc look how to determine the whole font of the element at the link I previously sent. Try use all the functions there, getCssStyle, getCanvasFont & modify getTextSize to receive only 2 params, text & font.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somehow, there are still some inconsistencies with the length calculated here, in WS, which I don't understand where they come from. This function works perfectly for my in chart-system.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @geoerika getting dynamically the font-size is not enough since some letters are smaller it is very inconsistent. I rather have it a bit larger than cutting off the title |
||
| } | ||
|
|
||
| return ( | ||
| <TextField | ||
| variant='borderless' | ||
| classes={textFieldClasses} | ||
| value={tentativeTitle} | ||
| onChange={(v) => { | ||
| setTentativeTitle(v) | ||
| }} | ||
| onBlur={(e) => { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
| e.nativeEvent.preventDefault() | ||
| e.nativeEvent.stopPropagation() | ||
| setEditing(false) | ||
| }} | ||
| onFocus={(e) => { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
| e.nativeEvent.preventDefault() | ||
| e.nativeEvent.stopPropagation() | ||
| setEditing(true) | ||
| }} | ||
| deleteButton={editing} | ||
| onDelete={(e) => { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
| e.nativeEvent.preventDefault() | ||
| e.nativeEvent.stopPropagation() | ||
| updateTitle('') | ||
| }} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| const updateTitle = title => userUpdate({ title }) | ||
|
|
||
| return ( | ||
| <div className={classes.outerContainer}> | ||
| <div | ||
| ref={textfieldRef} | ||
| className={classes.outerContainer} | ||
| > | ||
| { | ||
| editing | ||
| ? <form | ||
| action='.' | ||
| onSubmit={(e) => { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
| e.nativeEvent.preventDefault() | ||
| e.nativeEvent.stopPropagation() | ||
| updateTitle(e.target.children[0].children[0].value) | ||
| setEditing(false) | ||
| }} | ||
| > | ||
| <TextField | ||
| autoFocus | ||
| size='lg' | ||
| value={tentativeTitle} | ||
| onChange={(v) => setTentativeTitle(v)} | ||
| onSubmit={(e) => { | ||
| e.nativeEvent.preventDefault() | ||
| e.nativeEvent.stopPropagation() | ||
| }} | ||
| onBlur={(e) => { | ||
| updateTitle(e.target.value) | ||
| setTentativeTitle(title) | ||
| setEditing(false) | ||
| }} | ||
| /> | ||
| </form> | ||
| : <> | ||
| mode === modes.EDITOR | ||
| ? | ||
| <> | ||
| {renderTextfield()} | ||
| </> | ||
| : | ||
| <> | ||
| {renderTitle} | ||
| </> | ||
| } | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for empty line here, please.