Skip to content

Commit 31ef9bc

Browse files
committed
render function to return a custom JSX element based on value
1 parent cdcaec8 commit 31ef9bc

File tree

3 files changed

+92
-65
lines changed

3 files changed

+92
-65
lines changed

example/index.tsx

+86-62
Original file line numberDiff line numberDiff line change
@@ -36,68 +36,92 @@ const App = () => {
3636
}}
3737
/>
3838

39-
<InlineEdit
40-
value={value}
41-
validate={validate}
42-
onChange={onChange}
43-
viewClass="styled"
44-
allowEditWhileLoading
45-
optimisticUpdate={true}
46-
/>
47-
<InlineEdit
48-
value="disabled"
49-
validate={validate}
50-
onChange={onChange}
51-
isDisabled
52-
/>
53-
<InlineEdit
54-
value={value}
55-
validate={validate}
56-
onChange={onChange}
57-
invalidClass="invalid"
58-
loadingClass="loading"
59-
savedClass="saved"
60-
errorClass="error"
61-
errorDuration={1200}
62-
savedDuration={1200}
63-
/>
64-
<InlineEdit
65-
type={InputType.Number}
66-
value="34"
67-
validate={i => parseInt(i, 10) > 0}
68-
onChange={onChange}
69-
editProps={{ min: 10, max: 20, step: 2 }}
70-
format={value => '€ ' + value}
71-
loadingClass="loading"
72-
saveTimeout={1200}
73-
/>
74-
<InlineEdit
75-
type={InputType.Date}
76-
value="2020-03-12"
77-
onChange={onChange}
78-
/>
79-
<InlineEdit
80-
type={InputType.Range}
81-
value="6"
82-
validate={i => parseInt(i, 10) > 3}
83-
onChange={onChange}
84-
editProps={{ min: 0, max: 10, step: 1 }}
85-
/>
86-
<InlineEdit
87-
type={InputType.TextArea}
88-
value="pizza patatine"
89-
onChange={onChange}
90-
editProps={{ rows: 4 }}
91-
format={v => v.toUpperCase()}
92-
/>
93-
<InlineEdit
94-
type={InputType.Select}
95-
value="2"
96-
onChange={onChange}
97-
options={options}
98-
valueKey="id"
99-
labelKey="name"
100-
/>
39+
<div>
40+
<InlineEdit
41+
value={value}
42+
validate={validate}
43+
onChange={onChange}
44+
viewClass="styled"
45+
allowEditWhileLoading
46+
optimisticUpdate={true}
47+
/>
48+
</div>
49+
50+
<div>
51+
<InlineEdit
52+
value="disabled"
53+
validate={validate}
54+
onChange={onChange}
55+
isDisabled
56+
/>
57+
</div>
58+
59+
<div>
60+
<InlineEdit
61+
value={value}
62+
validate={validate}
63+
onChange={onChange}
64+
invalidClass="invalid"
65+
loadingClass="loading"
66+
savedClass="saved"
67+
errorClass="error"
68+
errorDuration={1200}
69+
savedDuration={1200}
70+
render={v => <code>{v}<sup>2</sup></code>}
71+
/>
72+
</div>
73+
74+
<div>
75+
<InlineEdit
76+
type={InputType.Number}
77+
value="34"
78+
validate={i => parseInt(i, 10) > 0}
79+
onChange={onChange}
80+
editProps={{ min: 10, max: 20, step: 2 }}
81+
format={value => '€ ' + value}
82+
loadingClass="loading"
83+
saveTimeout={1200}
84+
/>
85+
</div>
86+
87+
<div>
88+
<InlineEdit
89+
type={InputType.Date}
90+
value="2020-03-12"
91+
onChange={onChange}
92+
/>
93+
</div>
94+
95+
<div>
96+
<InlineEdit
97+
type={InputType.Range}
98+
value="6"
99+
validate={i => parseInt(i, 10) > 3}
100+
onChange={onChange}
101+
editProps={{ min: 0, max: 10, step: 1 }}
102+
/>
103+
</div>
104+
105+
<div>
106+
<InlineEdit
107+
type={InputType.TextArea}
108+
value="pizza patatine"
109+
onChange={onChange}
110+
editProps={{ rows: 4 }}
111+
format={v => v.toUpperCase()}
112+
/>
113+
</div>
114+
115+
<div>
116+
<InlineEdit
117+
type={InputType.Select}
118+
value="2"
119+
onChange={onChange}
120+
options={options}
121+
valueKey="id"
122+
labelKey="name"
123+
/>
124+
</div>
101125
</div>
102126
)
103127
}

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "riec",
3-
"version": "0.1.5",
3+
"version": "0.1.7",
44
"author": "Matteo Frana <[email protected]>",
55
"description": "Modern React component for inline edit of text/select values, written in Typescript",
66
"keywords": [
77
"react inline edit",
88
"react edit",
99
"react",
1010
"inline",
11-
"edit"
11+
"edit",
12+
"riek"
1213
],
1314
"license": "MIT",
1415
"repository": {

src/InlineEdit.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface InlineEditProps {
1010
onChange: (value: string) => void
1111
type?: InputType
1212
format?: (value: string) => string
13+
render?: (value: string) => React.ReactElement
1314
validate?: (value: string) => boolean
1415
isDisabled?: boolean
1516
allowEditWhileLoading?: boolean
@@ -38,6 +39,7 @@ const InlineEdit: React.FC<InlineEditProps> = ({
3839
onChange,
3940
type = InputType.Text,
4041
format,
42+
render,
4143
validate,
4244
isDisabled = false,
4345
allowEditWhileLoading = false,
@@ -198,7 +200,7 @@ const InlineEdit: React.FC<InlineEditProps> = ({
198200
onFocus={() => send('FOCUS')}
199201
tabIndex={0}
200202
>
201-
{viewValue}
203+
{render ? render(viewValue) : viewValue}
202204
</span>
203205
)}
204206
{current.value === 'edit' && (

0 commit comments

Comments
 (0)