From 55153acc39f915c895e6066df3b26aa39d08ae24 Mon Sep 17 00:00:00 2001 From: jordanl17 Date: Wed, 8 May 2024 11:51:48 +0100 Subject: [PATCH] fix(studio): adding tooltip to read-only bool inputs (#6580) * fix(studio): adding tooltip to read-only bool inputs * fix(studio): testing for tooltip on boolean read-only inputs * fix(studio): removing memoisation as it was useless --- dev/test-studio/schema/standard/booleans.ts | 20 +++++++++----- .../core/form/inputs/BooleanInput.test.tsx | 13 +++++++++ .../src/core/form/inputs/BooleanInput.tsx | 27 ++++++++++++------- .../sanity/src/core/i18n/bundles/studio.ts | 2 ++ 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/dev/test-studio/schema/standard/booleans.ts b/dev/test-studio/schema/standard/booleans.ts index f74300b7d3a..c3ac640080a 100644 --- a/dev/test-studio/schema/standard/booleans.ts +++ b/dev/test-studio/schema/standard/booleans.ts @@ -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', @@ -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', diff --git a/packages/sanity/src/core/form/inputs/BooleanInput.test.tsx b/packages/sanity/src/core/form/inputs/BooleanInput.test.tsx index 6c7fe3ab473..1742f78d744 100644 --- a/packages/sanity/src/core/form/inputs/BooleanInput.test.tsx +++ b/packages/sanity/src/core/form/inputs/BooleanInput.test.tsx @@ -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' @@ -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) => , + }) + + 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, diff --git a/packages/sanity/src/core/form/inputs/BooleanInput.tsx b/packages/sanity/src/core/form/inputs/BooleanInput.tsx index 22f86548165..3442c6d42d3 100644 --- a/packages/sanity/src/core/form/inputs/BooleanInput.tsx +++ b/packages/sanity/src/core/form/inputs/BooleanInput.tsx @@ -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' @@ -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' @@ -33,19 +36,23 @@ export function BooleanInput(props: BooleanInputProps) { const tone: CardTone | undefined = readOnly ? 'transparent' : undefined + const input = ( + + + + ) + return ( - - - + {readOnly ? {input} : input}