Skip to content

Use EditableInput component in Toolbar #2238

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

Merged
merged 7 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export const PROJECT_SAVE_SUCCESS = 'PROJECT_SAVE_SUCCESS';
export const PROJECT_SAVE_FAIL = 'PROJECT_SAVE_FAIL';
export const NEW_PROJECT = 'NEW_PROJECT';
export const RESET_PROJECT = 'RESET_PROJECT';
export const SHOW_EDIT_PROJECT_NAME = 'SHOW_EDIT_PROJECT_NAME';
export const HIDE_EDIT_PROJECT_NAME = 'HIDE_EDIT_PROJECT_NAME';

export const SET_PROJECT = 'SET_PROJECT';
export const SET_PROJECTS = 'SET_PROJECTS';
Expand Down
12 changes: 0 additions & 12 deletions client/modules/IDE/actions/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,6 @@ export function cloneProject(project) {
};
}

export function showEditProjectName() {
return {
type: ActionTypes.SHOW_EDIT_PROJECT_NAME
};
}

export function hideEditProjectName() {
return {
type: ActionTypes.HIDE_EDIT_PROJECT_NAME
};
}

export function setProjectSavedTime(updatedAt) {
return {
type: ActionTypes.SET_PROJECT_SAVED_TIME,
Expand Down
36 changes: 28 additions & 8 deletions client/modules/IDE/components/EditableInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,36 @@ function EditableInput({
emptyPlaceholder,
InputComponent,
inputProps,
onChange
onChange,
disabled,
'aria-label': ariaLabel
}) {
const [isEditing, setIsEditing] = React.useState(false);
const [currentValue, setCurrentValue] = React.useState(value || '');
const displayValue = currentValue || emptyPlaceholder;
const hasValue = currentValue !== '';
const classes = `editable-input editable-input--${
isEditing ? 'is-editing' : 'is-not-editing'
} editable-input--${hasValue ? 'has-value' : 'has-placeholder'}`;
const inputRef = React.createRef();
} editable-input--${hasValue ? 'has-value' : 'has-placeholder'} ${
disabled ? 'editable-input--disabled' : ''
}`;
const inputRef = React.useRef();
const { t } = useTranslation();
React.useEffect(() => {
if (isEditing) {
inputRef.current.focus();
inputRef.current?.focus();
}
}, [isEditing]);

function beginEditing() {
setIsEditing(true);
}

function cancelEditing() {
setIsEditing(false);
setCurrentValue(value);
}

function doneEditing() {
setIsEditing(false);

Expand All @@ -51,6 +60,8 @@ function EditableInput({
function checkForKeyAction(event) {
if (event.key === 'Enter') {
doneEditing();
} else if (event.key === 'Escape' || event.key === 'Esc') {
cancelEditing();
}
}

Expand All @@ -59,7 +70,11 @@ function EditableInput({
<button
className="editable-input__label"
onClick={beginEditing}
aria-label={t('EditableInput.EditValue', { display: displayValue })}
aria-label={
ariaLabel ?? t('EditableInput.EditValue', { display: displayValue })
}
aria-hidden={isEditing}
disabled={disabled}
>
<span>{displayValue}</span>
<EditIcon
Expand All @@ -74,9 +89,10 @@ function EditableInput({
type="text"
{...inputProps}
disabled={!isEditing}
aria-hidden={!isEditing}
onBlur={doneEditing}
onChange={updateValue}
onKeyPress={checkForKeyAction}
onKeyDown={checkForKeyAction}
ref={inputRef}
value={currentValue}
/>
Expand All @@ -89,7 +105,9 @@ EditableInput.defaultProps = {
InputComponent: 'input',
inputProps: {},
validate: () => true,
value: ''
value: '',
disabled: false,
'aria-label': undefined
};

EditableInput.propTypes = {
Expand All @@ -99,7 +117,9 @@ EditableInput.propTypes = {
inputProps: PropTypes.object, // eslint-disable-line
onChange: PropTypes.func.isRequired,
validate: PropTypes.func,
value: PropTypes.string
value: PropTypes.string,
disabled: PropTypes.bool,
'aria-label': PropTypes.string
};

export default EditableInput;
28 changes: 28 additions & 0 deletions client/modules/IDE/components/Header/ProjectName.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { useSketchActions } from '../../hooks';
import { selectProjectName } from '../../selectors/project';
import EditableInput from '../EditableInput';

export default function ProjectName() {
const { t } = useTranslation();

const { changeSketchName, canEditProjectName } = useSketchActions();

const projectName = useSelector(selectProjectName);

return (
<EditableInput
value={projectName}
disabled={!canEditProjectName}
aria-label={t('Toolbar.EditSketchARIA')}
inputProps={{
maxLength: 128,
'aria-label': t('Toolbar.NewSketchNameARIA')
}}
validate={(text) => text.trim().length > 0}
onChange={changeSketchName}
/>
);
}
71 changes: 4 additions & 67 deletions client/modules/IDE/components/Header/Toolbar.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import React, { useRef, useState } from 'react';
import React from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import { Link } from 'react-router-dom';
import {
hideEditProjectName,
showEditProjectName
} from '../../actions/project';
import {
openPreferences,
startAccessibleSketch,
Expand All @@ -19,12 +15,11 @@ import {
setGridOutput,
setTextOutput
} from '../../actions/preferences';
import { useSketchActions } from '../../hooks';

import PlayIcon from '../../../../images/play.svg';
import StopIcon from '../../../../images/stop.svg';
import PreferencesIcon from '../../../../images/preferences.svg';
import EditProjectNameIcon from '../../../../images/pencil.svg';
import ProjectName from './ProjectName';

const Toolbar = (props) => {
const { isPlaying, infiniteLoop, preferencesIsVisible } = useSelector(
Expand All @@ -35,34 +30,6 @@ const Toolbar = (props) => {
const dispatch = useDispatch();

const { t } = useTranslation();
const { changeSketchName, canEditProjectName } = useSketchActions();

const projectNameInputRef = useRef();
const [nameInputValue, setNameInputValue] = useState(project.name);

function handleKeyPress(event) {
if (event.key === 'Enter') {
dispatch(hideEditProjectName());
projectNameInputRef.current?.blur();
}
}

function handleProjectNameClick() {
if (canEditProjectName) {
dispatch(showEditProjectName());
setTimeout(() => {
projectNameInputRef.current?.focus();
}, 140);
}
}

function handleProjectNameSave() {
const newName = nameInputValue;
if (newName.length > 0) {
dispatch(hideEditProjectName());
changeSketchName(newName);
}
}

const playButtonClass = classNames({
'toolbar__play-button': true,
Expand All @@ -76,10 +43,6 @@ const Toolbar = (props) => {
'toolbar__preferences-button': true,
'toolbar__preferences-button--selected': preferencesIsVisible
});
const nameContainerClass = classNames({
'toolbar__project-name-container': true,
'toolbar__project-name-container--editing': project.isEditingName
});

return (
<div className="toolbar">
Expand Down Expand Up @@ -130,34 +93,8 @@ const Toolbar = (props) => {
{t('Toolbar.Auto-refresh')}
</label>
</div>
<div className={nameContainerClass}>
<button
className="toolbar__project-name"
onClick={handleProjectNameClick}
disabled={!canEditProjectName}
aria-label={t('Toolbar.EditSketchARIA')}
>
<span>{project.name}</span>
{canEditProjectName && (
<EditProjectNameIcon
className="toolbar__edit-name-button"
focusable="false"
aria-hidden="true"
/>
)}
</button>
<input
type="text"
maxLength="128"
className="toolbar__project-name-input"
aria-label={t('Toolbar.NewSketchNameARIA')}
disabled={!canEditProjectName}
ref={projectNameInputRef}
value={nameInputValue}
onChange={(e) => setNameInputValue(e.target.value)}
onBlur={handleProjectNameSave}
onKeyPress={handleKeyPress}
/>
<div className="toolbar__project-name-container">
<ProjectName />
{(() => {
if (project.owner) {
return (
Expand Down
4 changes: 0 additions & 4 deletions client/modules/IDE/reducers/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ const project = (state, action) => {
};
case ActionTypes.RESET_PROJECT:
return initialState();
case ActionTypes.SHOW_EDIT_PROJECT_NAME:
return Object.assign({}, state, { isEditingName: true });
case ActionTypes.HIDE_EDIT_PROJECT_NAME:
return Object.assign({}, state, { isEditingName: false });
case ActionTypes.SET_PROJECT_SAVED_TIME:
return Object.assign({}, state, { updatedAt: action.value });
case ActionTypes.START_SAVING_PROJECT:
Expand Down
8 changes: 8 additions & 0 deletions client/styles/components/_editable-input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

button.editable-input__label {
display: flex;
align-items: center;

@include themify() {
color: getThemifyVariable('inactive-text-color');
Expand Down Expand Up @@ -35,6 +36,13 @@ button.editable-input__label {
height: 1.5rem;
}

.editable-input--disabled {
pointer-events: none;
.editable-input__icon {
display: none;
}
}

.editable-input--is-not-editing .editable-input__input,
.editable-input--is-editing .editable-input__label {
display: none;
Expand Down
39 changes: 5 additions & 34 deletions client/styles/components/_toolbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -98,40 +98,23 @@
}

.toolbar__project-name-container {
@include themify() {
border-color: getThemifyVariable('inactive-text-color');
}
margin-left: #{10 / $base-font-size}rem;
padding-left: #{10 / $base-font-size}rem;
display: flex;
align-items: center;
}

.toolbar__project-name {
.toolbar .editable-input__label {
@include themify() {
color: getThemifyVariable('secondary-text-color');
&:hover {
color: getThemifyVariable('logo-color');
& .toolbar__edit-name-button path {
fill: getThemifyVariable('logo-color');
}
& path {
fill: getThemifyVariable('secondary-text-color');
}
}
cursor: pointer;
display: flex;
align-items: center;

.toolbar__project-name-container--editing & {
display: none;
}
}

.toolbar__project-name-input {
display: none;
border: 0px;
.toolbar__project-name-container--editing & {
display: block;
}
.toolbar .editable-input__input {
border: 0;
}

.toolbar__project-owner {
Expand Down Expand Up @@ -160,15 +143,3 @@
color:getThemifyVariable('logo-color');
}
}

.toolbar__edit-name-button {
display: inline-block;
vertical-align: top;
width: #{18 / $base-font-size}rem;
height: #{18 / $base-font-size}rem;
@include themify() {
& path {
fill: getThemifyVariable('secondary-text-color');
}
}
}