shinkansen-cogs 2.0.0 → 2.0.1

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/index.d.mts CHANGED
@@ -66,7 +66,7 @@ declare global {
66
66
  accessKey?: string
67
67
  placeholder?: string
68
68
  onChange?: OnEventType
69
- fieldRef?: React.RefObject<any> // eslint-disable-line @typescript-eslint/no-explicit-any -- Simplicity
69
+ fieldRef: React.RefObject<any> // eslint-disable-line @typescript-eslint/no-explicit-any -- Simplicity
70
70
  }
71
71
  }
72
72
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shinkansen-cogs",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Shinkansen Cogs",
5
5
  "keywords": [
6
6
  "Shinkansen",
@@ -62,7 +62,7 @@
62
62
  "@babel/register": "^7.25.9",
63
63
  "@modernpoacher/design-system": "2.0.0",
64
64
  "@sequencemedia/eslint-config-standard": "^0.2.42",
65
- "@sequencemedia/eslint-config-typescript": "^0.1.58",
65
+ "@sequencemedia/eslint-config-typescript": "^0.1.59",
66
66
  "@storybook/addon-actions": "^8.6.4",
67
67
  "@storybook/addon-essentials": "^8.6.4",
68
68
  "@storybook/addon-links": "^8.6.4",
@@ -136,7 +136,7 @@ export default class CheckboxCog extends CheckCog {
136
136
  tabIndex,
137
137
  accessKey,
138
138
  placeholder,
139
- fieldRef
139
+ fieldRef = React.createRef()
140
140
  } = this.props
141
141
 
142
142
  return (
@@ -104,7 +104,7 @@ export default class EmailCog extends ValueCog {
104
104
  tabIndex,
105
105
  accessKey,
106
106
  placeholder,
107
- fieldRef
107
+ fieldRef = React.createRef()
108
108
  } = this.props
109
109
 
110
110
  return (
@@ -59,7 +59,7 @@ export class ValueCog extends Cog {
59
59
  accessKey,
60
60
  placeholder,
61
61
  onChange = DEFAULT_HANDLE_EVENT,
62
- fieldRef
62
+ fieldRef = React.createRef()
63
63
  } = this.props
64
64
 
65
65
  return (
@@ -122,7 +122,7 @@ export class CheckCog extends Cog {
122
122
  placeholder,
123
123
  onChange = DEFAULT_HANDLE_EVENT,
124
124
  onClick = DEFAULT_HANDLE_EVENT,
125
- fieldRef
125
+ fieldRef = React.createRef()
126
126
  } = this.props
127
127
 
128
128
  return (
@@ -105,7 +105,7 @@ export default class NumberCog extends ValueCog {
105
105
  tabIndex,
106
106
  accessKey,
107
107
  placeholder,
108
- fieldRef
108
+ fieldRef = React.createRef()
109
109
  } = this.props
110
110
 
111
111
  return (
@@ -104,7 +104,7 @@ export default class PasswordCog extends ValueCog {
104
104
  tabIndex,
105
105
  accessKey,
106
106
  placeholder,
107
- fieldRef
107
+ fieldRef = React.createRef()
108
108
  } = this.props
109
109
 
110
110
  return (
@@ -136,7 +136,7 @@ export default class Radio extends CheckCog {
136
136
  tabIndex,
137
137
  accessKey,
138
138
  placeholder,
139
- fieldRef
139
+ fieldRef = React.createRef()
140
140
  } = this.props
141
141
 
142
142
  return (
@@ -10,6 +10,7 @@
10
10
  import React from 'react'
11
11
  import PropTypes from 'prop-types'
12
12
  import classnames from 'classnames'
13
+ import equal from 'fast-deep-equal'
13
14
 
14
15
  import {
15
16
  ValueField
@@ -23,16 +24,50 @@ function DEFAULT_HANDLE_EVENT () {
23
24
  }
24
25
 
25
26
  /**
26
- * @param {{ target: { selectedOptions: { value?: string, text?: string }[] } }} event
27
- * @returns {string[]}
27
+ * @param {HTMLOptionElement[]} selectedOptions
28
+ * @returns {string[]}
28
29
  */
29
- function getSelectedValues ({ target: { selectedOptions } }) {
30
+ function fromSelectedOptions (selectedOptions) {
30
31
  return (
31
32
  Array.from(selectedOptions)
32
33
  .map(({ value, text }) => (value || text) ?? '')
33
34
  )
34
35
  }
35
36
 
37
+ /**
38
+ * @param {{ current?: { selectedOptions: HTMLOptionElement[] } }} fieldRef
39
+ * @returns {string[] | undefined}
40
+ */
41
+ function getCurrentValues ({ current }) {
42
+ return (
43
+ current
44
+ ? fromSelectedOptions(current.selectedOptions)
45
+ : undefined
46
+ )
47
+ }
48
+
49
+ /**
50
+ * @param {{ current?: { value: string } }} fieldRef
51
+ * @returns {string | undefined}
52
+ */
53
+ function getCurrentValue ({ current }) {
54
+ return (
55
+ current
56
+ ? current.value
57
+ : undefined
58
+ )
59
+ }
60
+
61
+ /**
62
+ * @param {{ target: { selectedOptions: HTMLOptionElement[] } }} event
63
+ * @returns {string[]}
64
+ */
65
+ function getSelectedValues ({ target: { selectedOptions } }) {
66
+ return (
67
+ fromSelectedOptions(selectedOptions)
68
+ )
69
+ }
70
+
36
71
  /**
37
72
  * @param {{ target: { value?: string } }} event
38
73
  * @returns {string}
@@ -67,6 +102,42 @@ export default class SelectField extends ValueField {
67
102
  )
68
103
  }
69
104
 
105
+ /**
106
+ * @returns {void}
107
+ */
108
+ handleFieldRef = () => {
109
+ const {
110
+ fieldRef,
111
+ multiple,
112
+ defaultValue,
113
+ value = defaultValue
114
+ } = this.props
115
+
116
+ if (multiple) {
117
+ const currentValues = getCurrentValues(fieldRef)
118
+
119
+ if (!equal(value, currentValues)) {
120
+ const {
121
+ onChange = DEFAULT_HANDLE_EVENT,
122
+ name
123
+ } = this.props
124
+
125
+ onChange(name, currentValues)
126
+ }
127
+ } else {
128
+ const currentValue = getCurrentValue(fieldRef)
129
+
130
+ if (value !== currentValue) {
131
+ const {
132
+ onChange = DEFAULT_HANDLE_EVENT,
133
+ name
134
+ } = this.props
135
+
136
+ onChange(name, currentValue)
137
+ }
138
+ }
139
+ }
140
+
70
141
  /**
71
142
  * @overload
72
143
  * @param {{ target: { selectedOptions: { value?: string, text?: string }[] } }} event
@@ -128,7 +128,7 @@ export default class SelectCog extends ValueCog {
128
128
  accessKey,
129
129
  placeholder,
130
130
  multiple = false,
131
- fieldRef,
131
+ fieldRef = React.createRef(),
132
132
  children
133
133
  } = this.props
134
134
 
@@ -104,7 +104,7 @@ export default class TextCog extends ValueCog {
104
104
  tabIndex,
105
105
  accessKey,
106
106
  placeholder,
107
- fieldRef
107
+ fieldRef = React.createRef()
108
108
  } = this.props
109
109
 
110
110
  return (
@@ -104,7 +104,7 @@ export default class TextareaCog extends ValueCog {
104
104
  tabIndex,
105
105
  accessKey,
106
106
  placeholder,
107
- fieldRef
107
+ fieldRef = React.createRef()
108
108
  } = this.props
109
109
 
110
110
  return (
@@ -7,14 +7,26 @@ declare module '#cogs/components/field' {
7
7
  export type CheckProps = CogsTypes.Components.Field.CheckProps
8
8
 
9
9
  export class ValueField<P extends ValueProps> extends Field<P> {
10
+ componentDidMount (): void
11
+
10
12
  shouldComponentUpdate (props: ValueProps): boolean
11
13
 
14
+ componentDidUpdate (): void
15
+
16
+ handleFieldRef (): void
17
+
12
18
  handleChange (event: ChangeEvent<HTMLTextAreaElement> | ChangeEvent<HTMLInputElement>): void
13
19
  }
14
20
 
15
21
  export class CheckField<P extends CheckProps> extends Field<P> {
22
+ componentDidMount (): void
23
+
16
24
  shouldComponentUpdate (props: CheckProps): boolean
17
25
 
26
+ componentDidUpdate (): void
27
+
28
+ handleFieldRef (): void
29
+
18
30
  handleChange (event: ChangeEvent<HTMLInputElement>): void
19
31
 
20
32
  handleClick (event: MouseEvent<HTMLInputElement>): void
@@ -19,12 +19,43 @@ function DEFAULT_HANDLE_EVENT () {
19
19
  //
20
20
  }
21
21
 
22
+ /**
23
+ * @param {{ current?: { value: string } }} fieldRef
24
+ * @returns {string | undefined}
25
+ */
26
+ function getCurrentValue ({ current }) {
27
+ return (
28
+ current
29
+ ? current.value
30
+ : undefined
31
+ )
32
+ }
33
+
34
+ /**
35
+ * @param {{ current?: { checked: boolean } }} fieldRef
36
+ * @returns {boolean | undefined}
37
+ */
38
+ function getCurrentChecked ({ current }) {
39
+ return (
40
+ current
41
+ ? current.checked
42
+ : undefined
43
+ )
44
+ }
45
+
22
46
  /**
23
47
  * ValueField component
24
48
  *
25
49
  * @extends {Field<FieldProps & ValueProps>}
26
50
  */
27
51
  export class ValueField extends Field {
52
+ /**
53
+ * @returns {void}
54
+ */
55
+ componentDidMount () {
56
+ this.handleFieldRef()
57
+ }
58
+
28
59
  /**
29
60
  * @param {ValueProps} props
30
61
  * @returns {boolean}
@@ -36,8 +67,38 @@ export class ValueField extends Field {
36
67
  )
37
68
  }
38
69
 
70
+ /**
71
+ * @returns {void}
72
+ */
73
+ componentDidUpdate () {
74
+ this.handleFieldRef()
75
+ }
76
+
77
+ /**
78
+ * @returns {void}
79
+ */
80
+ handleFieldRef = () => {
81
+ const {
82
+ fieldRef,
83
+ defaultValue,
84
+ value = defaultValue
85
+ } = this.props
86
+
87
+ const currentValue = getCurrentValue(fieldRef)
88
+
89
+ if (value !== currentValue) {
90
+ const {
91
+ onChange = DEFAULT_HANDLE_EVENT,
92
+ name
93
+ } = this.props
94
+
95
+ onChange(name, currentValue)
96
+ }
97
+ }
98
+
39
99
  /**
40
100
  * @param {{ target: { value?: string } }} event
101
+ * @returns {void}
41
102
  */
42
103
  handleChange = ({ target: { value } }) => {
43
104
  const {
@@ -52,7 +113,12 @@ export class ValueField extends Field {
52
113
  ValueField.propTypes = {
53
114
  ...Field.propTypes,
54
115
  value: PropTypes.string,
55
- defaultValue: PropTypes.string
116
+ defaultValue: PropTypes.string,
117
+ fieldRef: PropTypes.shape({
118
+ current: PropTypes.shape({
119
+ value: PropTypes.string
120
+ })
121
+ }).isRequired
56
122
  }
57
123
 
58
124
  /**
@@ -61,6 +127,13 @@ ValueField.propTypes = {
61
127
  * @extends {Field<FieldProps & CheckProps>}
62
128
  */
63
129
  export class CheckField extends Field {
130
+ /**
131
+ * @returns {void}
132
+ */
133
+ componentDidMount () {
134
+ this.handleFieldRef()
135
+ }
136
+
64
137
  /**
65
138
  * @param {CheckProps} props
66
139
  * @returns {boolean}
@@ -83,8 +156,40 @@ export class CheckField extends Field {
83
156
  )
84
157
  }
85
158
 
159
+ /**
160
+ * @returns {void}
161
+ */
162
+ componentDidUpdate () {
163
+ this.handleFieldRef()
164
+ }
165
+
166
+ /**
167
+ * @returns {void}
168
+ */
169
+ handleFieldRef = () => {
170
+ const {
171
+ fieldRef,
172
+ value,
173
+ defaultChecked,
174
+ checked = defaultChecked
175
+ } = this.props
176
+
177
+ const currentValue = getCurrentValue(fieldRef)
178
+ const currentChecked = getCurrentChecked(fieldRef)
179
+
180
+ if (value !== currentValue || checked !== currentChecked) {
181
+ const {
182
+ onChange = DEFAULT_HANDLE_EVENT,
183
+ name
184
+ } = this.props
185
+
186
+ onChange(name, currentValue, currentChecked)
187
+ }
188
+ }
189
+
86
190
  /**
87
191
  * @param {{ target: { value?: string, checked?: boolean } }} event
192
+ * @returns {void}
88
193
  */
89
194
  handleChange = ({ target: { value, checked } }) => {
90
195
  const {
@@ -97,6 +202,7 @@ export class CheckField extends Field {
97
202
 
98
203
  /**
99
204
  * @param {{ target: { value?: string, checked?: boolean } }} event
205
+ * @returns {void}
100
206
  */
101
207
  handleClick = ({ target: { value, checked } }) => {
102
208
  const {
@@ -113,5 +219,11 @@ CheckField.propTypes = {
113
219
  value: PropTypes.string,
114
220
  checked: PropTypes.bool,
115
221
  defaultChecked: PropTypes.bool,
116
- onClick: PropTypes.func
222
+ onClick: PropTypes.func,
223
+ fieldRef: PropTypes.shape({
224
+ current: PropTypes.shape({
225
+ value: PropTypes.string,
226
+ checked: PropTypes.bool
227
+ })
228
+ }).isRequired
117
229
  }
@@ -121,7 +121,7 @@ export default class Cog extends Component {
121
121
  accessKey,
122
122
  placeholder,
123
123
  onChange = DEFAULT_HANDLE_EVENT,
124
- fieldRef
124
+ fieldRef = React.createRef()
125
125
  } = this.props
126
126
 
127
127
  return (
@@ -172,8 +172,8 @@ Cog.propTypes = {
172
172
  placeholder: PropTypes.string,
173
173
  onChange: PropTypes.func,
174
174
  fieldRef: PropTypes.shape({
175
- current: PropTypes.shape({}).isRequired
175
+ current: PropTypes.shape({
176
+ value: PropTypes.string
177
+ })
176
178
  })
177
179
  }
178
-
179
- export class CheckCog { }
@@ -69,6 +69,8 @@ Field.propTypes = {
69
69
  placeholder: PropTypes.string,
70
70
  onChange: PropTypes.func,
71
71
  fieldRef: PropTypes.shape({
72
- current: PropTypes.shape({}).isRequired
73
- })
72
+ current: PropTypes.shape({
73
+ value: PropTypes.string
74
+ })
75
+ }).isRequired
74
76
  }
@@ -1,5 +0,0 @@
1
- export function DEFAULT_HANDLE_EVENT (
2
- name: string,
3
- value?: string | string[],
4
- checked?: boolean
5
- ): void
@@ -1,3 +0,0 @@
1
- export function DEFAULT_HANDLE_EVENT () {
2
- /* */
3
- }