-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(plasma-react): value prop will no longer display syntax error
- Loading branch information
Mike DiDomizio
authored
May 13, 2022
1 parent
fc09c53
commit ccb15d8
Showing
5 changed files
with
83 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 27 additions & 15 deletions
42
packages/react/src/components/editor/JSONEditorConnected.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,34 @@ | ||
import {connect} from 'react-redux'; | ||
import {useDispatch, useSelector} from 'react-redux'; | ||
import {useEffect} from 'react'; | ||
|
||
import {PlasmaState} from '../../PlasmaState'; | ||
import {IDispatch} from '../../utils'; | ||
import {JSONEditor, JSONEditorDispatchProps, JSONEditorProps, JSONEditorStateProps} from './JSONEditor'; | ||
import {JSONEditor, JSONEditorProps} from './JSONEditor'; | ||
import {JSONEditorActions} from './JSONEditorActions'; | ||
import {JSONEditorSelectors} from './JSONEditorSelectors'; | ||
|
||
const mapStateToProps = (state: PlasmaState, ownProps: JSONEditorProps): JSONEditorStateProps => ({ | ||
value: JSONEditorSelectors.getValue(state, ownProps.id), | ||
}); | ||
interface JSONEditorConnectedProps { | ||
/** | ||
* initial value of the component | ||
*/ | ||
defaultValue?: string; | ||
} | ||
|
||
const mapDispatchToProps = (dispatch: IDispatch, ownProps: JSONEditorProps): JSONEditorDispatchProps => ({ | ||
onMount: () => dispatch(JSONEditorActions.addJSONEditor(ownProps.id, ownProps.defaultValue ?? ownProps.value)), | ||
onUnmount: () => dispatch(JSONEditorActions.removeJSONEditor(ownProps.id)), | ||
onChange: (value: string, inError: boolean) => { | ||
dispatch(JSONEditorActions.updateJSONEditorValue(ownProps.id, value)); | ||
ownProps.onChange?.(value, inError); | ||
}, | ||
}); | ||
export const JSONEditorConnected = (props: JSONEditorProps & JSONEditorConnectedProps) => { | ||
const dispatch: IDispatch = useDispatch(); | ||
const value = useSelector((state) => JSONEditorSelectors.getValue(state, props.id)); | ||
|
||
export const JSONEditorConnected = connect(mapStateToProps, mapDispatchToProps)(JSONEditor); | ||
useEffect(() => { | ||
dispatch(JSONEditorActions.addJSONEditor(props.id, props.defaultValue || props.value)); | ||
|
||
return () => { | ||
dispatch(JSONEditorActions.removeJSONEditor(props.id)); | ||
}; | ||
}, []); | ||
|
||
const onChange = (changedValue: string, inError: boolean) => { | ||
dispatch(JSONEditorActions.updateJSONEditorValue(props.id, changedValue)); | ||
props.onChange?.(changedValue, inError); | ||
}; | ||
|
||
return <JSONEditor {...props} value={value} onChange={onChange} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters