-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
242 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/ui/src/components/checkbox/CheckboxGroupRenderer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { DId } from '../../utils/types'; | ||
import type { DFormControl } from '../form'; | ||
|
||
import { useGeneralContext, useDValue } from '../../hooks'; | ||
import { cloneHTMLElement, registerComponentMate } from '../../utils'; | ||
import { useFormControl } from '../form'; | ||
import { useComponentConfig } from '../root'; | ||
import { DCheckbox } from './Checkbox'; | ||
|
||
export interface DCheckboxItem<V extends DId> { | ||
label: React.ReactNode; | ||
value: V; | ||
disabled?: boolean; | ||
} | ||
export interface DCheckboxGroupRendererProps<V extends DId> { | ||
dFormControl?: DFormControl; | ||
dModel?: V[]; | ||
dList: DCheckboxItem<V>[]; | ||
dDisabled?: boolean; | ||
dRender: (nodes: React.ReactElement[]) => JSX.Element | null; | ||
onModelChange?: (values: V[]) => void; | ||
} | ||
|
||
const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DCheckbox.GroupRenderer' as const }); | ||
export function DCheckboxGroupRenderer<V extends DId>(props: DCheckboxGroupRendererProps<V>): JSX.Element | null { | ||
const { dFormControl, dList, dModel, dDisabled = false, dRender, onModelChange } = useComponentConfig(COMPONENT_NAME, props); | ||
|
||
//#region Context | ||
const { gDisabled } = useGeneralContext(); | ||
//#endregion | ||
|
||
const formControlInject = useFormControl(dFormControl); | ||
const [value, changeValue] = useDValue<V[]>([], dModel, onModelChange, undefined, formControlInject); | ||
|
||
const disabled = dDisabled || gDisabled || dFormControl?.control.disabled; | ||
|
||
return dRender( | ||
dList.map((item, index) => ( | ||
<DCheckbox | ||
key={item.value} | ||
dDisabled={item.disabled || disabled} | ||
dInputRender={(el) => | ||
cloneHTMLElement(el, { | ||
['data-form-item-label-for' as string]: index === 0, | ||
}) | ||
} | ||
dModel={value.includes(item.value)} | ||
onModelChange={(checked) => { | ||
changeValue((draft) => { | ||
if (checked) { | ||
draft.push(item.value); | ||
} else { | ||
draft.splice( | ||
draft.findIndex((v) => v === item.value), | ||
1 | ||
); | ||
} | ||
}); | ||
}} | ||
> | ||
{item.label} | ||
</DCheckbox> | ||
)) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './Checkbox'; | ||
export * from './CheckboxGroup'; | ||
export * from './CheckboxGroupRenderer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.