@kaspernj/api-maker 1.0.430 → 1.0.432
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/super-admin/edit-page/edit-attribute-checkbox.jsx +80 -0
- package/src/super-admin/edit-page/edit-attribute-content.jsx +10 -3
- package/src/super-admin/edit-page/edit-attribute-input.jsx +1 -1
- package/src/super-admin/edit-page/edit-attribute.jsx +53 -29
- package/src/utils/checkbox.jsx +18 -8
package/package.json
CHANGED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {memo, useMemo} from "react"
|
|
2
|
+
import {StyleSheet, View} from "react-native"
|
|
3
|
+
import Checkbox from "../../utils/checkbox"
|
|
4
|
+
import BaseComponent from "../../base-component"
|
|
5
|
+
import PropTypes from "prop-types"
|
|
6
|
+
import propTypesExact from "prop-types-exact"
|
|
7
|
+
import {shapeComponent} from "set-state-compare/src/shape-component.js"
|
|
8
|
+
import {useForm} from "../../form"
|
|
9
|
+
|
|
10
|
+
const styles = StyleSheet.create({
|
|
11
|
+
checkbox: {
|
|
12
|
+
marginRight: 4
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
export default memo(shapeComponent(class EditAttributeInput extends BaseComponent {
|
|
17
|
+
static propTypes = propTypesExact({
|
|
18
|
+
attributeName: PropTypes.string.isRequired,
|
|
19
|
+
id: PropTypes.string.isRequired,
|
|
20
|
+
label: PropTypes.string.isRequired,
|
|
21
|
+
model: PropTypes.object.isRequired,
|
|
22
|
+
name: PropTypes.string.isRequired
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
setup() {
|
|
26
|
+
const {attributeName, id, name} = this.p
|
|
27
|
+
|
|
28
|
+
this.form = useForm()
|
|
29
|
+
this.useStates({
|
|
30
|
+
checked: this.tt.defaultChecked
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
this.dataSet = useMemo(
|
|
34
|
+
() => ({
|
|
35
|
+
attribute: attributeName,
|
|
36
|
+
id,
|
|
37
|
+
name
|
|
38
|
+
}),
|
|
39
|
+
[attributeName, id, name]
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
useMemo(() => {
|
|
43
|
+
if (this.form) {
|
|
44
|
+
this.form.setValue(name, this.s.checked)
|
|
45
|
+
}
|
|
46
|
+
}, [])
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
defaultChecked = () => this.p.model[this.p.attributeName]() || false
|
|
50
|
+
|
|
51
|
+
render() {
|
|
52
|
+
const {dataSet} = this.tt
|
|
53
|
+
const {attributeName, label, model} = this.p
|
|
54
|
+
const {checked} = this.s
|
|
55
|
+
|
|
56
|
+
if (!(attributeName in model)) {
|
|
57
|
+
throw new Error(`${attributeName} isn't set on the resource ${model.modelClassData().name}`)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<View dataSet={{component: "api-maker/super-admin/edit-page/edit-attribute-input"}} style={{flexDirection: "row", alignItems: "center"}}>
|
|
62
|
+
<Checkbox
|
|
63
|
+
checked={checked}
|
|
64
|
+
dataSet={dataSet}
|
|
65
|
+
label={label}
|
|
66
|
+
onCheckedChange={this.tt.onCheckedChange}
|
|
67
|
+
style={styles.checkbox}
|
|
68
|
+
/>
|
|
69
|
+
</View>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
onCheckedChange = (newChecked) => {
|
|
74
|
+
if (this.form) {
|
|
75
|
+
this.form.setValue(this.p.name, newChecked)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this.setState({checked: newChecked})
|
|
79
|
+
}
|
|
80
|
+
}))
|
|
@@ -34,13 +34,20 @@ export default memo(shapeComponent(class EditAttributeContent extends BaseCompon
|
|
|
34
34
|
id,
|
|
35
35
|
model
|
|
36
36
|
},
|
|
37
|
-
onChangeValue: this.tt.onChangeValue
|
|
38
|
-
|
|
37
|
+
onChangeValue: this.tt.onChangeValue,
|
|
38
|
+
value: this.s.value
|
|
39
|
+
}), [attribute.attribute, id, model, this.s.value])
|
|
39
40
|
|
|
40
41
|
return attribute.content(contentArgs)
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
defaultValue = () =>
|
|
44
|
+
defaultValue = () => {
|
|
45
|
+
if (!(this.p.attribute.attribute in this.p.model)) {
|
|
46
|
+
throw new Error(`No attribute called ${this.p.attribute.attribute} in model ${this.p.model.modelClassData().name}`)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return this.p.model[this.p.attribute.attribute]() || ""
|
|
50
|
+
}
|
|
44
51
|
|
|
45
52
|
onChangeValue = (newValue) => {
|
|
46
53
|
this.setState({value: newValue})
|
|
@@ -42,7 +42,7 @@ export default memo(shapeComponent(class EditAttributeInput extends BaseComponen
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
return (
|
|
45
|
-
<View
|
|
45
|
+
<View dataSet={{component: "api-maker/super-admin/edit-page/edit-attribute-input"}}>
|
|
46
46
|
<Text>{label}</Text>
|
|
47
47
|
<View>
|
|
48
48
|
<TextInput
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import BaseComponent from "../../base-component"
|
|
2
2
|
import {digg} from "diggerize"
|
|
3
|
+
import EditAttributeCheckbox from "./edit-attribute-checkbox"
|
|
3
4
|
import EditAttributeContent from "./edit-attribute-content"
|
|
4
5
|
import EditAttributeInput from "./edit-attribute-input"
|
|
5
6
|
import * as inflection from "inflection"
|
|
@@ -7,6 +8,7 @@ import Locales from "shared/locales"
|
|
|
7
8
|
import {memo} from "react"
|
|
8
9
|
import PropTypes from "prop-types"
|
|
9
10
|
import propTypesExact from "prop-types-exact"
|
|
11
|
+
import {View} from "react-native"
|
|
10
12
|
import {shapeComponent} from "set-state-compare/src/shape-component.js"
|
|
11
13
|
|
|
12
14
|
export default memo(shapeComponent(class EditAttribute extends BaseComponent {
|
|
@@ -20,37 +22,59 @@ export default memo(shapeComponent(class EditAttribute extends BaseComponent {
|
|
|
20
22
|
const {attribute, model, modelClass} = this.props
|
|
21
23
|
const availableLocales = Locales.availableLocales()
|
|
22
24
|
const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
|
|
25
|
+
const modelAttribute = modelClass.attributes().find((modelAttributeI) => modelAttributeI.name() == attribute.attribute)
|
|
23
26
|
|
|
24
27
|
return (
|
|
25
|
-
|
|
26
|
-
{
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
28
|
+
<View dataSet={{component: "api-maker/super-admin/edit-page/edit-attribute"}} style={{marginBottom: attribute.translated ? undefined : 12}}>
|
|
29
|
+
{(() => {
|
|
30
|
+
if (attribute.content) {
|
|
31
|
+
return (
|
|
32
|
+
<EditAttributeContent
|
|
33
|
+
attribute={attribute}
|
|
34
|
+
id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}`}
|
|
35
|
+
model={model}
|
|
36
|
+
name={inflection.underscore(attribute.attribute)}
|
|
37
|
+
/>
|
|
38
|
+
)
|
|
39
|
+
} else if (attribute.translated) {
|
|
40
|
+
return (
|
|
41
|
+
<>
|
|
42
|
+
{availableLocales.map((locale) =>
|
|
43
|
+
<View key={locale} style={{marginBottom: 12}}>
|
|
44
|
+
<EditAttributeInput
|
|
45
|
+
attributeName={`${attribute.attribute}${inflection.camelize(locale)}`}
|
|
46
|
+
id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}_${locale}`}
|
|
47
|
+
label={`${modelClass.humanAttributeName(attribute.attribute)} (${locale})`}
|
|
48
|
+
model={model}
|
|
49
|
+
name={`${inflection.underscore(attribute.attribute)}_${locale}`}
|
|
50
|
+
/>
|
|
51
|
+
</View>
|
|
52
|
+
)}
|
|
53
|
+
</>
|
|
54
|
+
)
|
|
55
|
+
} else if (modelAttribute?.getColumn()?.getType() == "boolean") {
|
|
56
|
+
return (
|
|
57
|
+
<EditAttributeCheckbox
|
|
58
|
+
attributeName={attribute.attribute}
|
|
59
|
+
id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}`}
|
|
60
|
+
label={modelClass.humanAttributeName(attribute.attribute)}
|
|
61
|
+
model={model}
|
|
62
|
+
name={inflection.underscore(attribute.attribute)}
|
|
63
|
+
/>
|
|
64
|
+
)
|
|
65
|
+
} else {
|
|
66
|
+
return (
|
|
67
|
+
<EditAttributeInput
|
|
68
|
+
attributeName={attribute.attribute}
|
|
69
|
+
id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}`}
|
|
70
|
+
label={modelClass.humanAttributeName(attribute.attribute)}
|
|
71
|
+
model={model}
|
|
72
|
+
name={inflection.underscore(attribute.attribute)}
|
|
73
|
+
/>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
})()}
|
|
77
|
+
</View>
|
|
54
78
|
)
|
|
55
79
|
}
|
|
56
80
|
}))
|
package/src/utils/checkbox.jsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BaseComponent from "../base-component"
|
|
2
|
-
import {CheckBox, View} from "react-native"
|
|
2
|
+
import {CheckBox, Pressable, View} from "react-native"
|
|
3
3
|
import {memo} from "react"
|
|
4
4
|
import PropTypes from "prop-types"
|
|
5
5
|
import propTypesExact from "prop-types-exact"
|
|
@@ -13,22 +13,32 @@ export default memo(shapeComponent(class ApiMakerUtilsCheckbox extends BaseCompo
|
|
|
13
13
|
|
|
14
14
|
static propTypes = propTypesExact({
|
|
15
15
|
checked: PropTypes.bool.isRequired,
|
|
16
|
+
dataSet: PropTypes.object,
|
|
16
17
|
label: PropTypes.string,
|
|
17
|
-
|
|
18
|
+
onCheckedChange: PropTypes.func.isRequired,
|
|
19
|
+
style: PropTypes.object
|
|
18
20
|
})
|
|
19
21
|
|
|
20
22
|
render() {
|
|
21
|
-
const {checked, label,
|
|
23
|
+
const {checked, label, onCheckedChange} = this.p
|
|
24
|
+
const actualStyle = Object.assign(
|
|
25
|
+
{flexDirection: "row", alignItems: "center"},
|
|
26
|
+
this.props.style
|
|
27
|
+
)
|
|
22
28
|
|
|
23
29
|
return (
|
|
24
|
-
<View dataSet={{component: "api-maker--utils--checkbox"}} style={
|
|
25
|
-
<CheckBox onValueChange={
|
|
30
|
+
<View dataSet={{component: "api-maker--utils--checkbox"}} style={actualStyle}>
|
|
31
|
+
<CheckBox dataSet={this.props.dataSet} onValueChange={onCheckedChange} value={checked} />
|
|
26
32
|
{label &&
|
|
27
|
-
<
|
|
28
|
-
{
|
|
29
|
-
|
|
33
|
+
<Pressable onPress={this.tt.onLabelPressed}>
|
|
34
|
+
<Text style={{marginLeft: 3}}>
|
|
35
|
+
{label}
|
|
36
|
+
</Text>
|
|
37
|
+
</Pressable>
|
|
30
38
|
}
|
|
31
39
|
</View>
|
|
32
40
|
)
|
|
33
41
|
}
|
|
42
|
+
|
|
43
|
+
onLabelPressed = () => this.p.onCheckedChange(!this.p.checked)
|
|
34
44
|
}))
|