@kaspernj/api-maker 1.0.431 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaspernj/api-maker",
3
- "version": "1.0.431",
3
+ "version": "1.0.432",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "index.js",
@@ -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
+ }))
@@ -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"
@@ -21,37 +22,58 @@ export default memo(shapeComponent(class EditAttribute extends BaseComponent {
21
22
  const {attribute, model, modelClass} = this.props
22
23
  const availableLocales = Locales.availableLocales()
23
24
  const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
25
+ const modelAttribute = modelClass.attributes().find((modelAttributeI) => modelAttributeI.name() == attribute.attribute)
24
26
 
25
27
  return (
26
28
  <View dataSet={{component: "api-maker/super-admin/edit-page/edit-attribute"}} style={{marginBottom: attribute.translated ? undefined : 12}}>
27
- {attribute.content &&
28
- <EditAttributeContent
29
- attribute={attribute}
30
- id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}`}
31
- model={model}
32
- name={inflection.underscore(attribute.attribute)}
33
- />
34
- }
35
- {!attribute.content && attribute.translated && availableLocales.map((locale) =>
36
- <View key={locale} style={{marginBottom: 12}}>
37
- <EditAttributeInput
38
- attributeName={`${attribute.attribute}${inflection.camelize(locale)}`}
39
- id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}_${locale}`}
40
- label={`${modelClass.humanAttributeName(attribute.attribute)} (${locale})`}
41
- model={model}
42
- name={`${inflection.underscore(attribute.attribute)}_${locale}`}
43
- />
44
- </View>
45
- )}
46
- {!attribute.content && !attribute.translated &&
47
- <EditAttributeInput
48
- attributeName={attribute.attribute}
49
- id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}`}
50
- label={modelClass.humanAttributeName(attribute.attribute)}
51
- model={model}
52
- name={inflection.underscore(attribute.attribute)}
53
- />
54
- }
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
+ })()}
55
77
  </View>
56
78
  )
57
79
  }
@@ -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
- onValueChange: PropTypes.func.isRequired
18
+ onCheckedChange: PropTypes.func.isRequired,
19
+ style: PropTypes.object
18
20
  })
19
21
 
20
22
  render() {
21
- const {checked, label, onValueChange} = this.p
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={{flexDirection: "row", alignItems: "center"}}>
25
- <CheckBox onValueChange={onValueChange} value={checked} />
30
+ <View dataSet={{component: "api-maker--utils--checkbox"}} style={actualStyle}>
31
+ <CheckBox dataSet={this.props.dataSet} onValueChange={onCheckedChange} value={checked} />
26
32
  {label &&
27
- <Text style={{marginLeft: 3}}>
28
- {label}
29
- </Text>
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
  }))