Skip to content

Commit 1bf1990

Browse files
authored
replace legacy context API (#500)
* replace legacy context API * bump version * update action cache version
1 parent 5b53150 commit 1bf1990

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
node-version: '14.18.2'
1717

1818
- name: Restore cache
19-
uses: actions/cache@v3.3.1
19+
uses: actions/cache@v3
2020
with:
2121
path: ~/.cache/yarn
2222
key: yarn-node-modules-v1-${{ hashFiles('yarn.lock') }}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ic-snacks",
3-
"version": "0.0.159",
3+
"version": "0.0.160",
44
"description": "The Instacart Component Library for Web",
55
"main": "dist/snacks.js",
66
"module": "dist/esm/index.js",

src/components/Forms/Form.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import React from 'react'
22
import PropTypes from 'prop-types'
33
import isEqual from 'lodash.isequal'
44

5+
// Create a new context
6+
export const FormContext = React.createContext({
7+
registerComponent: () => {},
8+
unregisterComponent: () => {},
9+
})
10+
511
class Form extends React.Component {
612
static propTypes = {
713
/** Form html chilren */
@@ -16,10 +22,6 @@ class Form extends React.Component {
1622
serverErrors: PropTypes.shape({}),
1723
}
1824

19-
static childContextTypes = {
20-
ICFormable: PropTypes.object,
21-
}
22-
2325
constructor() {
2426
super()
2527
this.state = { serverErrors: null }
@@ -28,15 +30,6 @@ class Form extends React.Component {
2830
this.invalidComponents = []
2931
}
3032

31-
getChildContext() {
32-
return {
33-
ICFormable: {
34-
registerComponent: this.registerComponent,
35-
unregisterComponent: this.unregisterComponent,
36-
},
37-
}
38-
}
39-
4033
componentDidMount() {
4134
this.updateModel()
4235
}
@@ -100,11 +93,17 @@ class Form extends React.Component {
10093

10194
render() {
10295
const { children, formProps } = this.props
96+
const contextValue = {
97+
registerComponent: this.registerComponent,
98+
unregisterComponent: this.unregisterComponent,
99+
}
103100

104101
return (
105-
<form {...formProps} onSubmit={this.handleSubmit}>
106-
{children}
107-
</form>
102+
<FormContext.Provider value={contextValue}>
103+
<form {...formProps} onSubmit={this.handleSubmit}>
104+
{children}
105+
</form>
106+
</FormContext.Provider>
108107
)
109108
}
110109
}

src/components/Forms/FormComponent.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
33
import Validator from 'validator'
4+
import { FormContext } from './Form'
45

56
const formComponent = WrappedComponent => {
67
return class FormComponent extends React.Component {
@@ -19,9 +20,7 @@ const formComponent = WrappedComponent => {
1920
validations: PropTypes.object,
2021
}
2122

22-
static contextTypes = {
23-
ICFormable: PropTypes.object,
24-
}
23+
static contextType = FormContext
2524

2625
state = {
2726
isValid: true,
@@ -34,11 +33,13 @@ const formComponent = WrappedComponent => {
3433
this.uniqueId =
3534
id || `${name}-${Math.floor(Math.random() * 0xffff)}`.replace(/[^A-Za-z0-9-]/gi, '')
3635

37-
this.context.ICFormable && this.context.ICFormable.registerComponent(this)
36+
const { registerComponent } = this.context
37+
registerComponent && registerComponent(this)
3838
}
3939

4040
componentWillUnmount() {
41-
this.context.ICFormable && this.context.ICFormable.unregisterComponent(this)
41+
const { unregisterComponent } = this.context
42+
unregisterComponent && unregisterComponent(this)
4243
}
4344

4445
getValue = () => {

0 commit comments

Comments
 (0)