Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit c3ca608

Browse files
Port image component to formik
1 parent 0da0633 commit c3ca608

File tree

1 file changed

+26
-63
lines changed
  • packages/builder/src/components/ComponentOptions/components/Content/Page/components/ItemOptions/components

1 file changed

+26
-63
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
1-
import React, { createRef, useContext } from 'react'
1+
import React, { useContext } from 'react'
22
import PropTypes from 'prop-types'
33

4-
import { Control } from 'react-redux-form'
4+
import { Field } from 'formik'
55
import {
6-
Row, Col, Collapse, Input, Button,
6+
Row, Col, Collapse,
77
InputGroup, InputGroupAddon, InputGroupText
88
} from 'reactstrap'
99

1010
import Icon from '../../../../../../../Icon'
11-
import FileSelector from '../../../../../../../FileSelector'
11+
import FileSelectorField from '../../../../../../../FileSelector/field'
1212

1313
import { ItemContext } from '../../../index'
1414

15-
export const ImageOptions = ({ rowIndex, ...props }) => {
15+
export const ImageOptions = ({ name, index }) => {
1616
const { openItem } = useContext(ItemContext)
1717

1818
return (
1919
<Row form>
2020
<Col>
21-
<Collapse isOpen={ openItem === rowIndex }>
21+
<Collapse isOpen={ openItem === index }>
2222
<InputGroup className="mt-2">
2323
<InputGroupAddon addonType="prepend">
2424
<InputGroupText>
2525
<Icon icon="arrows-h" fixedWidth />
2626
</InputGroupText>
2727
</InputGroupAddon>
28-
<Control
29-
model=".width"
28+
<Field
29+
name={ `${ name }.width` }
3030
defaultValue=""
3131
placeholder="width"
32-
debounce={ 300 }
33-
className="form-control"
34-
style={{ fontFamily: 'Fira Mono' }}
32+
className="form-control text-monospace"
3533
/>
3634
<InputGroupAddon addonType="prepend">
3735
<InputGroupText>
3836
<Icon icon="arrows-v" fixedWidth />
3937
</InputGroupText>
4038
</InputGroupAddon>
41-
<Control
42-
model=".height"
39+
<Field
40+
name={ `${ name }.height` }
4341
defaultValue=""
4442
placeholder="height"
45-
debounce={ 300 }
46-
className="form-control"
47-
style={{ fontFamily: 'Fira Mono' }}
43+
className="form-control text-monospace"
4844
/>
4945
</InputGroup>
5046
</Collapse>
@@ -53,60 +49,27 @@ export const ImageOptions = ({ rowIndex, ...props }) => {
5349
)
5450
}
5551

56-
const ImageSelector = ({ rowIndex }, { id, gridDispatch }) => {
57-
const fileSelector = createRef()
58-
59-
return (
60-
<Row form>
61-
<Col>
62-
<FileSelector
63-
accept="image/*"
64-
component={ id }
65-
ref={ fileSelector }
66-
/>
67-
<InputGroup>
68-
<Control
69-
model=".src"
70-
placeholder="source"
71-
component={ Input }
72-
style={{ fontFamily: 'Fira Mono' }}
73-
debounce={ 300 }
74-
/>
75-
<InputGroupAddon addonType="append">
76-
<Button
77-
outline color="secondary"
78-
style={{ minWidth: '3rem' }}
79-
onClick={ async () => {
80-
try {
81-
const files = await fileSelector.current.select()
82-
gridDispatch('change', {
83-
model: `local.items.rows[${ rowIndex }][0].src`,
84-
value: `\${ this.files["${ files[0].localPath }"] }`
85-
})
86-
} catch (error) {
87-
console.log('Error while selecting image', error)
88-
}
89-
} }
90-
>
91-
<Icon fixedWidth icon="folder" />
92-
</Button>
93-
</InputGroupAddon>
94-
</InputGroup>
95-
</Col>
96-
</Row>
97-
)
98-
}
52+
const ImageSelector = ({ name }, { id }) =>
53+
<Row form>
54+
<Col>
55+
<FileSelectorField
56+
name={ `${ name }.src`}
57+
accept="image/*"
58+
placeholder="source"
59+
component={ id }
60+
/>
61+
</Col>
62+
</Row>
9963

10064
ImageSelector.contextTypes = {
10165
id: PropTypes.oneOfType([
10266
PropTypes.string,
10367
PropTypes.number,
10468
]),
105-
gridDispatch: PropTypes.func,
10669
}
10770

108-
export default ({ rowIndex }) =>
71+
export default ({ name, index }) =>
10972
<>
110-
<ImageSelector rowIndex={ rowIndex } />
111-
<ImageOptions rowIndex={ rowIndex } />
73+
<ImageSelector name={ name } />
74+
<ImageOptions name={ name } index={ index } />
11275
</>

0 commit comments

Comments
 (0)