Skip to content

Commit

Permalink
feat: support change fieldKey
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyi.hzy committed Jan 7, 2020
1 parent 700e3fe commit ffc8f6a
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packages/react-schema-editor/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export const SchemaEditor: React.FC<{
const [componentType, setComponentType] = useState('antd')
const [selectedPath, setSelectedPath] = React.useState(null)

const selectedPaths = (selectedPath && selectedPath.split('.')) || []
const fieldKey =
selectedPaths.length > 0 && selectedPaths[selectedPaths.length - 1]

const handleTypeChange = e => {
setComponentType(e.target.value)
}
Expand All @@ -30,6 +34,7 @@ export const SchemaEditor: React.FC<{
const selectedSchema =
selectedPath &&
(selectedPath === 'root' ? schema : fp.get(selectedPath, schema))

return (
<div className="schema-editor">
<div className="schema-menus">
Expand Down Expand Up @@ -58,11 +63,24 @@ export const SchemaEditor: React.FC<{
components={
componentType === 'fusion' ? nextComponents : antdComponents
}
fieldKey="fieldC"
onFieldKeyChange={value => {}}
fieldKey={fieldKey}
onFieldKeyChange={value => {
const newSchema = _.cloneDeep(schema)
// 新增 key
const selectedPathPrev = selectedPaths
.slice(0, selectedPaths.length - 1)
.join('.')
const newSelectPath = selectedPathPrev + '.' + value
_.set(newSchema, newSelectPath, _.cloneDeep(selectedSchema))
// 移除旧 key
_.unset(newSchema, selectedPath)

onChange(newSchema)
setSelectedPath(newSelectPath)
}}
schema={selectedSchema}
onChange={value => {
const newSchema = _.clone(schema)
const newSchema = _.cloneDeep(schema)
_.set(newSchema, selectedPath, value)
onChange(newSchema)
}}
Expand Down

0 comments on commit ffc8f6a

Please sign in to comment.