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

[Conditions] Fix lint errors #1167

Merged
merged 2 commits into from
Apr 16, 2020
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
7 changes: 4 additions & 3 deletions react/components/Conditions/StrategySelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { ChangeEvent } from 'react'

import IconCaretDown from '../icon/CaretDown/index.js'
import { Labels, Operator } from './typings'
Expand All @@ -20,10 +20,10 @@ const StrategySelector: React.FC<Props> = ({
onChangeOperator,
labels,
}) => {
const handleOperatorChange = event => {
const handleOperatorChange = (event: ChangeEvent<HTMLSelectElement>) => {
const newOperator = event.target.value
if (operator !== newOperator) {
onChangeOperator(newOperator)
onChangeOperator(newOperator as Operator)
}
}

Expand All @@ -37,6 +37,7 @@ const StrategySelector: React.FC<Props> = ({
key="seletorContent-select"
className="o-0 absolute top-0 left-0 w-100 bottom-0 pointer t-small"
onChange={handleOperatorChange}
onBlur={undefined}
value={operator}
style={{
// safari select height fix
Expand Down
49 changes: 32 additions & 17 deletions react/components/Conditions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Statement from '../Statement/index'
import { Labels, Operator } from './typings'
import { DEFAULT_LABELS, MEDIUM_ICON_SIZE } from './constants'
import { SubjectOptions } from '../Statement/Atoms/SubjectAtom'
import { StatementProp } from '../Statement/typings'

type Props = {
canDelete?: boolean
Expand All @@ -21,16 +22,11 @@ type Props = {
operator: Operator
options: SubjectOptions
hideOperator?: boolean
statements: Array<{
subject: string
verb: string
object?: unknown
error?: string
}>
statements: StatementProp[]
subjectPlaceholder: string
}

const Conditions: React.FC<Props> = ({
const Conditions = ({
canDelete,
statements,
options,
Expand All @@ -42,8 +38,8 @@ const Conditions: React.FC<Props> = ({
operator,
onChangeOperator,
onChangeStatements,
}) => {
const objectIsEmpty = object => {
}: Props) => {
const objectIsEmpty = (object: unknown) => {
if (object === undefined) return true
if (object === null) return true
if (object === '') return true
Expand Down Expand Up @@ -73,15 +69,18 @@ const Conditions: React.FC<Props> = ({
onChangeStatements([...statements, emptyStatement])
}

const handleRemoveStatement = index => {
const handleRemoveStatement = (index: number) => {
const updatedStatements = statements
.slice(0, index)
.concat(statements.slice(index + 1))

onChangeStatements(updatedStatements)
}

const handleUpdatestatement = (newStatement, statementIndex) => {
const handleUpdatestatement = (
newStatement: StatementProp,
statementIndex: number
) => {
const newStatements = statements.map((statement, idx) =>
idx === statementIndex ? newStatement : statement
)
Expand All @@ -95,8 +94,10 @@ const Conditions: React.FC<Props> = ({
<StrategySelector
isRtl={isRtl}
operator={operator}
labels={labels}
onChangeOperator={operator => onChangeOperator(operator)}
labels={labels as Labels}
onChangeOperator={(newOperator: Operator) =>
onChangeOperator(newOperator)
}
/>
</div>
)}
Expand Down Expand Up @@ -136,6 +137,17 @@ const Conditions: React.FC<Props> = ({
{}
)

const handleKeyDown = ({
key,
}: React.KeyboardEvent<HTMLDivElement>) => {
const SPACE = ' '
const ENTER = 'Enter'

if (key === SPACE || key === ENTER) {
handleRemoveStatement(statementIndex)
}
}

const statementContent = [
<div key="1" className="flex-grow-1">
<Statement
Expand All @@ -152,6 +164,9 @@ const Conditions: React.FC<Props> = ({
canDelete &&
(!isFullWidth ? (
<div
role="button"
tabIndex={0}
onKeyDown={handleKeyDown}
key="2"
className="ma3 c-muted-2 pointer hover-c-danger"
onClick={() => handleRemoveStatement(statementIndex)}
Expand All @@ -167,7 +182,7 @@ const Conditions: React.FC<Props> = ({
icon={<IconClose className="c-on-action-primary" />}
onClick={() => handleRemoveStatement(statementIndex)}
>
{labels.delete ?? DEFAULT_LABELS.delete}
{labels?.delete ?? DEFAULT_LABELS.delete}
</ButtonWithIcon>
</div>
)),
Expand All @@ -192,8 +207,8 @@ const Conditions: React.FC<Props> = ({
<Separator
label={
operator === 'all'
? labels.operatorAnd ?? DEFAULT_LABELS.operatorAnd
: labels.operatorOr ?? DEFAULT_LABELS.operatorOr
? labels?.operatorAnd ?? DEFAULT_LABELS.operatorAnd
: labels?.operatorOr ?? DEFAULT_LABELS.operatorOr
}
/>
)}
Expand All @@ -213,7 +228,7 @@ const Conditions: React.FC<Props> = ({
disabled={!canAddNewCondition()}
onClick={handleAddNewCondition}
>
{labels.addNewCondition ?? DEFAULT_LABELS.addNewCondition}
{labels?.addNewCondition ?? DEFAULT_LABELS.addNewCondition}
</ButtonWithIcon>
</div>
</div>
Expand Down
47 changes: 28 additions & 19 deletions react/components/Statement/index.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
import React from 'react'

import SubjectAtom, { SubjectOptions } from './Atoms/SubjectAtom'
import VerbAtom from './Atoms/VerbAtom'
import VerbAtom, { VerbOption } from './Atoms/VerbAtom'
import ObjectAtom from './Atoms/ObjectAtom'
import { StatementProp } from './typings'

type Props = {
type StatementProps = {
isFullWidth?: boolean
isRtl?: boolean
omitSubject?: boolean
omitVerbs?: boolean
onChangeStatement: (statement: Props['statement']) => void
onChangeStatement: (statement: StatementProp) => void
options: SubjectOptions
statement?: {
subject: string
verb: string
object?: unknown
error?: string
}
statement?: StatementProp
subjectPlaceholder: string
}

const Statement: React.FC<Props> = ({
const NoopComponent = () => <></>

const Statement: React.FC<StatementProps> = ({
isFullWidth = false,
isRtl,
omitSubject,
omitVerbs,
onChangeStatement,
options,
statement = { subject: '', verb: '', object: null, error: null },
statement = { subject: '', verb: '', object: null },
subjectPlaceholder,
}) => {
const verbOptions =
statement.subject &&
options[statement.subject].verbs.find(verb => verb.value === statement.verb)
let verbOptions: VerbOption = {
label: '',
value: '',
object: NoopComponent,
emersonlaurentino marked this conversation as resolved.
Show resolved Hide resolved
}

if (statement.subject) {
const foundOption = options[statement.subject].verbs.find(
verb => verb.value === statement.verb
)

if (foundOption) {
verbOptions = foundOption
}
}

const statementAtoms = [
!omitSubject && (
<SubjectAtom
Expand All @@ -43,8 +54,6 @@ const Statement: React.FC<Props> = ({
...statement,
subject,
verb: options[subject].verbs[0].value,
object: null,
error: null,
}
onChangeStatement(newStatement)
}}
Expand All @@ -62,8 +71,8 @@ const Statement: React.FC<Props> = ({
const newStatement = {
...statement,
verb,
object: null,
error: null,
object: undefined,
error: undefined,
}
onChangeStatement(newStatement)
}}
Expand All @@ -76,7 +85,7 @@ const Statement: React.FC<Props> = ({
disabled={!statement.verb}
error={statement.error}
object={statement.object}
onChange={(object, error = null) => {
onChange={(object, error) => {
const newStatement = {
...statement,
object,
Expand Down
7 changes: 7 additions & 0 deletions react/components/Statement/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ export type SelectedOption = {
group: SelectOptionGroup['label']
option: SelectOption
}

export interface StatementProp {
subject: string
verb: string
object?: unknown
error?: string
}