Skip to content

Commit

Permalink
fix: Option component in plugin settings API (#627)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChFlick authored Nov 14, 2022
1 parent 01c28ba commit 27c3bb0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
15 changes: 13 additions & 2 deletions app/plugins/core/plugins/Preview/FormItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,26 @@ const components = {
option: Select,
}

function FormItem({ type, ...props }) {
function FormItem({
type, value, options, ...props
}) {
const Component = components[type] || Text

return <Component type={type} {...props} />
let actualValue = value
if (Component === Select) {
// when the value is a string, we need to find the option that matches it
if (typeof value === 'string' && options) {
actualValue = options.find((option) => option.value === value)
}
}

return <Component type={type} value={actualValue} options={options} {...props} />
}

FormItem.propTypes = {
value: PropTypes.any,
type: PropTypes.string.isRequired,
options: PropTypes.array
}

export default FormItem
13 changes: 9 additions & 4 deletions app/plugins/core/plugins/Preview/Settings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useState, useEffect } from 'react'
import PropTypes from 'prop-types'
import config from 'lib/config'
import FormItem from './FormItem'
Expand All @@ -7,13 +7,18 @@ import styles from './styles.module.css'
function Settings({ settings, name }) {
const [values, setValues] = useState(() => config.get('plugins')[name] || {})

const changeSetting = (label, value) => {
setValues((prev) => ({ ...prev, [label]: value }))

useEffect(() => {
config.set('plugins', {
...config.get('plugins'),
[name]: values,
})
}, [values])

const changeSetting = async (label, value) => {
setValues((prev) => ({
...prev,
[label]: value
}))
}

const renderSetting = (key) => {
Expand Down

0 comments on commit 27c3bb0

Please sign in to comment.