Skip to content

Commit

Permalink
feat(Switch): deprecate default_state property (#1864)
Browse files Browse the repository at this point in the history
  • Loading branch information
langz committed Feb 22, 2023
1 parent 7b9554d commit da2c568
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,6 @@ exports[`GlobalStatus snapshot have to match linked components snapshot 1`] = `
className={null}
custom_element={null}
custom_method={null}
default_state={null}
disabled={null}
global_status_id="linked"
id="switch"
Expand Down
11 changes: 1 addition & 10 deletions packages/dnb-eufemia/src/components/switch/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export default class Switch extends React.PureComponent {
]),
label_position: PropTypes.oneOf(['left', 'right']),
title: PropTypes.string,
default_state: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), // Deprecated
checked: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
disabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
id: PropTypes.string,
Expand Down Expand Up @@ -93,7 +92,6 @@ export default class Switch extends React.PureComponent {
label: null,
label_position: null,
title: null,
default_state: null, // Deprecated
checked: null,
disabled: null,
id: null,
Expand Down Expand Up @@ -130,14 +128,7 @@ export default class Switch extends React.PureComponent {
static getDerivedStateFromProps(props, state) {
if (state._listenForPropChanges) {
if (props.checked !== state._checked) {
if (
props.default_state !== null &&
typeof state.checked === 'undefined'
) {
state.checked = Switch.parseChecked(props.default_state)
} else {
state.checked = Switch.parseChecked(props.checked)
}
state.checked = Switch.parseChecked(props.checked)
}
}
state._listenForPropChanges = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,6 @@ describe('Switch component', () => {
expect(my_event.mock.calls[0][0].checked).toBe(true)
})

it('uses "default_value" as the startup state – if given', () => {
expect(
mount(<Component default_state={true} />)
.find('input')
.instance().checked
).toBe(true)

expect(
mount(<Component default_state={true} checked={false} />)
.find('input')
.instance().checked
).toBe(true)

const Comp = mount(<Component default_state={false} checked={true} />)
expect(Comp.find('input').instance().checked).toBe(false)

Comp.find('input').simulate('change')
expect(Comp.find('input').instance().checked).toBe(true)
})

it('does handle controlled vs uncontrolled state properly', () => {
const ControlledVsUncontrolled = () => {
const [checked, setChecked] = React.useState(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ exports[`Switch component have to match snapshot 1`] = `
className="className"
custom_element={{}}
custom_method={[Function]}
default_state="default_state"
disabled="disabled"
global_status_id="main"
id="id"
Expand Down Expand Up @@ -146,7 +145,6 @@ exports[`Switch component have to match snapshot 1`] = `
"className": "className",
"custom_element": {},
"custom_method": [Function],
"default_state": "default_state",
"disabled": "disabled",
"global_status_id": "main",
"id": "id",
Expand Down

0 comments on commit da2c568

Please sign in to comment.