-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(element): support breakpoints for FormLayout (#2340)
* feat(element): support breakpoints for FormLayout * docs(element): update docs
- Loading branch information
Showing
7 changed files
with
202 additions
and
55 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
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
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
115 changes: 115 additions & 0 deletions
115
packages/element/src/form-layout/useResponsiveFormLayout.ts
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,115 @@ | ||
import { isArr, isValid } from '@formily/shared' | ||
import { onMounted, Ref, ref } from 'vue-demi' | ||
|
||
interface IProps { | ||
breakpoints?: number[] | ||
layout?: | ||
| 'vertical' | ||
| 'horizontal' | ||
| 'inline' | ||
| ('vertical' | 'horizontal' | 'inline')[] | ||
labelCol?: number | number[] | ||
wrapperCol?: number | number[] | ||
labelAlign?: 'right' | 'left' | ('right' | 'left')[] | ||
wrapperAlign?: 'right' | 'left' | ('right' | 'left')[] | ||
[props: string]: any | ||
} | ||
|
||
interface ICalcBreakpointIndex { | ||
(originalBreakpoints: number[], width: number): number | ||
} | ||
|
||
interface ICalculateProps { | ||
(target: Element, props: IProps): IProps | ||
} | ||
|
||
interface IUseResponsiveFormLayout { | ||
( | ||
props: IProps, | ||
refs: { | ||
[key: string]: Vue | Element | Vue[] | Element[] | ||
} | ||
): { | ||
props: Ref<IProps> | ||
} | ||
} | ||
|
||
const calcBreakpointIndex: ICalcBreakpointIndex = (breakpoints, width) => { | ||
for (let i = 0; i < breakpoints.length; i++) { | ||
if (width <= breakpoints[i]) { | ||
return i | ||
} | ||
} | ||
} | ||
|
||
const calcFactor = <T>(value: T | T[], breakpointIndex: number): T => { | ||
if (Array.isArray(value)) { | ||
if (breakpointIndex === -1) return value[0] | ||
return value[breakpointIndex] ?? value[value.length - 1] | ||
} else { | ||
return value | ||
} | ||
} | ||
|
||
const factor = <T>(value: T | T[], breakpointIndex: number): T => | ||
isValid(value) ? calcFactor(value as any, breakpointIndex) : value | ||
|
||
const calculateProps: ICalculateProps = (target, props) => { | ||
const { clientWidth } = target | ||
const { | ||
breakpoints, | ||
layout, | ||
labelAlign, | ||
wrapperAlign, | ||
labelCol, | ||
wrapperCol, | ||
...otherProps | ||
} = props | ||
const breakpointIndex = calcBreakpointIndex(breakpoints, clientWidth) | ||
|
||
return { | ||
layout: factor(layout, breakpointIndex), | ||
labelAlign: factor(labelAlign, breakpointIndex), | ||
wrapperAlign: factor(wrapperAlign, breakpointIndex), | ||
labelCol: factor(labelCol, breakpointIndex), | ||
wrapperCol: factor(wrapperCol, breakpointIndex), | ||
...otherProps, | ||
} | ||
} | ||
|
||
export const useResponsiveFormLayout: IUseResponsiveFormLayout = ( | ||
props, | ||
refs | ||
) => { | ||
const root = ref<Element>(null) | ||
const { breakpoints } = props | ||
if (!isArr(breakpoints)) { | ||
return { props: ref(props) } | ||
} | ||
const layoutProps = ref<IProps>({}) | ||
|
||
const updateUI = () => { | ||
layoutProps.value = calculateProps(root.value, props) | ||
} | ||
|
||
onMounted(() => { | ||
root.value = refs.root as Element | ||
const observer = () => { | ||
updateUI() | ||
} | ||
const resizeObserver = new ResizeObserver(observer) | ||
if (root.value) { | ||
resizeObserver.observe(root.value) | ||
} | ||
|
||
updateUI() | ||
|
||
return () => { | ||
resizeObserver.disconnect() | ||
} | ||
}) | ||
|
||
return { | ||
props: layoutProps, | ||
} | ||
} |