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(studio): adding tooltip to read-only bool inputs #6580

Merged
merged 5 commits into from
May 8, 2024
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
20 changes: 13 additions & 7 deletions dev/test-studio/schema/standard/booleans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,23 @@ export default defineType({
title: 'Title',
},
{
name: 'switchTest',
name: 'switch_doesnt_exist',
type: 'boolean',
title: `I'm a switch`,
description: 'Try toggling me! This is the new switch design',
},
{
name: 'switchTest',
type: 'boolean',
title: `true & Read only`,
readOnly: true,
},
{
name: 'switch',
type: 'boolean',
title: `false & Read only`,
readOnly: true,
},
{
name: 'switchIndeterminate2',
type: 'boolean',
Expand All @@ -42,12 +54,6 @@ export default defineType({
layout: 'checkbox',
},
},
{
name: 'switch',
type: 'boolean',
description: 'Should be either true or false',
title: 'Check me?',
},
{
name: 'checkbox',
type: 'boolean',
Expand Down
13 changes: 13 additions & 0 deletions packages/sanity/src/core/form/inputs/BooleanInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {describe, expect, it} from '@jest/globals'
import {defineField} from '@sanity/types'
import {fireEvent, screen, waitFor} from '@testing-library/react'
import userEvent from '@testing-library/user-event'

import {renderBooleanInput} from '../../../../test/form/renderBooleanInput'
Expand Down Expand Up @@ -171,6 +172,18 @@ describe('readOnly property', () => {
expect(input).not.toHaveFocus()
})

it('renders a tooltip on the switch', async () => {
const {container} = await renderBooleanInput({
fieldDefinition: defs.booleanReadOnly,
render: (inputProps) => <BooleanInput {...inputProps} readOnly />,
})

const input = container.querySelector('input[id="booleanReadOnly"]')
fireEvent.mouseEnter(input!)

await waitFor(() => screen.getByText('Disabled'))
})

it('does not make field read-only with callback', async () => {
const {onChange, result} = await renderBooleanInput({
fieldDefinition: defs.readOnlyCallback,
Expand Down
27 changes: 17 additions & 10 deletions packages/sanity/src/core/form/inputs/BooleanInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {Box, Card, type CardTone, Checkbox, Flex, Switch} from '@sanity/ui'
import {styled} from 'styled-components'

import {Tooltip} from '../../../ui-components'
import {useTranslation} from '../../i18n/hooks/useTranslation'
import {FormFieldHeaderText} from '../components/formField/FormFieldHeaderText'
import {FormFieldStatus} from '../components/formField/FormFieldStatus'
import {type BooleanInputProps} from '../types'
Expand All @@ -23,6 +25,7 @@ const ZeroLineHeightBox = styled(Box)`
* @beta
*/
export function BooleanInput(props: BooleanInputProps) {
const {t} = useTranslation()
const {id, value, schemaType, readOnly, elementProps, validation} = props
const layout = schemaType.options?.layout || 'switch'

Expand All @@ -33,19 +36,23 @@ export function BooleanInput(props: BooleanInputProps) {

const tone: CardTone | undefined = readOnly ? 'transparent' : undefined

const input = (
<ZeroLineHeightBox padding={3}>
<LayoutSpecificInput
label={schemaType.title}
{...elementProps}
checked={checked}
readOnly={readOnly}
indeterminate={indeterminate}
style={{margin: -4}}
/>
</ZeroLineHeightBox>
)

return (
<Root border data-testid="boolean-input" radius={2} tone={tone}>
<Flex>
<ZeroLineHeightBox padding={3}>
<LayoutSpecificInput
label={schemaType.title}
{...elementProps}
checked={checked}
disabled={readOnly}
indeterminate={indeterminate}
style={{margin: -4}}
/>
</ZeroLineHeightBox>
{readOnly ? <Tooltip content={t('inputs.boolean.disabled')}>{input}</Tooltip> : input}
<Box flex={1} paddingY={2}>
<FormFieldHeaderText
deprecated={schemaType.deprecated}
Expand Down
2 changes: 2 additions & 0 deletions packages/sanity/src/core/i18n/bundles/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ export const studioLocaleStrings = defineLocalesResources('studio', {
'inputs.array.read-only-label': 'This field is read-only',
/** Label for when the array input is resolving the initial value for the item */
'inputs.array.resolving-initial-value': 'Resolving initial value…',
/** Tooltip content when boolean input is disabled */
'inputs.boolean.disabled': 'Disabled',
/** Placeholder value for datetime input */
'inputs.datetime.placeholder': 'e.g. {{example}}',
/** Acessibility label for button to open file options menu */
Expand Down
Loading