Skip to content

Commit

Permalink
Classifier: Clean up TaskInput styling (#6532)
Browse files Browse the repository at this point in the history
* Refactor styling of TaskInput and its components

* Remove enzyme from InputStatus.spec
  • Loading branch information
goplayoutside3 authored Dec 12, 2024
1 parent 57c52de commit 3e8f2ee
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
import PropTypes from 'prop-types'
import { node, string } from 'prop-types'
import styled, { css } from 'styled-components'

export const StyledInputIcon = styled.span`
${props => props.color && css`color: ${props.color};`}
background-color: #2D2D2D;
display: flex;
align-items: center;
padding-left: 15px;
&::after {
content: " ";
margin-right: 1ch;
white-space: pre;
}
padding: 15px;
> svg {
fill-opacity: 0.1;
height: 1.5em;
stroke: currentColor;
stroke-width: 5;
vertical-align: bottom;
width: 1.5em;
}
`

export default function InputIcon ({ color, icon }) {
export default function InputIcon ({ color = 'white', icon }) {
return (
<StyledInputIcon color={color}>
{icon}
</StyledInputIcon>
)
}

InputIcon.defaultProps = {
color: 'white'
}

InputIcon.propTypes = {
color: PropTypes.string,
icon: PropTypes.node.isRequired
color: string,
icon: node.isRequired
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import PropTypes from 'prop-types'
import { number, shape, oneOfType, string } from 'prop-types'
import { Text } from 'grommet'
import styled from 'styled-components'
import { Markdownz } from '@zooniverse/react-components'
import { useTranslation } from '@translations/i18n'

export const StyledInputStatus = styled(Text)`
display: flex;
justify-content: end;
align-items: center;
flex-grow: 1;
padding-right: 15px;
`

export default function InputStatus ({ count, tool }) {
export default function InputStatus ({ count = 0, tool = {} }) {
const { t } = useTranslation('plugins')
let status = t('InputStatus.drawn', { count })
const hasMin = tool.min && tool.min > 0
Expand All @@ -23,27 +24,22 @@ export default function InputStatus ({ count, tool }) {
}

return (
<StyledInputStatus textAlign='end'>
<Markdownz>{status}</Markdownz>
<StyledInputStatus>
{status}
</StyledInputStatus>
)
}

InputStatus.defaultProps = {
count: 0,
tool: {}
}

InputStatus.propTypes = {
count: PropTypes.number,
tool: PropTypes.shape({
min: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
count: number,
tool: shape({
min: oneOfType([
number,
string
]),
max: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
max: oneOfType([
number,
string
])
})
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
import { shallow } from 'enzyme'
import InputStatus, { StyledInputStatus } from './InputStatus'
import { render, screen } from '@testing-library/react'
import InputStatus from './InputStatus'

/** The translation function will simply return keys in a testing env */

describe('InputStatus', function () {
it('should render without crashing', function () {
const wrapper = shallow(<InputStatus />)
expect(wrapper).to.be.ok()
})

it('should render a StyledInputStatus component', function () {
const wrapper = shallow(<InputStatus />)
expect(wrapper.find(StyledInputStatus)).to.have.lengthOf(1)
})

it('should not render any requirements if props.tool.min or props.tool.max is not defined', function () {
const wrapper = shallow(<InputStatus />)
expect(wrapper.contains('InputStatus.drawn')).to.be.true()
render(<InputStatus />)
expect(screen.getByText('InputStatus.drawn')).to.be.ok()
})

it('should render minimum drawing requirements if props.tool.min is defined', function () {
const wrapper = shallow(<InputStatus tool={{ min: 1 }} />)
expect(wrapper.contains('InputStatus.min')).to.be.true()
render(<InputStatus tool={{ min: 1 }} />)
expect(screen.getByText('InputStatus.min')).to.be.ok()
})

it('should render maxmimum drawing requirements if props.tool.max is defined', function () {
const wrapper = shallow(<InputStatus tool={{ max: 2 }} />)
expect(wrapper.contains('InputStatus.max')).to.be.true()
render(<InputStatus tool={{ max: 2 }} />)
expect(screen.getByText('InputStatus.max')).to.be.ok()
})

it('should render minimum and maxmimum drawing requirements if props.tool.min and props.tool.max are defined', function () {
const wrapper = shallow(<InputStatus tool={{ min: 1, max: 2 }} />)
expect(wrapper.contains('InputStatus.maxAndMin')).to.be.true()
render(<InputStatus tool={{ min: 1, max: 2 }} />)
expect(screen.getByText('InputStatus.maxAndMin')).to.be.ok()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ function getHoverStyles (props, active = false) {
}

const StyledText = styled(Text)`
align-items: baseline;
${props => props.theme.dark ?
css`background: transparent;` :
css`background: ${props.theme.global.colors['light-1']};`
Expand All @@ -46,8 +45,9 @@ const StyledText = styled(Text)`
box-shadow: 1px 1px 2px 0 rgba(0,0,0,0.5);
cursor: pointer;
display: flex;
margin-top: .5em;
margin-bottom: .5em;
align-items: center;
min-height: 45px;
margin: 8px 0;
`

const StyledLabel = styled.label`
Expand Down Expand Up @@ -85,7 +85,7 @@ const StyledLabel = styled.label`
css`border: 2px solid ${props.theme.global.colors['light-1']};` :
css`border: 2px solid ${props.theme.global.colors['neutral-1']};`
}
> img:only-child, svg:only-child {
background-color: inherit !important;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ export function TaskInput({
type={type}
value={index}
/>
<StyledText margin={{ vertical: 'small', horizontal: 'none' }}>
<StyledText>
<TaskInputLabel label={label} labelIcon={labelIcon} labelStatus={labelStatus} />
</StyledText>
</StyledLabel>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Edit } from 'grommet-icons'

import TaskInput from './TaskInput'
import InputStatus from '../InputStatus/InputStatus.js'

export default {
title: 'Tasks / TaskInput',
component: TaskInput,
args: {
checked: true
}
}

export const Default = {}

Default.args = {
label: 'Label',
index: 0,
name: 'default',
type: 'radio'
}

export const CustomIcon = {}

CustomIcon.args = {
label: 'Drawing a point',
labelIcon: <Edit color='gold' />,
index: 0,
name: 'custom-icon',
type: 'radio'
}

export const ImageAndTextLabel = {}

ImageAndTextLabel.args = {
label: '![alt](https://thumbnails.zooniverse.org/60x/panoptes-uploads.zooniverse.org/production/project_attached_image/1ec52a74-9e49-4579-91ff-0140eb5371e6.png) Galaxies',
index: 0,
name: 'markdownz',
type: 'checkbox'
}

export const WithLabelStatus = {}

WithLabelStatus.args = {
index: 0,
label: 'Do the task',
labelStatus: <InputStatus count={3} />,
name: 'drawing',
type: 'radio'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@ import PropTypes from 'prop-types'
import { Box, Text } from 'grommet'
import styled from 'styled-components'
import { Markdownz } from '@zooniverse/react-components'
import { doesTheLabelHaveAnImage } from '../../helpers'

const StyledTaskInputLabelWrapper = styled.span`
&:first-child {
margin-top: 0;
}
&:last-child {
margin-bottom: 0;
}
`
function doesTheLabelHaveAnImage(label) {
const imageRegex = /(?:!\[(.*?)\]\((.*?)\))/g
return label && imageRegex.test(label)
}

const StyledText = styled(Text)`
display: flex;
align-content: center;
padding-left: 15px;
padding-right: 15px;
img, svg {
padding: 10px;
vertical-align: middle;
}
align-items: center;
padding: 5px 0 5px 15px;
gap: 15px; // in case there's multiple elements in the Markdown
`

export default function TaskInputLabel({
Expand All @@ -36,18 +27,16 @@ export default function TaskInputLabel({

return (
<Box
as={StyledTaskInputLabelWrapper}
direction='row'
fill='horizontal'
justify={howShouldTheLabelBeAligned}
pad={{ right: '15px' }}
>
{labelIcon &&
labelIcon}
{labelIcon && labelIcon}
<StyledText>
<Markdownz inline >{label}</Markdownz>
<Markdownz inline>{label}</Markdownz>
</StyledText>
{labelStatus &&
labelStatus}
{labelStatus && labelStatus}
</Box>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
*/

import { render, screen } from '@testing-library/react'
import { expect } from 'chai'
import TaskInputLabel from './TaskInputLabel'
import { Markdownz } from '@zooniverse/react-components'

const label = 'test label'

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Blank } from "grommet-icons"
import InputStatus from "../../../components/InputStatus"
import { Markdownz } from "@zooniverse/react-components"
import { observer } from "mobx-react"
import PropTypes from "prop-types"
import { bool, shape, string } from "prop-types"
import styled from "styled-components"
import TaskInput from "../../../components/TaskInput"

Expand All @@ -25,17 +25,10 @@ const StyledToolIcon = styled.div`
background-color: #2d2d2d;
display: flex;
align-items: center;
padding-left: 15px;
&::after {
content: " ";
margin-right: 1ch;
white-space: pre;
}
padding: 15px;
> svg {
height: 1.5em;
vertical-align: bottom;
width: 1.5em;
}
`
Expand Down Expand Up @@ -87,8 +80,9 @@ function VolumetricTask({ disabled = false, task }) {
}

VolumetricTask.propTypes = {
task: PropTypes.shape({
instruction: PropTypes.string,
disabled: bool,
task: shape({
instruction: string,
}),
}

Expand Down

0 comments on commit 3e8f2ee

Please sign in to comment.