0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

fix(console): change checkbox to controlled comp (#1235)

This commit is contained in:
Wang Sijie 2022-06-24 18:23:48 +08:00 committed by GitHub
parent a7642cec98
commit 9a72a34cef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 9 deletions

View file

@ -1,23 +1,36 @@
import { nanoid } from 'nanoid';
import React, { forwardRef, InputHTMLAttributes, ReactNode, Ref, useState } from 'react';
import React, { ReactNode, useState } from 'react';
import Icon from './Icon';
import * as styles from './index.module.scss';
type Props = Omit<InputHTMLAttributes<HTMLInputElement>, 'type'> & {
type Props = {
// eslint-disable-next-line react/boolean-prop-naming
value: boolean;
onChange: (value: boolean) => void;
label?: ReactNode;
// eslint-disable-next-line react/boolean-prop-naming
disabled: boolean;
};
const Checkbox = ({ label, disabled, ...rest }: Props, ref: Ref<HTMLInputElement>) => {
const Checkbox = ({ value, onChange, label, disabled }: Props) => {
const [id] = useState(nanoid());
return (
<div className={styles.checkbox}>
<input id={id} type="checkbox" disabled={disabled} {...rest} ref={ref} />
<input
id={id}
type="checkbox"
checked={value}
disabled={disabled}
onChange={(event) => {
onChange(event.target.checked);
}}
/>
<Icon className={styles.icon} />
{label && <label htmlFor={id}>{label}</label>}
</div>
);
};
export default forwardRef<HTMLInputElement, Props>(Checkbox);
export default Checkbox;

View file

@ -60,10 +60,17 @@ const SignInMethodsForm = () => {
return (
<div key={method} className={styles.method}>
<Checkbox
label={label}
disabled={primaryMethod === method}
{...register(`signInMethods.${method}`)}
<Controller
name={`signInMethods.${method}`}
control={control}
render={({ field: { value, onChange } }) => (
<Checkbox
label={label}
disabled={primaryMethod === method}
value={value}
onChange={onChange}
/>
)}
/>
{enabled && <ConnectorSetupWarning method={method} />}
</div>