redux-form
์ ํจ๊ป ์ฌ์ฉํ๊ณ onChange
"๋ณ๊ฒฝ" ์์
์ ์ ๋ฌํ๋ฉด AceEditor๊ฐ ๋ค์ ๋ ๋๋ง๋๊ณ ์ปค์๊ฐ ํธ์ง๊ธฐ ๋์ ์์นํฉ๋๋ค. ๋ณ๊ฒฝ์ ๋ฐฉ์งํฉ๋๋ค.
@venil7 redux ํ์์ผ๋ก ACE ํธ์ง๊ธฐ๋ฅผ ํตํฉํ ์ ์์์ต๋๊น?
๋๊ฐ์ ์ผ์ ํด์ผ ํฉ๋๋ค. ์ฝ๋ ์ํ์ ์ ๊ณตํด ์ฃผ์๊ฒ ์ต๋๊น?
import React from 'react'
import AceEditor from 'react-ace'
export default function ReduxAce (props) {
const {
input,
theme = 'github',
mode = 'html',
fontSize = 14,
tabSize = 2,
width = '1000px',
height = '500px',
...custom
} = props
return (
<AceEditor
theme={theme}
mode={mode}
fontSize={fontSize}
tabSize={tabSize}
width={width}
height={height}
editorProps={{$blockScrolling: true}}
{...input}
{...custom}
/>
)
}
๊ทธ๋ฐ ๋ค์ ์ฌ์ฉํ์ญ์์ค.
import React from 'react'
import { reduxForm } from 'redux-form'
import ReduxAce from './path/to/ReduxAce'
function MyForm (props) {
const { handleSubmit } = props
return (
<form onSubmit={handleSubmit}>
<Field
name='staticHtml'
component={ReduxAce}
/>
</form>
)
}
export default reduxForm({
form: 'edit-static-html',
onSubmit (values, dispatch, props) {
console.log('submit...')
}
})(MyForm)
๊ทธ๊ฒ๋งํผ ๊ฐ๋จํ๊ณ ํ๋ฅญํ๊ฒ ์๋ํฉ๋๋ค!
์ง๊ธ ๋น์ทํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
์์ ๊ฐ์ด Field์ ์ ๋ฌ๋๋ ๊ตฌ์ฑ ์์๋ก redux-form ๋ด๋ถ์์ react-ace๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
ํธ์ง๊ธฐ๊ฐ ์ด์ ์ ์์ ๋๋ง๋ค ๊ฐ์ด ""๋ก ์ค์ ๋๊ณ ์ ์ด ace ํธ์ง๊ธฐ ๋ด๋ถ์ ํ์๋ฉ๋๋ค.
๋ด ์ฝ๋๋ jschlieber๊ฐ ์ ์ํ ๊ฒ๊ณผ ๊ฐ์ต๋๋ค. ์ดํด๋ณด์ธ์.
`'react'์์ React ๊ฐ์ ธ์ค๊ธฐ
'react-ace'์์ AceEditor ๊ฐ์ ธ์ค๊ธฐ
import _ from 'lodash';
'์ค๊ดํธ/๋ชจ๋/์๋ฐ์คํฌ๋ฆฝํธ' ๊ฐ์ ธ์ค๊ธฐ;
'์ค๊ดํธ/ํ
๋ง/๋ชจ๋
ธ์นด์ด' ๊ฐ์ ธ์ค๊ธฐ;
๋ด๋ณด๋ด๊ธฐ ๊ธฐ๋ณธ ๊ธฐ๋ฅ ReduxAce(์ํ) {
console.log(props.input.onBlur, ์ํ);
const {
input,
theme = 'monokai',
mode = 'javascript',
fontSize = 14,
tabSize = 2,
width = '1000px',
height = '500px',
...custom
} = props;
return (
<AceEditor
theme={theme}
mode={mode}
fontSize={fontSize}
tabSize={tabSize}
width={width}
height={height}
onBlur={() => props.input.onBlur(props.input.value)}
editorProps={{$blockScrolling: Infinity}}
{...input}
{...custom}
/>
)
}`
๊ทธ๋ฆฌ๊ณ
`import React, { Component } from 'react';
'react-redux'์์ { ์ฐ๊ฒฐ } ๊ฐ์ ธ์ค๊ธฐ;
'../actions/index'์์ { saveDraft } ๊ฐ์ ธ์ค๊ธฐ;
'redux'์์ { bindActionCreators } ๊ฐ์ ธ์ค๊ธฐ;
import { ํ๋, reduxForm } from 'redux-form';
'../components/redux_ace'์์ ReduxAce ๊ฐ์ ธ์ค๊ธฐ
const ID = 'id123', ์ด๋ฆ = '์ด๋ฆ123', SOME_CODE = '์ฝ๋123';
ํจ์ mapStateToProps({์ด์}) {
๋ฐํ {
์ด์
};
}
ํจ์ mapDispatchToProps(๋์คํจ์น) {
๋ฐ์ธ๋์ก์
ํฌ๋ฆฌ์์ดํฐ({
์์ ๋ณด๊ดํจ์ ์ ์ฅ
}, ๋ณด๋ด๋ค)
}
ํด๋์ค ์ด์ ํ์ฅ ๊ตฌ์ฑ ์์ {
renderField(field) {
return (
<div className="form-group">
<label>{field.label}</label>
<input
className="form-control"
type="text"
{...field.input}
/>
</div>
)
}
onSubmit(values) {
console.log('sub',values);
this.props.saveDraft({
id: values[ID],
name: values[NAME],
code: values[SOME_CODE]
});
}
render() {
const { handleSubmit } = this.props;
return (
<form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
<Field
label="Id"
name={ID}
component={this.renderField}
/>
<Field
label="Name"
name={NAME}
component={this.renderField}
/>
<Field
label="Lambda code"
name={SOME_CODE}
component={ReduxAce}
/>
// if I put it here, then it suddenly works fine <ReduxAce/>
<button type="submit" className="btn btn-primary"> Submit</button>
</form>
);
}
}
๊ธฐ๋ณธ reduxForm({
ํ์ธ,
์์: 'DraftForm'
})(
์ฐ๊ฒฐ(mapStateToProps, mapDispatchToProps)(์ด์)
)`
๋ฒํผ ๋ฐ๋ก ์ง์ ์ ๋๊ธ์ ๋ฌ์์ต๋๋ค. " // ์ฌ๊ธฐ์ ๋ฃ์ผ๋ฉด ๊ฐ์๊ธฐ ์ ๋ฉ๋๋ค.
ํํธ๋ฅผ ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค!