Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Switch): deprecate default_state property #1864

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,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