React-ace: redux-form๊ณผ ์ž˜ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2016๋…„ 06์›” 28์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: securingsincity/react-ace

redux-form ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ณ  onChange "๋ณ€๊ฒฝ" ์ž‘์—…์„ ์ „๋‹ฌํ•˜๋ฉด AceEditor๊ฐ€ ๋‹ค์‹œ ๋ Œ๋”๋ง๋˜๊ณ  ์ปค์„œ๊ฐ€ ํŽธ์ง‘๊ธฐ ๋์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  3 ๋Œ“๊ธ€

@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)(์ดˆ์•ˆ)
)`

๋ฒ„ํŠผ ๋ฐ”๋กœ ์ง์ „์— ๋Œ“๊ธ€์„ ๋‹ฌ์•˜์Šต๋‹ˆ๋‹ค. " // ์—ฌ๊ธฐ์— ๋„ฃ์œผ๋ฉด ๊ฐ‘์ž๊ธฐ ์ž˜ ๋ฉ๋‹ˆ๋‹ค. " - ๋„ค, Ace Editor๋ฅผ Field ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์•„๋‹Œ ๊ทธ๋Œ€๋กœ ๋„ฃ์œผ๋ฉด ๊ทธ ๊ฐ’์ด ์ง€์›Œ์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ณด์‹œ๋‹ค์‹œํ”ผ onBlur ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ๋ณด์กดํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.

ํžŒํŠธ๋ฅผ ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰