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

Support self operator #3

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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: 7 additions & 0 deletions examples/react-guide-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ const GlobalStyle = createGlobalStyle`
.GlobalStyle.styled {}
`

const selfOperators = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

操作符应该需要根据字段类型做适配

{ label: '大于', value: '>' },
{ label: '小于', value: '<' },
{ label: '等于', value: '==' },
]

export default function App () {
const [content, setContent] = useState(MOCK_CONTENT)
return <>
<GlobalStyle />
<div style={{ padding: 16, border: '1px solid #E6E6E6', backgroundColor: 'white' }}>
<RuleEditor models={MOCK_MODEL_LIST} content={content}
onChange={(nextContent) => setContent(nextContent)}
// operators={selfOperators}
// maxDepth={2}
/>
</div>
Expand Down
11 changes: 7 additions & 4 deletions src/RuleEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import React, { useContext, useEffect, useState } from 'react'
import { Box, Button, Icon } from '@alifd/next'
import { EXPRESSION_TYPE_DATASOURCE, OPERATOR_TYPE_MAP, uuid } from './shared'
import { RuleEditorContext, tree2map, ModelAndField, RuleGroupNodeRelationColumn, WidthAutoSelect, RuleGroupNodeBodyColumnWrapper, fixContent, RuleGroupNodeWrapper, RuleGroupNodeRelationColumnWrapper, RuleConditionNodeWrapper, OperatorSelect, LiteralSetter } from './RuleEditorParts'
import { IRuleConditionNode, IRuleGroupNode, IRelation, IRuleModel, IRuleNodeType, IExpressionType } from './types'
import { IRuleConditionNode, IRuleGroupNode, IRelation, IRuleModel, IRuleNodeType, IExpressionType, INextRecord } from './types'
// Program Expression left right operator
// Rule / condition

function RuleConditionNode ({ node, depth = 0 } :{ node: IRuleConditionNode; depth?: number; }) {
const { models, mapped, onChange, maxDepth } = useContext(RuleEditorContext)
const { models, mapped, onChange, maxDepth, operators } = useContext(RuleEditorContext)
const { left, right, operator } = node
const OperatorMap = operators || OPERATOR_TYPE_MAP['*']

return <RuleConditionNodeWrapper>
<Box direction='row' spacing={8} wrap>
Expand All @@ -17,7 +18,7 @@ function RuleConditionNode ({ node, depth = 0 } :{ node: IRuleConditionNode; dep
{/* 2. 操作符 */}
<OperatorSelect
defaultValue={operator}
dataSource={OPERATOR_TYPE_MAP['*']}
dataSource={OperatorMap}
onChange={(value, action, item) => {
node.operator = value
onChange()
Expand Down Expand Up @@ -153,11 +154,12 @@ function RuleGroupNode ({ node, depth = 0, hasBackground, hasBorder }: { node: I
interface IRuleEditorProps {
models: IRuleModel[];
content: IRuleGroupNode;
operators?: INextRecord[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类型声明的命名似乎不合理

maxDepth?: number;
onChange?: (content: IRuleGroupNode) => void;
}

export default ({ models: remoteModels, content: remoteContent, maxDepth, onChange } : IRuleEditorProps) => {
export default ({ models: remoteModels, content: remoteContent, operators: remoteOperators, maxDepth, onChange } : IRuleEditorProps) => {
const [content, setContent] = useState<IRuleGroupNode>(remoteContent)
useEffect(() => {
const { content: nextContent, changed } = fixContent(content)
Expand All @@ -177,6 +179,7 @@ export default ({ models: remoteModels, content: remoteContent, maxDepth, onChan
setContent({ ...content })
if (onChange) onChange({ ...content })
},
operators: remoteOperators,
maxDepth
}}
>
Expand Down
3 changes: 2 additions & 1 deletion src/RuleEditorParts.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled, { css } from 'styled-components'
import { Box, Button, DatePicker, Input, NumberPicker, Range, Select, Switch, TimePicker } from '@alifd/next'
import { IRuleGroupNode, IRuleConditionNode, IRuleField, IMemberExpression, IRuleModel, IRelation, IRuleNodeType, IExpressionType } from './types/index'
import { IRuleGroupNode, IRuleConditionNode, IRuleField, IMemberExpression, IRuleModel, IRelation, IRuleNodeType, IExpressionType, INextRecord } from './types/index'
import React, { createContext, ReactNode, useContext, useEffect, useState } from 'react'
import { uuid } from './shared'
import moment from 'moment'
Expand Down Expand Up @@ -55,6 +55,7 @@ interface IRuleEditorContext {
[id: string]: IRuleConditionNode | IRuleGroupNode
};
onChange: (nextContent?: any) => void;
operators?: INextRecord[];
maxDepth?: number;
}
export const RuleEditorContext = createContext<IRuleEditorContext>(undefined)
Expand Down