Skip to content

Commit

Permalink
[add] Select component to support value extend mode
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Mar 15, 2024
1 parent e93e62a commit a34930f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
17 changes: 11 additions & 6 deletions packages/xgen/components/edit/Select/components/Extend/index.less
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
._local {
padding: 6px;
border-top: 1px solid var(--color_border_soft);
border-top: 1px solid var(--color_border_soft);

:global {
.input_extend {
width: calc((100% - 36px) / 2 - 6px);
box-shadow: none;
box-shadow: none;
}

.btn_extend{
width: 36px;
height: 36px;
}
.value_extend {
width: calc(100% - 36px - 6px);
box-shadow: none;
}

.btn_extend {
width: 36px;
height: 36px;
}
}
}
36 changes: 25 additions & 11 deletions packages/xgen/components/edit/Select/components/Extend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,45 @@ import { CheckOutlined } from '@ant-design/icons'
import styles from './index.less'

import type { IPropsExtend } from '../../types'
import { getLocale } from '@umijs/max'

const Index = (props: IPropsExtend) => {
const { addOptionItem } = props
const { addOptionItem, valueOnly, valuePlaceholder, labelPlaceholder } = props
const x = useReactive({ label: '', value: '' })

const add = () => {
if (!x.label || x.value === '') return message.warning('label,value不可为空')
if (!valueOnly && (!x.label || x.value === '')) return message.warning('label,value不可为空')
if (valueOnly && x.value === '') return message.warning('value不可为空')

addOptionItem(x.label, x.value)
// ValueOnly mode label = value
if (valueOnly) {
x.label = x.value
}

addOptionItem(x.label, x.value)
x.label = ''
x.value = ''
}

const is_cn = getLocale() === 'zh-CN'
const placeholderValue = valuePlaceholder ? valuePlaceholder : is_cn ? '添加选项' : 'Add Option'
const placeholderLabel = labelPlaceholder ? labelPlaceholder : is_cn ? '添加选项名称' : 'Add Option Name'
const className = valueOnly ? 'value_extend' : 'input_extend'

return (
<div className={clsx([styles._local, 'w_100 flex justify_between align_center'])}>
{!valueOnly && (
<Input
className='input_extend'
placeholder={placeholderLabel}
value={x.label}
onChange={(e) => (x.label = e.target.value)}
></Input>
)}

<Input
className='input_extend'
placeholder='label'
value={x.label}
onChange={(e) => (x.label = e.target.value)}
></Input>
<Input
className='input_extend'
placeholder='value'
className={className}
placeholder={placeholderValue}
value={x.value}
onChange={(e) => (x.value = e.target.value)}
></Input>
Expand Down
21 changes: 17 additions & 4 deletions packages/xgen/components/edit/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ const Custom = window.$app.memo((props: ICustom) => {
})

const Index = (props: IProps) => {
const { __bind, __name, itemProps, extend, ...rest_props } = props
const {
__bind,
__name,
itemProps,
extend,
extendValue,
extendValuePlaceholder,
extendLabelPlaceholder,
...rest_props
} = props
const [x] = useState(() => container.resolve(Model))

useLayoutEffect(() => {
Expand All @@ -67,13 +76,17 @@ const Index = (props: IProps) => {
})

const extra_props = useMemo(() => {
if (!extend) return {}

if (!extend && !extendValue) return {}
return {
dropdownRender: (items) => (
<Fragment>
{items}
<Extend addOptionItem={addOptionItem}></Extend>
<Extend
addOptionItem={addOptionItem}
valueOnly={extendValue}
valuePlaceholder={extendValuePlaceholder}
labelPlaceholder={extendLabelPlaceholder}
></Extend>
</Fragment>
)
} as SelectProps
Expand Down
6 changes: 6 additions & 0 deletions packages/xgen/components/edit/Select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type { SelectProps } from 'antd'

export interface IProps extends Remote.IProps, SelectProps {
extend?: boolean
extendValue?: boolean
extendValuePlaceholder?: string
extendLabelPlaceholder?: string
}

export interface ICustom extends SelectProps {
Expand All @@ -13,4 +16,7 @@ export interface ICustom extends SelectProps {

export interface IPropsExtend {
addOptionItem: (label: string, value: string) => void
valueOnly?: boolean
valuePlaceholder?: string
labelPlaceholder?: string
}

0 comments on commit a34930f

Please sign in to comment.