From 47c30c1b7f75ce2f966e4a0193f0d9380a260ea9 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Sun, 14 Apr 2019 14:59:58 -0700 Subject: [PATCH] fix up custom theme name input --- components/Editor.js | 4 ++-- components/Themes/ThemeCreate.js | 18 ++++++++++-------- components/Themes/index.js | 16 ++++++++++------ 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/components/Editor.js b/components/Editor.js index 4720661..7320f22 100644 --- a/components/Editor.js +++ b/components/Editor.js @@ -391,7 +391,7 @@ Editor.defaultProps = { onReset: () => {} } -function ThemesContainer(props) { +function EditorContainer(props) { const [themes, updateThemes] = React.useState(THEMES) React.useEffect(() => { @@ -408,4 +408,4 @@ function ThemesContainer(props) { return } -export default ThemesContainer +export default EditorContainer diff --git a/components/Themes/ThemeCreate.js b/components/Themes/ThemeCreate.js index ff8a2df..e340e5c 100644 --- a/components/Themes/ThemeCreate.js +++ b/components/Themes/ThemeCreate.js @@ -47,16 +47,18 @@ const HighlightPicker = ({ title, onChange, color }) => ( ) -const getCustomName = themes => - `Custom Theme ${themes.filter(({ name }) => name.startsWith('Custom Theme')).length + 1}` - -const ThemeCreate = ({ theme, themes, highlights, create, updateHighlights }) => { +const ThemeCreate = ({ + theme, + themes, + highlights, + create, + updateHighlights, + name, + onInputChange +}) => { const [preset, updatePreset] = React.useState(theme.id) const [highlight, selectHighlight] = React.useState() - // TODO consider using an uncontrolled input - const [name, updateName] = React.useState(getCustomName(themes)) - return (
@@ -67,7 +69,7 @@ const ThemeCreate = ({ theme, themes, highlights, create, updateHighlights }) => name="name" placeholder="Custom Theme" value={name} - onChange={({ target: { value } }) => updateName(value)} + onChange={onInputChange} maxLength="32" />
diff --git a/components/Themes/index.js b/components/Themes/index.js index 87d590c..6fc2044 100644 --- a/components/Themes/index.js +++ b/components/Themes/index.js @@ -48,13 +48,16 @@ const ThemeItem = ({ children, item, isSelected, remove }) => ( const themeIcon = +const getCustomName = themes => + `Custom Theme ${themes.filter(({ name }) => name.startsWith('Custom Theme')).length + 1}` + class Themes extends React.PureComponent { state = { - highlights: {} + highlights: {}, + name: '' } componentDidMount() { - // TODO consider just using withRouter directly const { queryState } = getRouteState(this.props.router) if (queryState.highlights) { @@ -66,11 +69,11 @@ class Themes extends React.PureComponent { static getDerivedStateFromProps(props) { if (!props.isVisible) { - // TODO use shared function for this next line const themeConfig = (props.themes && props.themes.find(t => t.id === props.theme)) || DEFAULT_THEME return { - highlights: themeConfig.highlights + highlights: themeConfig.highlights, + name: getCustomName(props.themes) } } return null @@ -107,7 +110,7 @@ class Themes extends React.PureComponent { const themeConfig = themes.find(t => t.id === theme) || DEFAULT_THEME - const dropdownValue = isVisible ? { name: '' } : { id: themeConfig.id, name: themeConfig.name } + const dropdownValue = isVisible ? { name: this.state.name } : themeConfig const dropdownList = [ { @@ -123,7 +126,6 @@ class Themes extends React.PureComponent { innerRef={this.dropdown} icon={themeIcon} disableInput={isVisible} - inputValue={dropdownValue} selected={dropdownValue} list={dropdownList} itemWrapper={this.itemWrapper} @@ -137,6 +139,8 @@ class Themes extends React.PureComponent { highlights={highlights} create={this.create} updateHighlights={this.updateHighlights} + name={this.state.name} + onInputChange={e => this.setState({ name: e.target.value })} /> )}