@modernpoacher/sprockets 0.0.187 → 0.0.189

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": "@modernpoacher/sprockets",
3
- "version": "0.0.187",
3
+ "version": "0.0.189",
4
4
  "description": "Sprockets",
5
5
  "keywords": [
6
6
  "Sprockets",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "classnames": "^2.5.1",
45
- "debug": "^4.3.6"
45
+ "debug": "^4.3.7"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@babel/core": "^7.25.2",
@@ -50,7 +50,7 @@
50
50
  "@babel/preset-env": "^7.25.2",
51
51
  "@babel/preset-react": "^7.24.7",
52
52
  "@babel/register": "^7.24.6",
53
- "@modernpoacher/design-system": "1.0.113",
53
+ "@modernpoacher/design-system": "1.0.179",
54
54
  "@modernpoacher/hooks": "^1.0.488",
55
55
  "@storybook/addon-actions": "7.6.20",
56
56
  "@storybook/addon-essentials": "7.6.20",
@@ -117,6 +117,7 @@
117
117
  "./sprockets/fieldset/description": "./src/sprockets/fieldset/description/index.cjs",
118
118
  "./sprockets/fieldset/error-message": "./src/sprockets/fieldset/error-message/index.cjs",
119
119
  "./sprockets/fieldset/group": "./src/sprockets/fieldset/group/index.cjs",
120
- "./sprockets/fieldset/title": "./src/sprockets/fieldset/title/index.cjs"
120
+ "./sprockets/fieldset/title": "./src/sprockets/fieldset/title/index.cjs",
121
+ "./transformers/common": "./src/transformers/common/index.mjs"
121
122
  }
122
123
  }
@@ -12,7 +12,7 @@ export default class Description extends Component {
12
12
  return 'description'
13
13
  }
14
14
 
15
- shouldComponentUpdate (props) {
15
+ shouldComponentUpdate (props, state) {
16
16
  return (
17
17
  (props.description !== this.props.description)
18
18
  )
@@ -18,7 +18,7 @@ export default class ErrorMessage extends Component {
18
18
  * @param {{errorMessage?: string}} props Latest props
19
19
  * @returns {boolean}
20
20
  */
21
- shouldComponentUpdate (props) {
21
+ shouldComponentUpdate (props, state) {
22
22
  return (props.errorMessage !== this.props.errorMessage)
23
23
  }
24
24
 
@@ -20,7 +20,7 @@ export default class Group extends Component {
20
20
  * @param {GroupProps} props
21
21
  * @returns {boolean}
22
22
  */
23
- shouldComponentUpdate (props) {
23
+ shouldComponentUpdate (props, state) {
24
24
  return (
25
25
  (props.onChange !== this.props.onChange) ||
26
26
  (props.children !== this.props.children)
@@ -23,7 +23,7 @@ export default class Title extends Component {
23
23
  return 'title'
24
24
  }
25
25
 
26
- shouldComponentUpdate (props) {
26
+ shouldComponentUpdate (props, state) {
27
27
  return (
28
28
  (props.title !== this.props.title)
29
29
  )
package/src/index.d.mts CHANGED
@@ -12,6 +12,11 @@ declare namespace SprocketsTypes {
12
12
  visuallyHiddenText?: string
13
13
  }
14
14
  }
15
+
16
+ export interface ErrorDefinitionType {
17
+ text: string
18
+ href: string
19
+ }
15
20
  }
16
21
 
17
22
  declare module '@modernpoacher/sprockets' {
@@ -6,41 +6,73 @@
6
6
  * Group state
7
7
  *
8
8
  * @typedef {Object} GroupState
9
- * @property {{}} [children]
9
+ * @property {{}} [checkAnswers]
10
10
  */
11
11
 
12
12
  import React from 'react'
13
13
  import PropTypes from 'prop-types'
14
14
 
15
+ import debug from 'debug'
16
+
17
+ import equal from 'fast-deep-equal'
15
18
  import classnames from 'classnames'
16
19
 
17
20
  import Group from '@modernpoacher/sprockets/components/group'
18
21
 
22
+ import {
23
+ getKey
24
+ } from '@modernpoacher/sprockets/transformers/common'
25
+
26
+ import AnswerTitle from './answer-title.cjs'
27
+ import AnswerValue from './answer-value.cjs'
28
+ import ChangeAnswer from './change-answer.cjs'
29
+
30
+ const DEFAULT_CHECK_ANSWERS = []
31
+
32
+ const log = debug('@modernpoacher/sprockets/components/group/check-answers')
33
+
34
+ /* eslint-disable-next-line react/prop-types */
35
+ function render ({ answer: { title, value }, changeAnswer: { href, text, ...changeAnswer } }, index) {
36
+ log('render')
37
+
38
+ return (
39
+ <div key={getKey(href, text, index)} className='answer'>
40
+ <AnswerTitle title={title} />
41
+ <AnswerValue value={value} />
42
+ <ChangeAnswer
43
+ changeAnswer={{ ...changeAnswer, href, text }}
44
+ />
45
+ </div>
46
+ )
47
+ }
48
+
19
49
  export default class CheckAnswersGroup extends Group {
20
50
  /**
21
51
  * @type {GroupState}
22
52
  */
23
- state = {}
53
+ state = {
54
+ checkAnswers: DEFAULT_CHECK_ANSWERS
55
+ }
24
56
 
25
57
  getClassName () {
26
58
  return classnames(super.getClassName(), 'check-answers')
27
59
  }
28
60
 
29
61
  /**
30
- * Compare latest 'props' with 'state' for changes to 'children'
62
+ * Compare latest 'props' with 'state' for changes to 'checkAnswers'
31
63
  *
32
64
  * @param {GroupProps} props Latest props
33
65
  * @param {GroupState} state Current state
34
- * @returns {{children: {}}}
66
+ * @returns {{checkAnswers: {}}}
35
67
  */
36
- static getDerivedStateFromProps ({ children: c }, { children: C }) {
68
+ static getDerivedStateFromProps ({ checkAnswers }, { checkAnswers: C }) {
37
69
  return {
38
- children: (c !== C) ? c : C
70
+ checkAnswers: equal(checkAnswers, C) ? C : checkAnswers
39
71
  }
40
72
  }
41
73
 
42
74
  /**
43
- * Compare latest 'props' with 'state' for changes to 'children'
75
+ * Compare latest 'props' with 'state' for changes to 'checkAnswers'
44
76
  *
45
77
  * @param {GroupProps} props Latest props
46
78
  * @param {GroupState} state Current state
@@ -48,11 +80,11 @@ export default class CheckAnswersGroup extends Group {
48
80
  */
49
81
  shouldComponentUpdate (props, state) {
50
82
  const {
51
- children: c
83
+ checkAnswers: c = DEFAULT_CHECK_ANSWERS
52
84
  } = state
53
85
 
54
86
  const {
55
- children: C
87
+ checkAnswers: C = DEFAULT_CHECK_ANSWERS
56
88
  } = this.state
57
89
 
58
90
  return (c !== C)
@@ -60,16 +92,19 @@ export default class CheckAnswersGroup extends Group {
60
92
 
61
93
  render () {
62
94
  const {
63
- groupRef,
64
- children
95
+ checkAnswers = DEFAULT_CHECK_ANSWERS
65
96
  } = this.props
66
97
 
67
- if (children) {
98
+ if (checkAnswers.length) {
99
+ const {
100
+ groupRef
101
+ } = this.props
102
+
68
103
  return (
69
104
  <dl
70
105
  className={this.getClassName()}
71
106
  ref={groupRef}>
72
- {children}
107
+ {checkAnswers.map(render)}
73
108
  </dl>
74
109
  )
75
110
  }
@@ -80,14 +115,5 @@ export default class CheckAnswersGroup extends Group {
80
115
 
81
116
  CheckAnswersGroup.propTypes = {
82
117
  ...Group.propTypes,
83
- children: PropTypes.oneOfType([
84
- PropTypes.node,
85
- PropTypes.arrayOf(
86
- PropTypes.node
87
- )
88
- ])
89
- }
90
-
91
- CheckAnswersGroup.defaultProps = {
92
- ...Group.defaultProps
118
+ checkAnswers: PropTypes.arrayOf(PropTypes.shape())
93
119
  }
@@ -13,6 +13,8 @@ import Sprocket from '@modernpoacher/sprockets/sprockets'
13
13
  import Title from './title/index.jsx'
14
14
  import Group from './group/index.jsx'
15
15
 
16
+ const DEFAULT_CHECK_ANSWERS = []
17
+
16
18
  export default class CheckAnswersSprocket extends Sprocket {
17
19
  getClassName () {
18
20
  return classnames(super.getClassName(), 'check-answers')
@@ -22,10 +24,10 @@ export default class CheckAnswersSprocket extends Sprocket {
22
24
  * @param {SprocketProps} props
23
25
  * @returns {boolean}
24
26
  */
25
- shouldComponentUpdate (props) {
27
+ shouldComponentUpdate (props, state) {
26
28
  return (
27
- super.shouldComponentUpdate(props) ||
28
- (props.children !== this.props.children)
29
+ super.shouldComponentUpdate(props, state) ||
30
+ (props.checkAnswers !== this.props.checkAnswers)
29
31
  )
30
32
  }
31
33
 
@@ -43,22 +45,22 @@ export default class CheckAnswersSprocket extends Sprocket {
43
45
 
44
46
  renderGroup () {
45
47
  const {
46
- children
48
+ checkAnswers = DEFAULT_CHECK_ANSWERS
47
49
  } = this.props
48
50
 
49
51
  return (
50
- <Group>
51
- {children}
52
- </Group>
52
+ <Group
53
+ checkAnswers={checkAnswers}
54
+ />
53
55
  )
54
56
  }
55
57
 
56
58
  render () {
57
59
  const {
58
- children
60
+ checkAnswers = DEFAULT_CHECK_ANSWERS
59
61
  } = this.props
60
62
 
61
- if (children) {
63
+ if (checkAnswers.length) {
62
64
  return (
63
65
  <div className={this.getClassName()}>
64
66
  {this.renderTitle()}
@@ -73,14 +75,5 @@ export default class CheckAnswersSprocket extends Sprocket {
73
75
 
74
76
  CheckAnswersSprocket.propTypes = {
75
77
  ...Sprocket.propTypes,
76
- children: PropTypes.oneOfType([
77
- PropTypes.node,
78
- PropTypes.arrayOf(
79
- PropTypes.node
80
- )
81
- ])
82
- }
83
-
84
- CheckAnswersSprocket.defaultProps = {
85
- ...Sprocket.defaultProps
78
+ checkAnswers: PropTypes.arrayOf(PropTypes.shape())
86
79
  }
@@ -27,7 +27,3 @@ export default class CheckAnswersTitle extends Title {
27
27
  CheckAnswersTitle.propTypes = {
28
28
  ...Title.propTypes
29
29
  }
30
-
31
- CheckAnswersTitle.defaultProps = {
32
- ...Title.defaultProps
33
- }
@@ -6,41 +6,69 @@
6
6
  * Group state
7
7
  *
8
8
  * @typedef {Object} GroupState
9
- * @property {{}} [children]
9
+ * @property {{}} [errorSummary]
10
10
  */
11
11
 
12
12
  import React from 'react'
13
13
  import PropTypes from 'prop-types'
14
14
 
15
+ import debug from 'debug'
16
+
17
+ import equal from 'fast-deep-equal'
15
18
  import classnames from 'classnames'
16
19
 
17
20
  import Group from '@modernpoacher/sprockets/components/group'
18
21
 
22
+ import {
23
+ getKey
24
+ } from '@modernpoacher/sprockets/transformers/common'
25
+
26
+ import TextContent from '@modernpoacher/sprockets/components/common/text-content'
27
+
28
+ const DEFAULT_ERROR_SUMMARY = []
29
+
30
+ const log = debug('@modernpoacher/sprockets/sprockets/error-summary/group')
31
+
32
+ /* eslint-disable-next-line react/prop-types */
33
+ function render ({ href, text }, index) {
34
+ log('render')
35
+
36
+ return (
37
+ <li key={getKey(href, text, index)} className='error'>
38
+ <a href={href}>
39
+ <TextContent textContent={text} />
40
+ </a>
41
+ </li>
42
+ )
43
+ }
44
+
19
45
  export default class ErrorSummaryGroup extends Group {
20
46
  /**
21
47
  * @type {GroupState}
22
48
  */
23
- state = {}
49
+ state = {
50
+ errorSummary: DEFAULT_ERROR_SUMMARY
51
+ }
24
52
 
25
53
  getClassName () {
26
54
  return classnames(super.getClassName(), 'error-summary')
27
55
  }
28
56
 
29
57
  /**
30
- * Compare latest 'props' with 'state' for changes to 'children'
58
+ * Compare latest 'props' with 'state' for changes to 'errorSummary'
31
59
  *
32
60
  * @param {GroupProps} props Latest props
33
61
  * @param {GroupState} state Current state
34
62
  * @returns {GroupState}
35
63
  */
36
- static getDerivedStateFromProps ({ children: e }, { children: E }) {
64
+ static getDerivedStateFromProps ({ errorSummary }, { errorSummary: E }) {
37
65
  return {
38
- children: (e !== E) ? e : E
66
+ errorSummary: equal(errorSummary, E) ? E : errorSummary
39
67
  }
40
68
  }
41
69
 
42
70
  /**
43
- * Compare latest 'props' with 'state' for changes to 'children'
71
+ * Compare latest 'props' with 'state' for changes to 'errorSummary'
44
72
  *
45
73
  * @param {GroupProps} props Latest props
46
74
  * @param {GroupState} state Current state
@@ -48,11 +76,11 @@ export default class ErrorSummaryGroup extends Group {
48
76
  */
49
77
  shouldComponentUpdate (props, state) {
50
78
  const {
51
- children: e
79
+ errorSummary: e = DEFAULT_ERROR_SUMMARY
52
80
  } = state
53
81
 
54
82
  const {
55
- children: E
83
+ errorSummary: E = DEFAULT_ERROR_SUMMARY
56
84
  } = this.state
57
85
 
58
86
  return (e !== E)
@@ -60,16 +88,19 @@ export default class ErrorSummaryGroup extends Group {
60
88
 
61
89
  render () {
62
90
  const {
63
- groupRef,
64
- children
91
+ errorSummary = DEFAULT_ERROR_SUMMARY
65
92
  } = this.props
66
93
 
67
- if (children) {
94
+ if (errorSummary.length) {
95
+ const {
96
+ groupRef
97
+ } = this.props
98
+
68
99
  return (
69
100
  <ul
70
101
  className={this.getClassName()}
71
102
  ref={groupRef}>
72
- {children}
103
+ {errorSummary.map(render)}
73
104
  </ul>
74
105
  )
75
106
  }
@@ -80,14 +111,5 @@ export default class ErrorSummaryGroup extends Group {
80
111
 
81
112
  ErrorSummaryGroup.propTypes = {
82
113
  ...Group.propTypes,
83
- children: PropTypes.oneOfType([
84
- PropTypes.node,
85
- PropTypes.arrayOf(
86
- PropTypes.node
87
- )
88
- ])
89
- }
90
-
91
- ErrorSummaryGroup.defaultProps = {
92
- ...Group.defaultProps
114
+ errorSummary: PropTypes.arrayOf(PropTypes.shape())
93
115
  }
@@ -13,6 +13,8 @@ import Sprocket from '@modernpoacher/sprockets/sprockets'
13
13
  import Title from './title/index.jsx'
14
14
  import Group from './group/index.jsx'
15
15
 
16
+ const DEFAULT_ERROR_SUMMARY = []
17
+
16
18
  export default class ErrorSummarySprocket extends Sprocket {
17
19
  getClassName () {
18
20
  return classnames(super.getClassName(), 'error-summary')
@@ -22,10 +24,10 @@ export default class ErrorSummarySprocket extends Sprocket {
22
24
  * @param {SprocketProps} props
23
25
  * @returns {boolean}
24
26
  */
25
- shouldComponentUpdate (props) {
27
+ shouldComponentUpdate (props, state) {
26
28
  return (
27
- super.shouldComponentUpdate(props) ||
28
- (props.children !== this.props.children)
29
+ super.shouldComponentUpdate(props, state) ||
30
+ (props.errorSummary !== this.props.errorSummary)
29
31
  )
30
32
  }
31
33
 
@@ -43,22 +45,22 @@ export default class ErrorSummarySprocket extends Sprocket {
43
45
 
44
46
  renderGroup () {
45
47
  const {
46
- children
48
+ errorSummary = DEFAULT_ERROR_SUMMARY
47
49
  } = this.props
48
50
 
49
51
  return (
50
- <Group>
51
- {children}
52
- </Group>
52
+ <Group
53
+ errorSummary={errorSummary}
54
+ />
53
55
  )
54
56
  }
55
57
 
56
58
  render () {
57
59
  const {
58
- children
60
+ errorSummary = DEFAULT_ERROR_SUMMARY
59
61
  } = this.props
60
62
 
61
- if (children) {
63
+ if (errorSummary.length) {
62
64
  const className = this.getClassName()
63
65
 
64
66
  return (
@@ -75,14 +77,5 @@ export default class ErrorSummarySprocket extends Sprocket {
75
77
 
76
78
  ErrorSummarySprocket.propTypes = {
77
79
  ...Sprocket.propTypes,
78
- children: PropTypes.oneOfType([
79
- PropTypes.node,
80
- PropTypes.arrayOf(
81
- PropTypes.node
82
- )
83
- ])
84
- }
85
-
86
- ErrorSummarySprocket.defaultProps = {
87
- ...Sprocket.defaultProps
80
+ errorSummary: PropTypes.arrayOf(PropTypes.shape())
88
81
  }
@@ -27,7 +27,3 @@ export default class ErrorSummaryTitle extends Title {
27
27
  ErrorSummaryTitle.propTypes = {
28
28
  ...Title.propTypes
29
29
  }
30
-
31
- ErrorSummaryTitle.defaultProps = {
32
- ...Title.defaultProps
33
- }
@@ -14,7 +14,3 @@ export default class FieldsetDescription extends Description {
14
14
  FieldsetDescription.propTypes = {
15
15
  ...Description.propTypes
16
16
  }
17
-
18
- FieldsetDescription.defaultProps = {
19
- ...Description.defaultProps
20
- }
@@ -14,7 +14,3 @@ export default class FieldsetErrorMessage extends ErrorMessage {
14
14
  FieldsetErrorMessage.propTypes = {
15
15
  ...ErrorMessage.propTypes
16
16
  }
17
-
18
- FieldsetErrorMessage.defaultProps = {
19
- ...ErrorMessage.defaultProps
20
- }
@@ -14,7 +14,3 @@ export default class FieldsetGroup extends Group {
14
14
  FieldsetGroup.propTypes = {
15
15
  ...Group.propTypes
16
16
  }
17
-
18
- FieldsetGroup.defaultProps = {
19
- ...Group.defaultProps
20
- }
@@ -33,9 +33,9 @@ export default class FieldsetSprocket extends Sprocket {
33
33
  * @param {SprocketProps} props
34
34
  * @returns {boolean}
35
35
  */
36
- shouldComponentUpdate (props) {
36
+ shouldComponentUpdate (props, state) {
37
37
  return (
38
- super.shouldComponentUpdate(props) ||
38
+ super.shouldComponentUpdate(props, state) ||
39
39
  (props.description !== this.props.description) ||
40
40
  (props.errorMessage !== this.props.errorMessage)
41
41
  )
@@ -104,7 +104,3 @@ FieldsetSprocket.propTypes = {
104
104
  uri: PropTypes.string.isRequired
105
105
  })
106
106
  }
107
-
108
- FieldsetSprocket.defaultProps = {
109
- ...Sprocket.defaultProps
110
- }
@@ -13,7 +13,3 @@ export default class FieldsetTitle extends Title {
13
13
  FieldsetTitle.propTypes = {
14
14
  ...Title.propTypes
15
15
  }
16
-
17
- FieldsetTitle.defaultProps = {
18
- ...Title.defaultProps
19
- }
@@ -23,7 +23,7 @@ export default class Sprocket extends Component {
23
23
  * @param {SprocketProps} props
24
24
  * @returns {boolean}
25
25
  */
26
- shouldComponentUpdate (props) {
26
+ shouldComponentUpdate (props, state) {
27
27
  return (
28
28
  (props.children !== this.props.children) ||
29
29
  (props.title !== this.props.title) ||
@@ -84,7 +84,3 @@ Sprocket.propTypes = {
84
84
  current: PropTypes.shape().isRequired
85
85
  })
86
86
  }
87
-
88
- Sprocket.defaultProps = {
89
- onChange: DEFAULT_HANDLE_CHANGE
90
- }
@@ -0,0 +1,3 @@
1
+ declare module '@modernpoacher/sprockets/transformers/common' {
2
+ export function getKey (href: string, text: string, index: number): string
3
+ }
@@ -0,0 +1,26 @@
1
+ import debug from 'debug'
2
+
3
+ const CHAR32 = String.fromCharCode(32)
4
+ const CHAR45 = String.fromCharCode(45)
5
+
6
+ const log = debug('@modernpoacher/sprockets/transformers/common')
7
+
8
+ log('`shinkansen` is awake')
9
+
10
+ /**
11
+ * @param {string} href
12
+ * @param {string} text
13
+ * @param {number} index
14
+ * @returns {string}
15
+ */
16
+ export function getKey (href, text, index) {
17
+ return (
18
+ `${href}-${text}-${index}`
19
+ .toLowerCase()
20
+ .replace(/^#/, 'error-')
21
+ .replace(/[^\w\-\d]/g, CHAR32).trim()
22
+ .replace(/\s\s+/g, CHAR32)
23
+ .replace(/\-\-+/g, CHAR45) // eslint-disable-line
24
+ .replace(/[\s\s|\-\-|\s\-|\-\s]+/g, CHAR45) // eslint-disable-line
25
+ )
26
+ }