Skip to content

Commit

Permalink
fix(grid): doesn't adjust it's content to fit responsive config in SS…
Browse files Browse the repository at this point in the history
…R page, closes tusen-ai#2462
  • Loading branch information
07akioni authored and zhoukekun committed Jun 22, 2022
1 parent 35b024f commit 25ed99a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Fix `n-color-picker` can't input alpha value correctly manually.
- Fix some components don't work correctly if `__VUE_OPTIONS_API__` is set to `false`, closes [#3146](https://github.com/TuSimple/naive-ui/issues/3146).
- Fix `n-grid` doesn't adjust it's content to fit responsive config in SSR page, closes [#2462](https://github.com/TuSimple/naive-ui/issues/2462).

## 2.30.5

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- 修复 `n-color-picker` 手动输入 alpha 值时不生效
- 修复某些组件在 `__VUE_OPTIONS_API__` 设为 `false` 时工作不正常的问题,关闭 [#3146](https://github.com/TuSimple/naive-ui/issues/3146)
- 修复 `n-grid` 在 SSR 页面挂载后不会正确的适配响应式,关闭 [#2462](https://github.com/TuSimple/naive-ui/issues/2462)

## 2.30.5

Expand Down
20 changes: 9 additions & 11 deletions src/grid/src/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { defaultSpan, gridInjectionKey } from './config'

const defaultCols = 24

const ssrAttrName = 'data-ssr-key'
const SSR_ATTR_NAME = '__ssr__'

const gridProps = {
responsive: {
Expand Down Expand Up @@ -60,7 +60,7 @@ const gridProps = {
} as const

export interface NGridInjection {
isSSR: Ref<boolean>
isSsrRef: Ref<boolean>
itemStyleRef: Ref<CSSProperties | string | undefined>
xGapRef: Ref<string | undefined>
overflowRef: Ref<boolean>
Expand Down Expand Up @@ -123,29 +123,27 @@ export default defineComponent({
)

// for SSR, fix bug https://github.com/TuSimple/naive-ui/issues/2462
const isSSRRef = ref(false)
const isSsrRef = ref(false)
const contentElRef = ref<HTMLElement>()
onMounted(() => {
const { value: contentEl } = contentElRef
if (contentEl) {
const ssrKey = contentEl.getAttribute(ssrAttrName)
/* istanbul ignore if */
if (ssrKey) {
contentEl.removeAttribute(ssrAttrName)
isSSRRef.value = true
if (contentEl.hasAttribute(SSR_ATTR_NAME)) {
contentEl.removeAttribute(SSR_ATTR_NAME)
isSsrRef.value = true
}
}
})

provide(gridInjectionKey, {
isSSR: isSSRRef,
isSsrRef,
itemStyleRef: toRef(props, 'itemStyle'),
xGapRef: responsiveXGapRef,
overflowRef
})

return {
isSSR: !isBrowser,
isSsr: !isBrowser,
contentEl: contentElRef,
mergedClsPrefix: mergedClsPrefixRef,
style: computed<CSSProperties>(() => {
Expand Down Expand Up @@ -293,7 +291,7 @@ export default defineComponent({
ref: 'contentEl',
class: `${this.mergedClsPrefix}-grid`,
style: this.style,
[ssrAttrName]: this.isSSR || null
[SSR_ATTR_NAME]: this.isSsr || undefined
},
this.$attrs
),
Expand Down
17 changes: 3 additions & 14 deletions src/grid/src/GridItem.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
h,
defineComponent,
inject,
getCurrentInstance,
PropType,
Ref
} from 'vue'
import { h, defineComponent, inject, getCurrentInstance, PropType } from 'vue'
import { pxfy } from 'seemly'
import { keysOf } from '../../_utils'
import type { ExtractPublicPropTypes } from '../../_utils'
Expand Down Expand Up @@ -44,18 +37,14 @@ export const gridItemPropKeys = keysOf(gridItemProps)

export type GridItemProps = ExtractPublicPropTypes<typeof gridItemProps>

const track = (target: Ref<unknown>): void => {
Reflect.get(target, 'value')
}

export default defineComponent({
__GRID_ITEM__: true,
name: 'GridItem',
alias: ['Gi'],
props: gridItemProps,
setup () {
const {
isSSR,
isSsrRef,
xGapRef,
itemStyleRef,
overflowRef
Expand All @@ -66,7 +55,7 @@ export default defineComponent({
overflow: overflowRef,
itemStyle: itemStyleRef,
deriveStyle: () => {
track(isSSR)
void isSsrRef.value
// Here is quite a hack, I hope there is a better way to solve it
const {
privateSpan = defaultSpan,
Expand Down

0 comments on commit 25ed99a

Please # to comment.