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

fix: issues with switch and checked state #3831

Merged
merged 6 commits into from
Feb 19, 2025
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
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
Loading