Skip to content

Commit

Permalink
fix: issues with switch and checked state (#3831)
Browse files Browse the repository at this point in the history
* fix: issues with switch and checked state

* fix: issue for react

* chore: update snapshot

* fix: issue with aria checked for switch

* fix: issue with typescript
  • Loading branch information
nmerget authored Feb 19, 2025
1 parent 125c4c2 commit 96816d1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions packages/components/scripts/post-build/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ const setControlValueAccessorReplacements = (
from: 'ngOnInit()',
to: `
writeValue(value: any) {
this.${valueAccessor} = value;
this.${valueAccessor} = ${valueAccessor === 'checked' ? '!!' : ''}value;
if (this._ref?.nativeElement) {
this.renderer.setProperty(this._ref?.nativeElement, '${valueAccessor}', value);
this.renderer.setProperty(this._ref?.nativeElement, '${valueAccessor}', ${valueAccessor === 'checked' ? '!!' : ''}value);
}
}
Expand Down
4 changes: 1 addition & 3 deletions packages/components/src/components/switch/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
GlobalState,
IconAfterProps,
IconProps,
InitializedState,
SizeProps
} from '../../shared/model';

Expand Down Expand Up @@ -41,5 +40,4 @@ export type DBSwitchState = DBSwitchDefaultState &
GlobalState &
ChangeEventState<HTMLInputElement> &
FocusEventState<HTMLInputElement> &
FormState &
InitializedState;
FormState;
17 changes: 14 additions & 3 deletions packages/components/src/components/switch/switch.lite.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
onMount,
onUpdate,
Show,
useDefaultProps,
useMetadata,
Expand Down Expand Up @@ -28,8 +29,10 @@ export default function DBSwitch(props: DBSwitchProps) {
// jscpd:ignore-start
const state = useStore<DBSwitchState>({
_id: undefined,
_checked: false,
initialized: false,
_checked: useTarget({
react: props['defaultChecked'] ?? false,
default: false
}),
handleChange: (event: ChangeEvent<HTMLInputElement>) => {
if (props.onChange) {
props.onChange(event);
Expand Down Expand Up @@ -71,6 +74,13 @@ export default function DBSwitch(props: DBSwitchProps) {
onMount(() => {
state._id = props.id ?? `switch-${uuid()}`;
});

onUpdate(() => {
if (props.checked !== undefined && props.checked !== null) {
state._checked = !!props.checked;
}
}, [props.checked]);

// jscpd:ignore-end

return (
Expand All @@ -85,9 +95,10 @@ export default function DBSwitch(props: DBSwitchProps) {
id={state._id}
type="checkbox"
role="switch"
aria-checked={state._checked}
aria-checked={getBooleanAsString(state._checked)}
ref={_ref}
checked={props.checked}
value={props.value}
disabled={props.disabled}
aria-describedby={props.describedbyid}
aria-invalid={props.validation === 'invalid'}
Expand Down

0 comments on commit 96816d1

Please sign in to comment.