Skip to content

Commit 1fb53ce

Browse files
author
Yofriadi Yahya
committed
it works
1 parent 655c15a commit 1fb53ce

File tree

3 files changed

+55
-32
lines changed

3 files changed

+55
-32
lines changed

lowcoder-comp-stone-parcel-input/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lowcoder-comp-stone-parcel-input",
3-
"version": "0.0.3",
3+
"version": "0.0.5",
44
"type": "module",
55
"license": "MIT",
66
"dependencies": {

lowcoder-comp-stone-parcel-input/src/StoneParcelComp.tsx

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react";
1+
import { useEffect, useState } from "react";
22
import {
33
antd,
44
styleControl,
@@ -97,24 +97,59 @@ let StoneParcelComp = new UICompBuilder(
9797
{ ...initValue },
9898
]);
9999

100+
useEffect(() => {
101+
console.log(props.data.value);
102+
if (props.data.length) {
103+
setParcelValues(props.data.value);
104+
}
105+
}, [props.data, parcelValues]);
106+
107+
// convenient function, so that we don't need to pass index to child component
108+
const closureHandleChange =
109+
(i: number, parcelValue: StoneValue) => (k: string, v: string | number) =>
110+
handleChange(i, { ...parcelValue, [k]: v });
100111
const handleChange = (key: number, value: StoneValue) => {
101112
setParcelValues((parcelValues: StoneValue[]): StoneValue[] => {
102-
return parcelValues.map(
113+
const newParcelValues = parcelValues.map(
103114
(parcelValue: StoneValue, i: number): StoneValue => {
104115
if (i === key) {
105116
return value;
106117
}
107118
return parcelValue;
108119
},
109120
);
121+
props.data.onChange(newParcelValues);
122+
props.onEvent("change");
123+
return newParcelValues;
124+
});
125+
};
126+
127+
const handleAdd = () => {
128+
setParcelValues((parcelValues: StoneValue[]) => {
129+
const newParcelValues = [...parcelValues, initValue];
130+
props.data.onChange(newParcelValues);
131+
props.onEvent("change");
132+
return newParcelValues;
110133
});
111-
props.data.onChange(parcelValues);
112-
props.onEvent("change");
113134
};
114-
const handleClose = (i: number) => {
115-
setParcelValues(parcelValues.toSpliced(i, 1));
116-
props.data.onChange(parcelValues);
117-
props.onEvent("change");
135+
136+
// convenient function, so that we don't need to pass index to child component
137+
const closureHandleClose = (i: number) => () => handleClose(i);
138+
const handleClose = (key: number) => {
139+
setParcelValues((parcelValues: StoneValue[]): StoneValue[] => {
140+
const newParcelValues = parcelValues.reduce(
141+
(acc: StoneValue[], curr: StoneValue, i: number): StoneValue[] => {
142+
if (i !== key) {
143+
acc.push(curr);
144+
}
145+
return acc;
146+
},
147+
[],
148+
);
149+
props.data.onChange(newParcelValues);
150+
props.onEvent("change");
151+
return newParcelValues;
152+
});
118153
};
119154

120155
const [dimensions, setDimensions] = useState({ width: 480, height: 280 });
@@ -141,17 +176,15 @@ let StoneParcelComp = new UICompBuilder(
141176
{parcelValues.map((parcelValue, i) => (
142177
<StoneParcel
143178
key={i}
144-
i={i}
145-
parcelValue={parcelValue}
146-
handleChange={handleChange}
147-
handleClose={handleClose}
179+
handleChange={closureHandleChange(i, parcelValue)}
180+
handleClose={closureHandleClose(i)}
148181
/>
149182
))}
150183
<antd.Button
151184
type="primary"
152185
shape="circle"
153186
icon={<PlusOutlined />}
154-
onClick={() => setParcelValues([...parcelValues, initValue])}
187+
onClick={handleAdd}
155188
></antd.Button>
156189
</Container>
157190
);

lowcoder-comp-stone-parcel-input/src/vendors/StoneParcel.tsx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ interface Option {
99
}
1010

1111
function StoneParcel({
12-
i,
13-
parcelValue,
1412
handleChange,
1513
handleClose,
1614
}: {
17-
i: number;
18-
parcelValue: StoneValue;
19-
handleChange: (i: number, parcelValue: StoneValue) => void;
20-
handleClose: (i: number) => void;
15+
handleChange: (k: string, v: string | number) => void;
16+
handleClose: () => void;
2117
}) {
2218
return (
2319
<Row>
@@ -37,9 +33,7 @@ function StoneParcel({
3733
.toLowerCase()
3834
.localeCompare((optB?.label ?? "").toLowerCase())
3935
}
40-
onChange={(v: string) =>
41-
handleChange(i, { ...parcelValue, parcel: v })
42-
}
36+
onChange={(v: string) => handleChange("parcel", v)}
4337
options={[
4438
{
4539
value: "Not Identified",
@@ -74,10 +68,8 @@ function StoneParcel({
7468
style={{ margin: 3 }}
7569
min={1}
7670
max={10}
77-
defaultValue={parcelValue.pieces}
78-
onChange={(v: number) =>
79-
handleChange(i, { ...parcelValue, pieces: v })
80-
}
71+
defaultValue={0}
72+
onChange={(v: number) => handleChange("pieces", v)}
8173
/>
8274
</Wrapper>
8375
<Wrapper>
@@ -86,17 +78,15 @@ function StoneParcel({
8678
style={{ margin: 3 }}
8779
min={1}
8880
max={10}
89-
defaultValue={parcelValue.carat}
90-
onChange={(v: number) =>
91-
handleChange(i, { ...parcelValue, carat: v })
92-
}
81+
defaultValue={0}
82+
onChange={(v: number) => handleChange("carat", v)}
9383
/>
9484
</Wrapper>
9585
<antd.Button
9686
type="primary"
9787
shape="circle"
9888
icon={<CloseOutlined />}
99-
onClick={() => handleClose(i)}
89+
onClick={() => handleClose()}
10090
></antd.Button>
10191
</Row>
10292
);

0 commit comments

Comments
 (0)