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

Checkbox ID and margin fixes #1152

Merged
merged 1 commit into from
Apr 17, 2024
Merged
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
15 changes: 11 additions & 4 deletions packages/web-components/src/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
import { css } from '@emotion/react'
import { darkTheme, lightTheme } from './theme.js'
import React, { ButtonHTMLAttributes, CSSProperties, useId, useMemo, useState } from 'react'
import React, { ButtonHTMLAttributes, CSSProperties, useMemo, useState } from 'react'

interface CheckboxProps extends ButtonHTMLAttributes<HTMLButtonElement> {
themeColor: 'light' | 'dark'
Expand Down Expand Up @@ -45,6 +45,12 @@ const baseStyle: CSSProperties = {
borderWidth: '1px',
}

const ID_LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

const generateRandomId = () => {
return Array.from({ length: 8 }, () => ID_LETTERS[Math.floor(Math.random() * ID_LETTERS.length)]).join('')
}

export const Checkbox: React.FC<CheckboxProps> = ({ themeColor, onChange, checked, labelText }: CheckboxProps) => {
const theme = useMemo(() => (themeColor === 'light' ? lightTheme : darkTheme), [themeColor])
const checkboxStyleBase: CSSProperties = {
Expand All @@ -59,14 +65,15 @@ export const Checkbox: React.FC<CheckboxProps> = ({ themeColor, onChange, checke
borderColor: hover ? theme.palette.background.contrastText : theme.palette.grey[400],
appearance: checked ? 'auto' : 'none',
flex: 1,
margin: '0 15px',
margin: '15px',
}
}, [hover, theme, checked])
const id = useId()
const id = generateRandomId()
return (
<span style={{ display: 'inline-flex' }}>
<input
name={id}
id={id}
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
css={checkboxBefore}
Expand All @@ -84,7 +91,7 @@ export const Checkbox: React.FC<CheckboxProps> = ({ themeColor, onChange, checke
display: 'flex',
cursor: 'pointer',
userSelect: 'none',
top: '4px',
top: '18px',
}}
htmlFor={id}
>
Expand Down
Loading